In my previous post about using Vagrant to run Docker containers, I recommended not to use Boot2Docker. Since then my issues with Boot2Docker have been resolved and now it is my preferred way to use Docker in OSX.

Here is a quick tutorial on how to set up Postgres, Elasticsearch, and Redis as Docker containers using Boot2Docker on Mac OS X.

Boot2Docker for OSX

If you are using OSX and want to use Docker, then Boot2Docker is the recommended tool. It works by using a virtual machine to host Docker and letting the OSX command line ‘remotely’ call it with the docker command.

To install Boot2Docker (requires homebrew): brew install boot2docker

To initialise and start the virtual machine: boot2docker init boot2docker up

Finally, to link the docker command line interface to the Docker host: export DOCKER_IP=boot2docker ip export DOCKER_HOST=boot2docker socket``

Call docker ps to test that the docker is correctly linked, this will return successfully if it is working. If the Docker host is not running, or the DOCKER_HOST environment variable is not set, the error will look something like: Get [http:///var/run/docker.sock/v1.12/containers/json:](http:///var/run/docker.sock/v1.12/containers/json:) dial unix /var/run/docker.sock: no such file or directory

PostgreSQL

Running Postgres as a Docker container is pretty easy, simply execute: docker run -it -p 5432:5432 postgres

The -it links the the standard in and out from the container to the OSX terminal, making it way easier to debug and you to kill a container with Ctrl-C.

You can use psql to check PostgreSQL is working: psql -h $DOCKER_IP -U postgres

Elasticsearch

I don’t like to run Elasticsearch on my local machine because I don’t like the JVM reminding me to update. Instead I now use Docker: docker run -it -p 9200:9200 dockerfile/elasticsearch

You can use Elasticsearch’s HTTP interface to see if it is running: curl $DOCKER_IP:9200

Redis

Running Redis is also pretty easy: docker run -it -p 6379:6379 dockerfile/redis

You can use the redis-cli to check it is running: redis-cli -h $DOCKER_IP

Persistance

These containers are initialised to not store their data, so if you kill or restart them you will lose everything. To properly set them up refer to their documentation on the Docker registry:

  1. postgres
  2. dockerfile/elasticsearch
  3. dockerfile/redis

Further Reading

The Docker Book

The Future of Docker