Get started with new Docker toys: docker-machine, docker-compose and docker-swarm

Docker released docker-compose, docker-machine and docker-swarm. I fell in love. Three new great tools in the Docker toolbelt.

Get the party started

Well, since I’m on Mac OS X 10.11 and lazy, I used Homebrew to install these tools.

brew install docker docker-machine docker-compose

(When writing this blog post, Docker announced another new product; docker-toolbox, a complete installer to provide everything to get started with Docker. )

docker-machine

docker-machine is a tool to create/configure Docker-enabled machines. (Whether this is on your local machine or in the cloud). It replaces boot2docker, but is currently in Beta. (But hey, living on the edge ;-))

docker-machine create -d virtual box dev

When using the virtual box deamon, it uses the boot2docker image to create a lightweight virtual machine.

After creation, fire up the machine with docker-machine start dev and set the environment variables so docker knows which host to contact: docker-machine env dev.

docker-composer

Docker describes docker-compose as follows: Compose is a tool for defining and running multi-container applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.

Now, let's take a look at a docker-compose configuration I used for a project:

nginxapi:
  build: docker/nginx-api
  command: nginx -g "daemon off;"
  links:
    - php
  ports:
    - "8080:8080"
nginxangular:
  build: docker/nginx-angular
  command: nginx -g "daemon off;"
  ports:
    - "80:80"
  volumes:
      - ./angular:/app/angular
php:
  build: docker/php
  volumes:
    - .:/app
  working_dir: /app/public
  command: php-fpm
  links:
    - db
    - cache
  ports:
    - "9000:9000"
  environment:
    APP_ENV: local
    APP_DEBUG: true
    APP_KEY: "yolo"
    APP_LOCALE: en
    APP_FALLBACK_LOCALE: en
    DB_CONNECTION: mysql
    DB_HOST: db
    DB_DATABASE: lumen
    DB_USERNAME: lumen
    DB_PASSWORD: secret
    MEMCACHED_HOST: cache
    CACHE_DRIVER: memcached
db:
  image: mariadb
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: secret
    MYSQL_DATABASE: lumen
    MYSQL_USER: lumen
    MYSQL_PASSWORD: secret
cache:
  image: memcached
compass:
  image: antonienko/compass-watch
  command: watch --poll /src/resources/sass
  volumes:
    - .:/src

In this example you see multiple containers defined: * two nginx containers (one for an api, another one for an angular app) * one php container for the api * one database container (mariadb) * one caching container (memcached) * one compass container

As you see, docker-compose provides you with an easy way to define your whole stack in just one configuration file.

We’ve set up an environment with docker-machine, now lets run the stack I described above: docker-compose build & docker-compose up -d. Your stack is now running! To check out the IP address of your machine, run: docker-machine ls.

docker-swarm

I didn’t experiment with docker-swarm. You can watch the following Youtube video to get an impression of the possibilities:

Notes

Error while starting container

If you get an error like:

Cannot start container : [8] System error: no such file or directory

make sure the working directory exists, otherwise the process will be killed immediately.

Uninstall boot2docker

It’s possible to migrate from a boot2docker vm to docker-machine environment. I chose not to migrate, and uninstalled boot2docker with the following uninstall script:

Uninstall boot2docker

You also need to delete: /Application/boot2docker.app /Users/{user}/VirtualBox VMs/boot2docker-vm