Home Ansible logo

Using Ansible for Docker deployment

Both Ansible and Docker are great tools, and when you're managing (development) environments you might already have been using one or both. However, if you use only one of these tools (or both separately), you might still be missing out.

Combining these tools and deploying Docker containers using Ansible might utilize the full potential of your development environment.

About Ansible

Ansible is a configuration management tool that uses SSH to provision servers based on predefined "playbooks". Ansible is often used to install and configure software packages like Apache, NGINX, MySQL, Postgres etc.

Installing a server using Ansible is as easy as running a simple script. When the script is finished, your server is up running. Awesome.

About Docker

Docker is a cool platform that allows you to "containerize" services with minimum a amount of overhead. Docker containers behave more or less like virtual machines but require less resources.

Once a service has been containerized it behaves like an isolated (jailed) environment and won't conflict with other packages installed on the system (or other containers). In addition, the entire setup of a Docker container is usually managed in a "Dockerfile" and may be reproduced at any time. Awesome.

The issue

Both Ansible and Docker are pretty neat tools. But using both of them might let you end up repeating yourself. Something you should not want.

A typical use case for Docker is to simulate the production environment. By providing the same webserver, libraries and configuration as the production environment a Docker container enables you to test code as if it were running on a production machine. The problem is that when something changes on production. The Docker containers will need maintenance as well.

The root of this problem is that the configuration is managed in 2 locations:

  • On the server (perhaps using Ansible/Puppet/Chef).
  • In the Dockerfile.

Having a configuration managed in multiple places is anything but DRY. But there might be a solution:

Deploy Docker using Ansible

To some extend, Docker is actually a configuration management tool. It is quite easy to configure a service using a Dockerfile and then start a docker container on the production environment. Ansible has some built-in tools for Docker to do just that.

Let's imagine a situation where you're working on a Django based website:

  • In development you'll be able to run the built-in testing server.
  • If you need a more production like environment for testing you'll be able to run the Docker container locally.
  • When you go live, you use Ansible to deploy the Docker on the production server.

The situation above is perfectly DRY and ensures that the development environment is a spot-on clone of the production environment.


There is is still some debate about whether Docker is production ready or not. If you do run it in production you should definatly know what you're doing or you might end up with downtime or even losing data.

By far the biggest pitfall is managing data. Understanding how to persist data on a Docker platform is crucial when you're running it in production. The Docker website has a detailed article on this subject.

Another pitfall may be that Docker might be wasting storage space with outdated data. It might be nessesary to periodically clean up some old data. I earlier wrote an article about cleaning up old images and containers.

If you manage to overcome the caveats of Docker and you run your services in containers you'll be able to move your applications around quite easily and have the exact same development environment as if it were in production. Awesome.

Written by svenv on Sept. 13, 2015 Permalink