Docker Community Forums

Share and learn in the Docker community.

The DevOps Series Using Docker with Ansible


(Azharuddin) #1

Docker provides operating system level virtualisation in the form of containers. These containers allow you to run standalone applications in an isolated environment. The three important features of Docker containers are isolation, portability and repeatability. 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 lightweight 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 implemented using the Go programming 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 applications and help you to easily scale horizontally.

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:


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


update_cache: yes

  • name: Install dependencies


name: “{{ item }}”

state: latest


  • python2-docker

  • docker

  • service:

name: docker

state: started

  • name: Run the hello-world container


name: hello-world

image: library/hello-world

The Parabola package repository is updated before proceeding to install the dependencies. The python2-docker package is required for use with Ansible. Get DevOps online training for graining more knowledge. 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/configuration/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 dependencies] *********************************

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 unreachable=0 failed=0
With the verbose ‘-v’ option to ansible-playbook, you will see an entry for LogPath, such as /var/lib/docker/containers//-json.log. In this log file, you will see the output of the execution of the hello-world 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 installation 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