How to build docker container using ansible

What is Docker?

The leading container platform that allows you to wrap your app, all of its dependencies, and library into a small executable bundle that can be run anywhere.

What is Ansible?

Ansible is an open-source DevOps tool that can help the business in configuration management, deployment, provisioning, etc. It is designed to automate system management tasks. Ansible “programs” are called plays and are a collection of tasks in a playbook. It uses the playbook to describe automation jobs, and the playbook uses a very simple language YAML.

How to build a Debian docker container using ansible-playbook?

Below basic directory structure of the ansible files.

docker_demo % ls -l

total 24

-rw-r — r — 1 root root103 Dec 2 20:10 Dockerfile

-rw-r — r — 1 root root597 Dec 2 20:27 build.yml

-rw-r — r — 1 root root 417 Dec 2 20:05 host

I have a Dockerfile, an Ansible inventory, and playbooks for working with the Ansible Docker image module.

Docker file
I am going to bring up Debian container and install cowsay package inside the container

docker_demo % cat Dockerfile

FROM debian

RUN apt-get update && apt-get install -y cowsay fortune

ENTRYPOINT [“/usr/games/cowsay”]

Inventory file
I am going to install the docker image using ansible locally, you can also update the inventory file for remote device support.

docker_demo % cat host

[var]

10.10.10.10 ansible_ssh_connection=local

Ansible Playbook

I am creating the directory in the tmp path, copy the docker file from the current directory to tmp directory and then build the docker file.

docker_demo % cat build.yml

— -

- hosts: all

connection: local

gather_facts: no

tasks:

- name: create build directory

file:

path: /tmp/demo-container

state: directory

owner: root

group: root

mode: ‘0755’

- name: copy Dockerfile

copy:

src: ./Dockerfile

dest: /tmp/demo-container/Dockerfile

owner: root

group: root

mode: ‘0644’

- name: build container image

docker_image:

build:

path: /tmp/demo-container

source: build

name: demo-container:v1.0

state: present

Running the playbook

Below the command to run the ansible playbook

docker_demo % sudo ansible-playbook -i host build.yml

PLAY [all] ****************************************************************************************************************************************************************

TASK [create build directory] *********************************************************************************************************************************************

ok: [10.10.10.10]

TASK [copy Dockerfile] ****************************************************************************************************************************************************

ok: [10.10.10.10]

TASK [build container image] **********************************************************************************************************************************************

[DEPRECATION WARNING]: The default for build.pull is currently ‘yes’, but will be changed to ‘no’ in community.general 2.0.0. Please set build.pull explicitly to the

value you need. This feature will be removed from community.general in version 2.0.0. Deprecation warnings can be disabled by setting deprecation_warnings=False in

ansible.cfg.

ok: [10.10.10.10]

PLAY RECAP ****************************************************************************************************************************************************************

10.10.10.10 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

You can see the docker image using below command

docker_demo % docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

demo-container v1.0 f21d3b8705d7 53 seconds ago 181MB

Network Automation Engineer