Configure Apache Server with Ansible hosted on Docker Containers
If you are taking your steps into the world of DevOps and want to practice ansible through docker, this blog will guide you step-by-step, showing the path to use these tools. Here, I’ll walk you through the process of creating Docker containers for both your Ansible control host and nodes. Our destination? Deploying a simple web application on an Apache server using Ansible playbooks. Let’s begin.
Prerequisites:
- Git
- Docker
- VS Code
Steps:
Step 1: Open the terminal and run this command in the home directory:
mkdir ansible_vol && cd ansible_vol
mkdir ubuntu-c && cd ubuntu-c
For this project, we have used bind mount to mount source code into the container in the Docker Compose Configuration file.
Step 2: Clone GitHub repository and open it in Visual Studio Code:
git clone https://github.com/dhanbdrkarki1/simple-web-ansible-docker.git
cd simple-web-ansible-docker
code .
Step 4: Now, explore the files inside the cloned folder.
In the docker-compose.yml file, you can find three containers configuration. In the control-host, ansible is installed and it will be used for running the playbook whereas other two container node1 and node2 is where application will be deployed.
Step 5: Open terminal using Ctrl + Alt + ` or Terminal > New Terminal. Then, run this command to start the services defined in a docker compose config file.
docker-compose up - build
Step 6: Wait until you see your CLI like this:
Step 7: Again open new terminal and to see list of currently running Docker containers and then to enter to an interactive shell of our control host i.e., ubuntu-c here, use this command.
docker ps
docker exec -it ubuntu-c bash
After entering into a container shell, run:
ls && cd simple-web-ansible-docker
ls
Step 8: Before running playbook, let’s check the reachability of the hosts in the webservers group defined in hosts file using ansible ping module. Then, we are ready to run our ansible playbook.
ansible webserver -m ping
ansible-playbook site.yml
We have exposed port 80 from both of the service container and mapped it to port 8003 and 8004 respectively in docker compose file. Now, lets open browser and search http://localhost:8003/.
Also explore http://localhost:8004/.
Step 9: After successfully deploying a simple web using ansible, you can destroy container using:
./destroy-container.sh
Follow me :
Linkedin: https://www.linkedin.com/in/dhanbdrkarki/
GitHub: https://github.com/dhanbdrkarki1/
Please feel free to contact me on LinkedIn if you are unsure about anything or are having trouble understanding anything about the project.
Thank You!!!