How to Build Docker Images with Ansible on Ubuntu 20.04

Today I would like to show you how to build Docker images with Ansible.

But first, let me briefly tell you what Docker is. Docker is virtualization software and allows you to package your applications in containers.

What is Ansible is a software for configuration management, in particular configuration management of operating systems.

First we need to install Docker and Ansible, then we will create a Dockerfile and build a container for the Web server. After that, we will create a simple Ansible playbook to create a Docker image for another web server.

Installing Docker

sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

curl -fsSL | sudo apt-key add -

sudo add-apt-repository “deb [arch=amd64] $(lsb_release -cs) stable”

sudo apt update

sudo apt install docker-ce docker-ce-cli

sudo groupadd docker

sudo usermod -aG docker $USER

Now you need to log in to the operating system and check the version of Docker installed:

docker --version

Installing Ansible

sudo apt install ansible


Installing additional software

Above, we installed the main software, but for full-fledged work, we need to install additional packages.

sudo apt install python3-pip

pip3 install docker

Now let’s write the code

You can download all the code that will be provided next. in my GitHub repository.

We create a docker-build folder and create a Dockerfile file in it, it will be used to create a Docker image.

File contents:

FROM nginx:latest

# Default Nginx static file location
# Ideally, we would edit the nginx.conf for new configuration
WORKDIR /usr/share/nginx/html

# Add all local static files to image working directory
ADD ./ ./

# If there was a lot of ADD COPY and RUN Steps, Bash script file is common practice
# To bootstrap the image without additional layers
# RUN /

# self explanatory

Now let’s create an ansible-build folder next to the docker-build folder. In it we create the docker-image-playbook.yaml file

File contents:

- name: Pull latest Nginx Image
  hosts: localhost
    - name: Pull Nginx Image
        name: «nginx-ansible-base»
        image: «nginx:latest»
        state: started
        command: tail -f /dev/null
    - name: Add Nginx Image to Ansible Hosts
        name: «nginx-ansible-base»
        ansible_connection: docker
        ansible_ssh_user: root
- name: Configure Nginx Base Image for deploying Webserver
  hosts: «nginx-ansible-base»
  gather_facts: false
    - name: Install Python3
      raw: apt update && apt upgrade -y && apt install python3 -y
    - name: Install Rsync
        name: rsync
    - name: Copy local configuration Files to Nginx Conf
        src: ./website/
        dest: /usr/share/nginx/html/
    - name: Ensure NGINX Is enabled
        name: nginx
        enabled: yes
    - name: Clean up Python 3 and Prerequisites
      raw: apt purge python3 rsync -y
- name: Snapshot base image to create newly configured Image
  hosts: localhost
    - name: Commit Docker image
      command: docker commit «nginx-ansible-base» «nginx-ansible-build-demo»
- name: Clean Up Docker Containers
  hosts: localhost
    - name: Remove Running Base Image
        name: nginx-ansible-base
        state: absent
        force_kill: yes

This file describes the commands that Ansible will execute.

In fact, this file describes how to build a Docker container based on the image we need, install dependencies, and copy the necessary files. I see no reason to copy the official documentation here, it may become out of date, so I recommend that you study it on the official Ansible website.

To build an image based on the newly created docker-image-playbook.yaml file, enter the command ansible-playbook docker-image-playbook.yaml in the console

In the repository you will find more files from two html site templates. They are added so that when you run the Docker container through the Dockerfile and through the docker-image-playbook.yaml file, you can see the difference (the templates are different from each other).

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *