On Mac or Windows, it lacks the native running environment for containers, thus a virtual machine is needed.
A virtual machine named
boot2docker-vm will hold docker daemon and containers if boot2docker is used. IP 192.168.59.103 will be used for the virtual machine.
In newer distributions, an alternative program name docker-machine do the same task as boot2docker.
if you are using docker-machine, VM
default is used to run daemon, ip 192.168.99.100 will be used.
Docker clients (docker command on terminal) communicate with docker containers through ports, there will be a port mapping between the VM and the containers.
Docker images are stored in the virtual machines (hosts), if there is no such images, it will be downloaded. If you use a new VM as host, the images will be re-downloaded to it.
- create a virtual machine on VirtualBox named
docker-machine create --driver virtualbox dev
- start the virtual machine
$ docker-machine start dev
- export the environmental variable for VM
dev. make sure to run the command every time a new terminal is started, otherwise there could be error “Cannot connect to the Docker daemon.”
$ eval "$(docker-machine env dev)"
- create a new container named “web” with image
$ docker run -d -P --name web nginx
- connect to the container web, remember there will be a port mapping between “dev” and “web”. It means on your Mac, you can talk to the VM with IP address 192.168.99.100. If connected to port 32769, it will redirect to port 80 of container web.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1ff2361e090 nginx "nginx -g 'daemon off" 11 hours ago Up 3 minutes 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp web $ docker-machine ip dev 192.168.99.100
- connect to nginx server with any web browser, just type
http://192.168.99.100:32769, the welcome information will show up.