How to start the Web Server in Docker Container using Ansible Playbook?
- 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:-
- name: Create Yum Repo
description: "this is a docker repo"
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
state: present- name: Check Service
enabled: yes- name: Install Docker SDK
command: "pip3 install docker-py"- name: Pull IMG From DockerHub
- name: Run Container
- "8080:80"- name: Initiate HTTPD on Docker
command: "systemctl start httpd.service"- name: Get Container Details
register: result- name: Copy File To TN
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:-
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:-
Finally, run the following command to see the content inside the HTML file:-
Thanks for reading :)