Gangmax Blog

Docker Machine & Local VM

This post is about using docker-machine to create, use and manage a Docker host inside of a local virtual machine. From here.

First you need to install the latest version of Docker and VirtualBox. In my case they are “Docker 18.03.1-ce for Mac” and “VirtualBox 5.2.10 for Mac”.

Create a machine(VM)

1
2
docker-machine create --driver virtualbox default
docker-machine ls

Connect to the machine(VM)

First, update your local Docker environment variables to make your local Docker client connect to Docker inside the created machine rather than your local Docker.

1
2
3
4
5
6
7
8
9
10
11
# 1. Show the content.
$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.16.62.130:2376"
export DOCKER_CERT_PATH="/Users/<yourusername>/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval "$(docker-machine env default)"

# 2. Set to environment.
$ eval "$(docker-machine env default)"

This should be done every time when you open a new terminal in which you want to operate the Docker machine. Or you can set this into your “/.zshrc” or “/.bashrc” file, but if you do so, you have to make sure the Docker machine(VM) is running. More details of how to do so can be found in the official document here.

Operation

1
2
3
4
5
6
7
8
# 1. Get the host IP address.
docker-machine ip default

# 2. Run a Nginx web server.
docker run -d -p 8000:80 nginx

# 3. Verify Nginx running status.
curl $(docker-machine ip default):8000

Start and stop machines

1
2
docker-machine stop default
docker-machine start default

Use “unset” command to stop running Docker commands on the target machine

1
2
3
4
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_HOST

Besides the usage on local machine(VM), Docker Machine is also working with Docker host cloud providers. More details can be found here.

Comments