How to Install Ansible using Docker Compose Ubuntu 20.04 (Container)

How to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04. In this guide we will introduce Ansible with it’s main advantages then move on to installation with Docker Compose.

Ansible is a free, open source and on of the most popular automation tool used by system administrators and developers to configure and manage multiple servers from a central place. It helps you to build your entire infrastructure in minimal time. Installing Ansible inside a container will helps you to easily use your Ansible environment wherever you need it. You can build your Ansible container image and copy it across environments as needed.

Next in this blog tutorial about how to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04 is to introduce Ansible. 

What Is Ansible?

Individual associated with IT knows how significant Ansible is to them. It is an open source automation tool or platform that is used for fulfilling complex IT tasks, like configuration management, application deployment, intra service orchestration, and provisioning. It is widely used for multi tier deployments and its models within the IT infrastructure in one deployment. Therefore it eliminates the hassle of handling them separately.

So, with the help of Ansible, you can get a desired state of infrastructure and crush the complexity of infrastructure scalability. It also helps in automating the entire process of infrastructure provisioning and configuration. It is usually done by handling it with the help of the code.

Ansible Pros

Using Ansible, you can handle any scale of infrastructure, even if it is as small as defining a package that needs to be installed on a system and as large as setting up a multi tier orchestration.

Moreover, Ansible becomes really significant when the configuration of the server’s manual becomes a very difficult task. At this point, it helps you with:

  • Managing Configuration – Ansible manages configuration by providing control at a granular level. It provides consistency in configuration management.
  • Deploying ApplicationAnsible manages applications from development to production quite straightforwardly with just a few changes.
  • Orchestration – Ansible teaches you the process of configuring infra in the entire server.
  • Security And Compliance – With the assistance of Ansible, you can also deploy a security policy. It provides you with the server’s best practices.

Advantages Of Ansible

Ansible provides you with the following advantages:

Simple To Learn

Due to its simplicity, Ansible is considered the best tool for beginners as well. Its easy to learn features enable you to learn using Ansible fast. It also helps  in increasing productivity.

Written In Python

Since Ansible is written in Python, getting it up and using it becomes effortless in it. Also, Python is a language that is common for administration and scripting tasks. Therefore, engineers and system administrations are familiar with Python as opposed to another language, like Ruby.

Do Not Depend On Agents

Ansible’s agentless nature enables it to manage all the master agent communication via Standard SSH or Paramiko module. Since the Paramiko module is a Python implementation of SSH2, it is very crucial for managing nodes. Also, it restricts Ansible from using any forms of agents installed on the remote system to ensure management. This way, it reduces maintenance overheads and performance degradations by vast margins.

YAMP Based Playbooks

Playbooks, one of the Ansible configuration files is written in the YAML language. It makes it a perfect fit for configuration management and automation purposes as compared to other formats like JSON. This format is effortless to read, supports comments and employs the use of anchors for referencing other items.

Ansible Galaxy

Ansible Galaxy is a portal that acts as a central repository for locating, reusing and sharing content related to Ansible. For instance, downloading reusable Roles for installing applications and server configuration makes them ideal for use in a particular user’s playbooks. It can contribute substantially to an increase in deployment speed.

Next in this post, we will show you how to install Ansible using Docker Compose on Ubuntu 20.04.

Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04


  • Minimum 4 GB of RAM and 3.4 GHz CPU with 2 Cores
  • A root password is setup on your server.

Install Docker CE

Before starting, you will need to install Docker CE on your server. By default, the latest version of Docker is not included in the Ubuntu default repository. So you will need to install it from the Docker’s official repository.

First, install all required dependencies using the following command:

					apt install apt-transport-https ca-certificates curl software-properties-common -y

Once all the required dependencies are installed, download and add the Docker GPG key with the following command:

					curl -fsSL | apt-key add -

Next, add the Docker repository to APT:

					add-apt-repository "deb [arch=amd64] focal stable"

Once the repository is added, update the repository cache using the following command:

					apt update -y

Then, install the Docker CE by running the following command:

					apt install docker-ce -y

After the successful installation, verify Docker version using the following command:

					docker --version

The output would look like this:

					Docker version 20.10.17, build 100c701

Also see the Docker information with the following command:

					docker info

You will get the following output:

 Context:    default
 Debug Mode: false
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  scan: Docker Scan (Docker Inc., v0.17.0)

 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog


Install Docker Compose

Next, you will also need to install the Docker Compose to your server. At the time of writing this tutorial, the latest version of Docker Compose is v2.6.1. You can download it with the following command:

					curl -L "" -o /usr/local/bin/docker-compose

Next thing is to set the execution permission on the downloaded binary file with the following command:

					chmod +x /usr/local/bin/docker-compose

Here, verify the Docker Compose version using the following command:

					docker-compose --version

You should see the following output:

					Docker Compose version v2.6.1

Create a Dockerfile

At this stage you will need to create two Dockerfile on your system. One is for the Ansible container and the other is for another Ubuntu containers.

Firstly, create a directory to hold all configuration files:

					mkdir ansible
mkdir ansible/ssh-enabled-ubuntu

Secondly, change the directory to ansible and create a Dockerfile for Ansible:

					cd ansible
nano Dockerfile

Add the following lines:

					FROM ubuntu

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y gcc python2-dev libkrb5-dev && \
    apt-get install python3-pip -y && \
    pip3 install --upgrade pip && \
    pip3 install --upgrade virtualenv && \
    pip3 install pywinrm && \
    pip3 install kerberos && \
    apt install krb5-user -y && \
    pip3 install ansible && \
    apt install openssh-server openssh-client -y && \
    apt install sshpass -y

Save and close the file then create another Dockerfile for the Ubuntu container:

					nano ssh-enabled-ubuntu/Dockerfile

Again here add the following lines:

					FROM ubuntu:16.04
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'rootpassword' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s* optional' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
CMD ["/usr/sbin/sshd", "-D"]


Save and close the file when you are finished.

Create a Docker-Compose File to Launch Ansible Container

Now, you will need to create a Docker Compose file to launch both container. You can create it inside the ansible directory:

					nano ansible/docker-compose.yml

Add the following configurations:

					version: '3'
        container_name: ansible
        image: ansible
        tty: true
        stdin_open: true
          context: ./

        container_name: remote-host-one
        image: remote-host-ssh
          context: ./ssh-enabled-ubuntu



Save and close the file when you are finished.

Launch Ansible Container

At this point, all the configuration files are ready to launch the Ansible container. You can now change the directory to ansible and run the following command to launch the Ansible container:

					cd ansible
docker-compose up -d

This command will download Ubuntu images from the Docker hub, install Ansible inside the Ubuntu container and launch both container as shown below:

					[+] Building 0.2s (6/6) FINISHED                                                                                                              
 => [internal] load build definition from Dockerfile                                                                                     0.0s
 => => transferring dockerfile: 493B                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                        0.0s
 => => transferring context: 2B                                                                                                          0.0s
 => [internal] load metadata for                                                                         0.1s
 => [1/2] FROM                          0.0s
 => CACHED [2/2] RUN apt-get update &&     apt-get install -y gcc python2-dev libkrb5-dev &&     apt-get install python3-pip -y &&       0.0s
 => exporting to image                                                                                                                   0.0s
 => => exporting layers                                                                                                                  0.0s
 => => writing image sha256:e51c01029a4269ae4d8dbf11721f3fe96c43325eaaa9ed2d1ebe8d2dbbb2257e                                             0.0s
 => => naming to                                                                                               0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
 ⠿ Container remote-host-one  Created                                                                                                    0.1s
 ⠿ Container ansible          Created                                                                                                    0.1s
Attaching to ansible, remote-host-one


You can now verify the downloaded images using the following command:

					docker images

This results in the following output:

					REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
ansible           latest    e51c01029a42   12 minutes ago   978MB
remote-host-ssh   latest    8e66e11df038   28 minutes ago   220MB


Verify the running containers using the following command:

					docker ps

You will get the following output:

					CONTAINER ID   IMAGE             COMMAND               CREATED         STATUS         PORTS     NAMES
b8f87b8b4cdf   remote-host-ssh   "/usr/sbin/sshd -D"   3 minutes ago   Up 3 minutes   22/tcp    remote-host-one
324f1c88f928   ansible           "bash"                3 minutes ago   Up 3 minutes             ansible

Connect to the Ansible Container

You can now connect to Ansible container using the following command:

					docker exec -it 324f1c88f928 bash

Once you are connected to the Ansible container, you will get the following shell:


Verify the Ansible installed version using the following command:

					root@324f1c88f928:/# ansible --version

You should see the Ansible version in the following output:

					ansible [core 2.13.1]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.10/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.4 (main, Apr  2 2022, 09:04:19) [GCC 11.2.0]
  jinja version = 3.1.2
  libyaml = True

If you want to connect to another Ubuntu container, run the SSH command:

					ssh root@remote-host-one

Provide your root password which you have defined in the Dockerfile to connect to the container. Once you are connected, you will get the following output:

					The authenticity of host 'remote-host-one (' can't be established.
ED25519 key fingerprint is SHA256:oIt+LV7hy0Xpb/jslLYQzX4fIufhM9Wc9EiJx2IOjns.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'remote-host-one' (ED25519) to the list of known hosts.
root@remote-host-one's password: 
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 5.4.0-121-generic x86_64)

 * Documentation:
 * Management:
 * Support:

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


Great Effort! We have learned how to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04.

How to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04. Conclusion

In this guide, we explained how to install Ansible using Docker Compose in Ubuntu 20.04. You can now create an inventory file to define remote nodes that you want to manage, create a playbook file to define your task. In summary you can run the Ansible playbook to execute all tasks on the remote nodes.

I hope you can now easily deploy Ansible in the Docker environment. You can also copy the Ansible container to other hosts and use it to manage your infrastructure.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x