Automate Docker with Ansible
Ansible is an open source automation tool that simplifies IT operations by automating tasks such as configuration management, application deployment and configuration. The combination of Docker and Ansible provides the power to automate containerized environments, enabling efficient, repeatable, and scalable workflows.
Why use Ansible for Docker automation?
-
Declarative configuration:
Ansible lets you define Docker containers and configurations declaratively, ensuring consistency across environments.
-
agentless architecture:
Ansible uses SSH to communicate without requiring an agent on the target machine, making it lightweight and easy to set up.
-
Extensive Docker support:
Ansipur’s
docker
Modules allow you to seamlessly manage Docker images, containers, volumes, and networks. -
Integrate with DevOps workflows:
Ansible integrates with orchestration tools such as CI/CD pipelines and Kubernetes for end-to-end automation.
-
Idempotence:
Ansible ensures that tasks are only executed when needed, avoiding duplication and maintaining the desired state.
Setting up Ansible for Docker automation
- Install Ansible:
sudo apt update
sudo apt install ansible -y
-
Install Docker on the target host:
Make sure Docker is installed and running on all target computers. -
Set up Ansible manifest:
Define target hostinventory
document:
[docker_hosts]
192.168.1.100
192.168.1.101
-
Enable Docker module:Install
docker-py
ordocker
Python SDK required for Ansible to manage Docker:
pip install docker
Ansible manual for Docker
1. Pull the Docker image
- name: Pull Docker Image
hosts: docker_hosts
tasks:
- name: Pull nginx image
community.docker.docker_image:
name: nginx
tag: latest
2. Run the Docker container
- name: Run Docker Container
hosts: docker_hosts
tasks:
- name: Start nginx container
community.docker.docker_container:
name: nginx_server
image: nginx
ports:
- "80:80"
3. Set up Docker network
- name: Create Docker Network
hosts: docker_hosts
tasks:
- name: Create network
community.docker.docker_network:
name: my_custom_network
4. Manage volumes
- name: Create Docker Volume
hosts: docker_hosts
tasks:
- name: Create volume for persistent data
community.docker.docker_volume:
name: my_volume
5. Stop and remove the container
- name: Stop and Remove Container
hosts: docker_hosts
tasks:
- name: Stop container
community.docker.docker_container:
name: nginx_server
state: stopped
- name: Remove container
community.docker.docker_container:
name: nginx_server
state: absent
End-to-End Example: Deploying a Web Application
- Directory structure:
├── playbook.yml
├── templates
│ └── app.Dockerfile
└── files
└── index.html
-
Ansible Manual (
playbook.yml
):
- name: Deploy Web App
hosts: docker_hosts
tasks:
- name: Create Dockerfile
copy:
src: templates/app.Dockerfile
dest: /tmp/Dockerfile
- name: Copy HTML file
copy:
src: files/index.html
dest: /tmp/index.html
- name: Build Docker image
community.docker.docker_image:
name: custom_web_app
path: /tmp
- name: Run Docker container
community.docker.docker_container:
name: web_app
image: custom_web_app
ports:
- "8080:80"
-
Dockerfile template (
templates/app.Dockerfile
):
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
-
HTML file (
files/index.html
):
Welcome
Hello, World! This is a custom web app.
best practices
-
Use variables:
Define reusable variables for Docker image name, container name, and port in separate containersvars.yml
document. -
Role-based automation:
Organize tasks into Ansible roles for modularization and reuse. -
Error handling:
Handlers are included to manage failures gracefully, such as restarting services. -
Record:
Enable verbose logging for troubleshooting:
ansible-playbook playbook.yml -vvv
- version locked: Specify the version of the Docker image to avoid unexpected updates disrupting the deployment.
Use cases
-
Automated container deployment:
Deploy multiple containers across servers with minimal manual intervention. -
Cluster settings:
Automatically create Docker Swarm or Kubernetes cluster. -
Continuous Integration/Continuous Delivery Integration:
Use Ansible to deploy Dockerized applications as part of your CI/CD pipeline. -
disaster recovery:
Automatically back up and restore Docker volumes and configurations.
in conclusion
Ansible makes Docker management simpler, scalable, and efficient by automating repetitive tasks and ensuring consistency across environments. Whether you’re deploying a single container or managing a complex microservices architecture, Ansible and Docker provide powerful automation solutions.