Build & Push Ansible Docker Image in DockerHub

Introduction

Ansible – The purpose of configuration management systems is to make it easier for administrators and operations teams to manage a large number of servers. They let you automate control of many different systems from a single location.

Docker image is a set of command instructions for getting an application up and running, and we can create our own custom Docker image and push it to the DockerHub registry. 

Prerequisites

  • Basic knowledge of Docker.
  • Basic knowledge of Linux commands.

In this post, We will show you how to build and use Ansible docker image and container.

Step 1: Create a Sample Dockerfile

We need to create a sample dockerfile like Apache webserver that will use in build and push docker image.

To create a directory.

mkdir ansible && cd ansible 

To create Dockerfile.

nano Dockerfile

Paste the following command in order to build the docker image.

FROM ubuntu:22.04
RUN apt-get update && \
  apt-get install python3-pip -y && \
  pip3 install --upgrade pip && \
  pip3 install --upgrade virtualenv && \
  pip3 install pywinrm && \
  pip3 install ansible

To build the ansible’s custom docker image, We need to use following command to build docker image.

To build docker image.

docker build -t aftab70/custom_ansible:1.0.0 .

I am using aftab70/custom_ansible DockerHub repository in order to where aftab70 would be account name and custom_ansible would be repository name with tags.

Step 2: Verify Docker Image

We need to validate the custom ansible docker image by following the given command.

To view the docker images.

sudo docker images

You should get output like this.

Test the custom ansible image.

sudo docker run -it aftab70/custom_ansible:1.0.0

Step 3: Creating DockerHub Repository

We must first ensure that our Dockerhub account and repository are both available; if not, we can create them by following the instructions. 

To login or create Dockerhub account.

URL – https://login.docker.com/

After logged in Dockerhub account So then you need to click on Create repository button.

When the Dockerhub repository is successfully created, the output should look like this. 

Step 4: Pushing Image to DockerHub

We are good to push the custom ansible docker image by following command.

To login DockerHub using CLI.

docker login

You will be asked for username and password, You need to enter your cred and after that you should get Login Succeeded message on your terminal.

To push docker image.

docker push aftab70/custom_ansible:1.0.0

You should get output like this.

Note: If you have a Docker image without a Dockerhub account name, then you need to tag your first image using the given command.

docker tag custom_image:tag_here account_name/custom_image_here:tag_here

Lets verify the Pushed Docker image in Dockerhub account.

Conclusion

We have successfully build and pushed Ansible docker image to DockerHub using command-line on ubuntu 22.04 LTS, If you still have questions, please post them in the comments section below.

Author

Build & Push Ansible Docker Image in DockerHub

Leave a Reply

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

Scroll to top