OpenSource For You

The DevOps Series Using Docker with Ansible

This article is the eighth in the DevOps series. This month, we shall learn to set up Docker in the host system and use it with Ansible.


Docker provides operating system level virtualisa­tion in the form of containers. These containers allow you to run standalone applicatio­ns in an isolated environmen­t. The three important features of Docker containers are isolation, portabilit­y and repeatabil­ity. All along we have used Parabola GNU/Linux-libre as the host system, and executed Ansible scripts on target virtual machines (VM) such as CentOS and Ubuntu.

Docker containers are extremely lightweigh­t and fast to launch. You can also specify the amount of resources that you need such as the CPU, memory and network. The Docker technology was launched in 2013, and released under the Apache 2.0 licence. It is implemente­d using the Go programmin­g language. A number of frameworks have been built on top of Docker for managing these clusters of servers. The Apache Mesos project, Google’s Kubernetes, and the Docker Swarm project are popular examples. These are ideal for running stateless applicatio­ns and help you to easily scale horizontal­ly.

Setting it up

The Ansible version used on the host system (Parabola GNU/Linux-libre x86_64) is Internet access should be available on the host system. The ansible/ folder contains the following file: ansible/playbooks/configurat­ion/docker.yml


The following playbook is used to install Docker on the host system:

--name: Setup Docker hosts: localhost gather_facts: true become: true tags: [setup]


- name: Update the software package repository pacman: update_cache: yes

- name: Install dependenci­es package: name: “{{ item }}” state: latest

with_items: - python2-docker - docker

- service: name: docker state: started

- name: Run the hello-world container docker_container: name: hello-world image: library/hello-world

The Parabola package repository is updated before proceeding to install the dependenci­es. The python2-docker package is required for use with Ansible. Hence, it is installed along with the docker package. The Docker daemon service is then started and the library/hello-world container is fetched and executed. A sample invocation and execution of the above playbook is shown below:

$ ansible-playbook playbooks/configurat­ion/docker.yml -K --tags=setup

SUDO password:

PLAY [Setup Docker] *****************************************

TASK [Gathering Facts] ************************************** ok: [localhost]

TASK [Update the software package repository] *************** changed: [localhost]

TASK [Install dependenci­es] ********************************* ok: [localhost] => (item=python2-docker) ok: [localhost] => (item=docker)

TASK [service] ********************************************** ok: [localhost]

TASK [Run the hello-world container] ************************ changed: [localhost]

PLAY RECAP ************************************************** localhost : ok=5 changed=2 unreachabl­e=0 failed=0

With the verbose ‘-v’ option to ansible-playbook, you will see an entry for LogPath, such as /var/lib/docker/ containers/<container-id>/<container-id>-json.log. In this log file, you will see the output of the execution of the helloworld container. This output is the same when you run the container manually as shown below:

$ sudo docker run hello-world Hello from Docker!

This message shows that your installati­on appears to be working correctly.

To generate this message, Docker took the following steps:

1. The Docker client contacted the Docker daemon.

2. The Docker daemon pulled the hello-world image from the Docker Hub.

3. The Docker daemon created a new container from that image, which runs the executable that produces the output you are currently reading.

4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

$ docker run -it ubuntu bash

You can share images, automate workflows, and more with a free Docker ID at

For more examples and ideas, do visit https://docs.docker. com/engine/userguide/.

An example

A deep learning (DL) Docker project is available ( with support for frameworks, libraries and software tools. We can use Ansible to build the entire DL container from the source code of the tools. The base OS of the container is Ubuntu 14.04, and will include the following software packages: TensorFlow





Torch iPython/Jupyter Notebook




Scikit Learn



The playbook to build the DL Docker image is given below:

- name: Build the dl-docker image hosts: localhost gather_facts: true become: true tags: [deep-learning]


DL_BUILD_DIR: “/tmp/dl-docker” DL_DOCKER_NAME: “floydhub/dl-docker”

 ??  ??

Newspapers in English

Newspapers from India