Easier work with docker-machine

Okay,… I think now everyone knows that i like it to work with docker-machine. This tutorial should give some hints – how to put local files and directories into a Docker container and change the default VM settings.


Let’s start local

# create new project (local)
$ mkdir -p ~/Projects/tutorial-project/config && cd ~/Projects/tutorial-project

# create some files and directory (local)
$ touch Dockerfile && touch .dockerignore && touch .git && touch config/file_{1..5}

# show content (local)
$ tree -a .
├── .dockerignore
├── .git
├── Dockerfile
└── config
    ├── file_1
    ├── file_2
    ├── file_3
    ├── file_4
    └── file_5

# edit Dockerfile (local)
$ echo -e "FROM centos:7\nRUN yum install -y tree\nCOPY . /home/docker-target" > Dockerfile

# edit .dockerignore (local)
$ echo -e ".*\nDockerfile\n*/file_5" > .dockerignore

Create new VM with specific settings

# create new VM (local)
$ docker-machine create -d virtualbox --virtualbox-cpu-count "2" --virtualbox-memory "2048" tutorial-vm

Note: there is a very well documented reference for all supported drivers!

Copy local stuff into vm

# copy local folder into VM (local)
$ docker-machine scp -r ~/Projects/tutorial-project tutorial-vm:tutorial-project

# ssh into VM (local)
$ docker-machine ssh tutorial-vm

# show content of directory (VM)
$ ls -la ~/tutorial-project/
$ ls -la ~/tutorial-project/config/

Create Docker image and container

# create Docker image from Dockerfile (VM)
$ docker build -t centos/tutorial ~/tutorial-project/

# create Docker container from image (VM)
$ docker run -ti --rm centos/tutorial

# show content of directory (Container)
$ tree -a /home/docker-target/
`-- config
    |-- file_1
    |-- file_2
    |-- file_3
    `-- file_4

Bang… this is just one way… !