Configure Webserver on docker container using Ansible

🔹 Configure Docker

🔹 Start and enable Docker services

🔹 Pull the httpd server image from the Docker Hub

🔹 Run the docker container and expose it to the public

🔹 Copy the html code in /var/www/html directory and start the web server


For this task, we should have atleast two Virtual Machines, one as a Controller Node and the other as a Manage Node. I am using RHEL8 for this task. Now, in the controller node, install ansible via pip (pip3 install ansible). Here, we need to create a inventory having list of IP address of the manage nodes. We also need to give username and password for doing SSH and authentication. In the global configuration file /etc/ansible/ansible.cfg file, we need to write the path to Inventory so that ansible knows its nodes. While running playbook, it will need SSH permissions, so add host_key_checking=False in the ansible global configuration file. If want to disable command warning you can use command_warning=False in the same file above.

Create a workspace and create a .yml file inside it. This yml file will be our ansible playbook to configure webserver on top of docker.

Ansible code

- name: Configuring Webserver using docker
— name: Adding docker repository to yum configuration file
name: “Docker”
description: “Docker Repo”
baseurl: “
gpgcheck: “no”

- name: Installing docker package
name: “docker-ce-18.09.1–3.el7.x86_64”
state: present

- name: Starting docker service
name: “docker”
state: started
enabled: yes

- name: Installing Python36
name: “python36”
state: present

- name: Installing docker for python3
command: pip3 install docker

- name: Creating our WorkSpace for Manage Node
path: “/root/docker-web”
state: directory

- name: Copying html file to the workspace created
src: “index.html”
dest: “/root/docker-web”

- name: Pulling httpd image and creating container and exposing ports
name: “MyDockerWeb”
image: “httpd”
state: started
— “80”
— “8080:80”
— /root/docker-web:/usr/local/apache2/htdocs/

Please go through below image for the explaination of the code:

I have also created a index.html file in the same directory, which I need to /root/docker-web directory in ansible code.

Now, that we have our code ready, to run the playbook write command ansible-playbook <filename>.yml

If the code runs fine, you should get similar output on the screen.

To check our website has been hosted or not we can use curl. Before that we make sure our container has been booted up and we need IP address of the container created. You can do docker inspect <containerName> to get the IP address and docker ps to get the current running containers on your manage node.

You can see the output of index.html file has come up. Hence our webpage has been successfully hosted on docker container using Ansible.

Feel free to get connected with me and to drop any suggestion. Thank You!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How Does React Native Manage to Stay in Trend?

How we preview websites to clients

Super Simple Services With Nanobox

Chapter 9 Extension Through Delegation

Create a “Rollup” of Rollup in Notion

Working with Modu les and Packages

Gitcoin Product Update (March 2019)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tirth Patel

Tirth Patel

More from Medium

Installing Apache Web Server on CentOS8.

Service Accounts and Roles

Service Accounts and Roles

Redis Cluster in Docker

Google Cloud Build + Google App Engine: How to add environmental variables and use in Python