How to start the Web Server in Docker Container using Ansible Playbook?

Description:-

  • 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.

The ansible playbook for the same is as follows:-

hosts: all
tasks:
- name: Create Yum Repo
yum_repository:
name: "docker"
description: "this is a docker repo"
reposdir: "/etc/yum.repos.d/"
baseurl: "
https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck : "no"
enabled: yes- name: Run Yum RepoList
command: yum repolist- name: Run Docker CMD
command: yum install -y --nobest docker-ce- name: Check Package
package:
name: "docker-ce"
state: present- name: Check Service
service:
name: docker
state: started

enabled: yes- name: Install Docker SDK
command: "pip3 install docker-py"- name: Pull IMG From DockerHub

docker_image:
name: "vimal13/apache-webserver-php:v1"
source: pull

- name: Run Container
docker_container:
name: "container1"
image: "vimal13/apache-webserver-php:v1"
state: started
detach: yes
exposed_ports:
- 80
ports:
- "8080:80"- name: Initiate HTTPD on Docker
command: "systemctl start httpd.service"- name: Get Container Details
docker_container_info:
name: "container1"
register: result- name: Copy File To TN
copy:
src: "demo1.html"
dest: "/root"- name: Copy File To Docker Container
command: "docker cp /root/demo1.html container1:/var/www/html"

Now, run the playbook using the following command:-

ansible-playbook 'playbook_name.yml'

We can also check whether these services are configured in the target node or not. The command for the same is as follows:-

systemctl status docker

To run the container created using playbook, the following command is executed (here the name of my container is “container1”).

docker exec -it container1 bash

Next, find the IP address of the container using the following command:-

ifconfig

Finally, run the following command to see the content inside the HTML file:-

curl 172.17.0.2/demo1.html

Thanks for reading :)

--

--

--

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

The real OG…Open Graph tags!

Today I Learnt — How to inject multiple instances of an interface in a service

_*Task Description - AWS*_ 👨🏻‍💻

Native integration between Apache Spark and Volcano Kubernetes scheduler

5 Types of Documentation

Deibiz — LTFFT_1

Mozilla Firefox 11 Download For Windows Xp

Hands-on AWS Project Ideas for Beginners

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
Ananya Sharma

Ananya Sharma

More from Medium

Gitlab SSH Config Host File, Allowing Multiple Account SSH Access

Dockerfile COPY cannot find the file

Install Gitlab runner on ubuntu work with dind

Deploy Root Certificates to Debian-based Linux systems with Ansible