How to setup a development environment with Docker

Docker installation:

Docker runs natively on Linux, to use it on Mac OS or Windows you need to run it in a Virtual Machine. For this blog entry I will use running docker on Mac OS in VirtualBox. I recommend using brew to install it because it also takes care of updating it.

brew install docker docker-compose docker-machine
brew cask install virtualbox

If you want to do it by hand instead, follow the Tutorial at https://docs.docker.com/engine/installation/mac/ or https://docs.docker.com/engine/installation/windows/

Start Docker:

After you installed docker-machine you need to create a Virtual Machine that runs the docker daemon inside a tiny Linux guest:

docker-machine create --driver=virtualbox --virtualbox-memory 2048 --virtualbox-cpu-count 2 dev

The output will look like:

Creating CA: /Users/cornel/.docker/machine/certs/ca.pem
Creating client certificate: /Users/cornel/.docker/machine/certs/cert.pem
Image cache does not exist, creating it at /Users/cornel/.docker/machine/cache...
No default boot2docker iso found locally, downloading the latest release...
Downloading https://github.com/boot2docker/boot2docker/releases/download/v1.8.2/boot2docker.iso to /Users/cornel/.docker/machine/cache/boot2docker.iso...
Creating VirtualBox VM...
Creating SSH key...
Starting VirtualBox VM...
Starting VM...
To see how to connect Docker to this machine, run: docker-machine env dev

Verify that the Virtual Machine is running:

docker-machine ls

The output of the command should show the generated Virtual Machine:

NAME   ACTIVE   DRIVER       STATE     URL                         SWARM
dev             virtualbox   Running   tcp://192.168.99.100:2376

Configure shell environment:

The local docker client that will access the docker daemon inside the Virtual Machine needs to know how it should access it. docker-machine can show you the needed configuration, execute:

docker-machine env dev

The command output depends on the detected shell that you used to execute the command:

set -x DOCKER_TLS_VERIFY "1";
set -x DOCKER_HOST "tcp://192.168.99.100:2376";
set -x DOCKER_CERT_PATH "/Users/cornel/.docker/machine/machines/dev";
set -x DOCKER_MACHINE_NAME "dev";
# Run this command to configure your shell:
# eval (docker-machine env dev)

You can execute the given commands by hand or output the configuration file of your shell:

  • bash shell: ~/.bash_profile
  • fish shell: ~/.config/fish/config.fish

To test the setup, list all running docker containers with the command:

docker ps

Working with docker containers:

To see which docker containers are currently running:

docker ps

To connect into a container, use the container name shown by docker ps and the following command. Press enter after the command is executed once to see the actual shell. You have to exit the shell by pressing Ctrl+P+Q or closing the terminal, using Ctrl+D or exit as a command will shut down the container.

docker attach <containername>

A different way to attach to a container where you can exit the shell in a normal way:

docker exec -it <containername> /bin/bash

To see the log output from a container:

docker logs <containername>

You can watch the log output of a container with:

docker logs --tail=10 -f <containername>

Issues

Mounting a directory from outside into the docker container has performance issues on Mac OS: http://stackoverflow.com/questions/30090007/whats-the-right-way-to-setup-a-development-environment-on-os-x-with-docker

You may also like