Configuring Docker, and configuring a webserver on the Docker container using Ansible

Neha sonone
5 min readNov 30, 2020

--

The Task is to:-

  • configure a 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 webserver.

Let's start How?:-

the first step is to have a docker repo in the managed node/target node .let me see first in the target whether I have a docker repo or not.

there is no repo in the target node so in the controller node we have to write an ansible playbook to create a docker repository.

to create an ansible playbook u can follow the below command

vim docker.yml

write the below code in the playbook to create a docker repo.

— Now run the playbook by the command.

#ansible-playbook  -v docker.yml     (v->verbosity to see more info)

Now see in the target node whether the repo created or not.

To install the Docker software in the target node, so that we can start using the service write the code in the playbook.

— Now run the playbook.

Now see in the target node whether the docker-ce software installed or not.

we have successfully installed the docker-ce software in the target node after that we have to start the service in the target node so that we can start using the docker, first check the status of the docker in the target node by the below command.

for starting the service in the target node write the below code in docker.yml

now run the ansible playbook.

now in the target node check whether the service started or not by the below command.

the service is started in the target node...

After this, we have to check-in the target node about the docker images exits, if not then we have to pull the image from the docker hub https://hub.docker.com/ Docker Hub is a hosted repository service provided by Docker for finding and sharing container images with your team.

as there is no image, so here I'm pulling the httpd image from docker hub by writing the below code in docker.yml

now run the ansible playbook.

now see in the target node one httpd image come up.

now we have to create one webpage inside the controller node , locally.

vim index.html

and here I am copying the local webpage created in the /home, we have write the code for it.

now run the playbook

now in the target node see whether the index.html file is copied or not by the below command

now we have to run docker container with an httpd image in the target node, first, check where there is any os running or launched before by the below command.

writing the code for it in the ansible playbook

now run the playbook

now see whether the os is running or not by the below command

to go inside the docker container follow the below command

successfully launched a docker container with an httpd image, and we have copied the index.html file in it

Thank you for reading :))

--

--

Neha sonone
Neha sonone

No responses yet