Managing Docker Containers with Ansible: Start, Stop, and Run Containers Automatically

Ayushmaan Srivastav
4 min readOct 26, 2024

--

Ansible provides a simple, flexible way to manage Docker containers, making it easy to automate container operations such as starting, stopping, and running containers across your infrastructure. This blog will walk you through how to use Ansible to automate Docker container management.

Why Use Ansible to Manage Docker?

Ansible allows you to:

  • Automate Docker container operations on multiple hosts.
  • Keep container configurations consistent.
  • Easily integrate Docker operations into larger automation workflows.

Prerequisites

Before you begin, ensure the following:

  1. Docker installed on your system.
  2. Ansible installed: Install Ansible using pip install ansible.
  3. Ansible Docker modules: Install docker-py and Docker SDK for Python
pip install docker

4. Ansible configuration: Make sure your inventory is configured with the hosts where Docker is installed.

Step-by-Step Guide to Manage Docker with Ansible

1. Set Up Inventory File

Create an inventory file to define the target hosts where Docker operations will be performed.

Example hosts file:

[docker_hosts]
localhost ansible_connection=local

2. Ansible Playbook for Docker Management

Now, create an Ansible playbook to manage Docker containers. In this example, we’ll write a playbook that:

  • Starts a container.
  • Stops a container.
  • Runs a new container.

Create a file called docker-management.yml:

---
- name: Manage Docker Containers
hosts: docker_hosts
tasks:

- name: Start a Docker container
docker_container:
name: my_nginx_container
image: nginx
state: started
restart_policy: always
ports:
- "80:80"

- name: Stop a Docker container
docker_container:
name: my_nginx_container
state: stopped

- name: Remove a Docker container
docker_container:
name: my_nginx_container
state: absent

- name: Run a new Docker container
docker_container:
name: my_new_container
image: alpine
state: started
command: "echo Hello from Ansible!"

Explaining the Playbook:

  1. Starting a Container: The first task uses the docker_container module to start a container named my_nginx_container using the nginx image. The container will be started with port 80 mapped to the host and will always restart if stopped.
- name: Start a Docker container
docker_container:
name: my_nginx_container
image: nginx
state: started
restart_policy: always
ports:
- "80:80"

2. Stopping a Container: The second task stops the container named my_nginx_container if it’s running.

- name: Stop a Docker container
docker_container:
name: my_nginx_container
state: stopped

3. Removing a Container: The third task removes the my_nginx_container container if it exists. This is useful for cleaning up containers that are no longer needed.

- name: Remove a Docker container
docker_container:
name: my_nginx_container
state: absent

4. Running a New Container: The final task runs a new container named my_new_container from the alpine image, executing the command echo Hello from Ansible!.

- name: Run a new Docker container
docker_container:
name: my_new_container
image: alpine
state: started
command: "echo Hello from Ansible!"

3. Run the Playbook

Once the playbook is ready, you can execute it using the following command:

ansible-playbook -i hosts docker-management.yml

Ansible will connect to the Docker host and perform the specified operations (starting, stopping, removing, or running containers).

Extending Docker Management with Ansible

Here are some additional operations you can perform with Ansible and Docker:

Pulling Docker Images

You can pull Docker images from Docker Hub before running them:

- name: Pull latest nginx image
docker_image:
name: nginx
source: pull

Running Multiple Containers

To start multiple containers, you can define a list of containers in your playbook:

- name: Run multiple containers
docker_container:
name: "{{ item.name }}"
image: "{{ item.image }}"
state: started
with_items:
- { name: "nginx_container", image: "nginx" }
- { name: "redis_container", image: "redis" }

Logging and Monitoring Containers

Ansible can also be used to monitor logs or resource usage of Docker containers:

- name: Monitor Docker logs
shell: docker logs my_nginx_container

Building Docker Images

If you need to build Docker images from a Dockerfile, you can use Ansible to automate the process:

- name: Build Docker image
docker_image:
path: /path/to/Dockerfile
name: my_custom_image

Benefits of Using Ansible to Manage Docker Containers

  1. Consistency: Ensures that Docker containers are managed consistently across multiple environments.
  2. Scalability: Ansible’s inventory management allows you to scale Docker container management across multiple hosts.
  3. Automation: Reduces manual effort by automating container operations such as starting, stopping, and running containers.
  4. Ease of Use: Ansible’s simple YAML syntax makes it easy to write and maintain playbooks.
  5. Integration: Ansible can integrate Docker management into larger CI/CD or infrastructure automation workflows.

Conclusion

Using Ansible to manage Docker containers can greatly simplify the process of container lifecycle management. With a few lines of YAML, you can start, stop, run, and remove containers across multiple Docker hosts. Whether you’re managing a small set of containers or a large-scale containerized infrastructure, Ansible provides a powerful and flexible toolset for automation.

--

--

No responses yet