Ansible has many modules for automating your Docker host, containers, volumes, networks, etc. If you want to stop your running Docker containers, you can do that as well.

In this article, I am going to show you how to stop a specific Docker container. I am also going to show you how to stop all the running Docker containers, or all the Docker containers running a specific Docker image using Ansible. So, let’s get started.

Prerequisites:

If you want to try out the examples of this article,

1) You must have Ansible installed on your computer.


2) You must have Docker installed on your computer or a remote Ubuntu host.


3) You must have Python Docker library installed on your Docker host.


4) You must configure the Docker host for Ansible automation.

NOTE: Docker host is the computer where you have Docker installed.

There are many articles on LinuxHint dedicated to Installing Ansible and configuring hosts for Ansible automation and installing Docker. You may check them out if needed.

Creating a Project Directory:

To keep all the files and directory we will be creating in this article organized, it’s a good idea to create a project directory.

To create a project directory docker1/ in your current working directory, run the following command:

$ mkdir -pv docker1/playbooks

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, navigate to the docker1/ project directory as follows:

How to Stop All Docker Containers Using Ansible Ansible Docker

Configuring Ansible for Remote Docker Host Automation:

If you have Docker installed on a remote Ubuntu host which you want to automate using Ansible, then this section is for you.

First, create an Ansible inventory file hosts with the following command:

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, add the IP address or DNS name of your Docker host in the hosts’ inventory file as follows.

[docker]


vm4.nodekite.com

In my case, the DNS name of my Ubuntu Docker host is vm4.nodekite.com. It will be different for you. So, make sure to replace it as necessary.

Once you’re done, press X followed by Y and to save the hosts file.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, create an Ansible configuration file ansible.cfg as follows:

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, type in the following lines in the ansible.cfg configuration file.

[docker]


vm4.nodekite.com

Once you’re done, press X followed by Y and to save the ansible.cfg file.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, check whether you can ping the remote Docker host from your computer with the following command:

$ ansible all -u ansible -m ping

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, I can ping my remote Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, I have Docker 19.03.11 installed on my remote Ubuntu Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, you must have Python Docker library installed on your remote Docker host for Ansible docker modules to work. In this article, I am using Ubuntu. So, this is what I will cover.

To install Python Docker library on your remote Docker host (Ubuntu), create a new Ansible playbook install_docker_python_lib.yaml in the playbooks/ directory as follows:

$ nano playbooks/install_docker_python_lib.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, type in the following lines in the install_docker_python_lib.yaml file.

– hosts: docker


  user
: ansible


  become
: True


  tasks
:


    – name
: Ensure python3-docker package is installed


      apt
:


        name
: python3-docker


        state
: present


        update_cache
: True

Once you’re done, press X followed by Y and to save the install_docker_python_lib.yaml file.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, run the install_docker_python_lib.yaml playbook as follows:

$ ansible-playbook playbooks/install_docker_python_lib.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

The playbook should run successfully and it will install the Python Docker library on your remote Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

If you have Docker installed on the host where you have Ansible installed and you want to automate it using Ansible, then this section is for you.

First, create a new Ansible playbook install_docker_python_lib.yaml in the playbooks/ directory as follows:

$ nano playbooks/install_docker_python_lib.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, type in the following lines in the install_docker_python_lib.yaml file.

– hosts: localhost


  connection
: local


  user
: shovon


  become
: True


  tasks
:


    – name
: Ensure python3-docker package is installed


      apt
:


        name
: python3-docker


        state
: present


        update_cache
: True

Here, the line user: shovon sets shovon as the user who will be executing the tasks. It will be different for you. So, make sure to change it to your login username.

You can find the login username of your Docker host with the following command:

How to Stop All Docker Containers Using Ansible Ansible Docker

Once you’re done, press X followed by Y and to save the install_docker_python_lib.yaml file.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, run the install_docker_python_lib.yaml playbook as follows:

$ ansible-playbook –ask-pass –ask-become-pass


playbooks/install_docker_python_lib.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

Ansible will ask you for the login password of the user you have specified in the playbook. Type in the log in password and press .

How to Stop All Docker Containers Using Ansible Ansible Docker

Ansible will ask you for the BECOME/sudo password as well. It should be the same password as your login password. So, leave it empty and press .

How to Stop All Docker Containers Using Ansible Ansible Docker

The playbook should start running. It may take a while to complete.

How to Stop All Docker Containers Using Ansible Ansible Docker

At this point, the playbook should be successful and the Python Docker library should be installed.

How to Stop All Docker Containers Using Ansible Ansible Docker

Making Necessary Adjustment to Playbooks:

Depending on whether you’re managing Docker containers on your local Docker host or a remote Docker host, you need to adjust your playbooks accordingly.

From the next sections of this article, I will be running the example playbooks on a remote Docker host. So, all the playbooks will start with the following lines:

– hosts: docker


  user
: ansible


  tasks:

How to Stop All Docker Containers Using Ansible Ansible Docker

If you want to run the playbooks on your local Docker host, then make sure that the playbooks start with the following lines instead.

– hosts: localhost


  connection
: local


  user
: shovon


  become
: True


  tasks:

How to Stop All Docker Containers Using Ansible Ansible Docker

Then, replace with the tasks you want to run and save the playbook by pressing X followed by Y and .

You also have to run the Ansible playbooks a little bit differently if you’re going to manage a Docker host locally using Ansible.

You can run an Ansible playbook locally as follows:

$ ansible-playbook –ask-pass –ask-become-pass

How to Stop All Docker Containers Using Ansible Ansible Docker

Make sure to change to the path of your Ansible playbook YAML file.

Stopping a Docker Container:

If you have a running Docker container and you know the ID or name of the Docker container, you can easily stop that Docker container using Ansible.

In this section, I am going to show you how to do that.

First, I am going to create an Nginx (image name) Docker container http_server1 (container name) as follows:

$ docker run -p 8080:80 -d –name http_server1 nginx

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, the Docker container http_server1 is running.

How to Stop All Docker Containers Using Ansible Ansible Docker

To stop the Docker container http_server1 using Ansible, create a new playbook stop_container.yaml in the playbooks/ directory as follows:

$ nano playbooks/stop_container.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

Then, type in the following lines in the stop_container.yaml playbook.

– hosts: docker


  user
: ansible


  tasks
:


    – name
: Stop the http_server1 container


      docker_container
:


        name
: http_server1


        state
: stopped

Once you’re done, press X followed by Y and to save the stop_container.yaml file.

How to Stop All Docker Containers Using Ansible Ansible Docker

Here, the Ansible docker_container module is used to stop the Docker container http_server1.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, run the stop_container.yaml playbook with the following command:

$ ansible-playbook playbooks/stop_container.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, the Docker container http_server1 is not running anymore on my remote Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

You can list all the Docker containers (running, paused, stopped) with the following command:

As you can see, the stopped Docker container http_server1 is listed.

How to Stop All Docker Containers Using Ansible Ansible Docker

Stopping All Running Docker Containers:

If you want to stop all the Docker containers running on your Docker host using Ansible, then this section is for you.

First, I am going to create 3 Docker containers (server1, server2, and server3) so that I can stop them all using Ansible later.

To create the server1 Docker container, run the following command:

$ docker run -p 8081:80 -d –name server1 nginx

How to Stop All Docker Containers Using Ansible Ansible Docker

To create the server2 Docker container, run the following command:

$ docker run -p 8082:80 -d –name server2 nginx

How to Stop All Docker Containers Using Ansible Ansible Docker

To create the server3 Docker container, run the following command:

$ docker run -p 8083:80 -d –name server3 nginx

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, the server1, server2, and server3 Docker containers are running on my remote Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

To stop all those Docker containers, create a new playbook stop_all_container.yaml in your playbooks/ directory as follows:

$ nano playbooks/stop_all_container.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, type in the following lines in the stop_all_container.yaml playbook file.

– hosts: docker


  user: ansible


  tasks:


    – name: Get a list of all running containers


      docker_host_info:


        containers: True


      register: docker_info


    – name: Stop all running containers


      docker_container:


        name: ‘{{ item.Names[0] | regex_replace(“^/”, “”) }}’


        state: stopped


      loop: ‘{{ docker_info.containers }}’

To save the stop_all_container.yaml file, press X followed by Y and .

How to Stop All Docker Containers Using Ansible Ansible Docker

Here, I have defined 2 tasks.

How to Stop All Docker Containers Using Ansible Ansible Docker

The first task uses the Ansible docker_host_info module to get a list of all the running Docker containers and stores it in the docker_info variable.

How to Stop All Docker Containers Using Ansible Ansible Docker

The second task loops through the docker_info.containers array finds the running Docker container names and stops them one by one.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, run the stop_all_container.yaml playbook with the following command:

$ ansible-playbook playbooks/stop_all_container.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

The playbook should run successfully as you can see in the screenshot below.

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, there are no running Docker containers on my remote Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

You can list all the Docker containers (running, paused, stopped) with the following command:

As you can see, the stopped Docker containers server1, server2, and server3 are listed.

How to Stop All Docker Containers Using Ansible Ansible Docker

Stop All Docker Containers Running a Specific Image:

If you want to stop all the Docker containers running a specific Docker image, then this section is for you.

First, let’s create some dummy Docker containers.

I will create 2 Docker containers (nginx1 and nginx2) using the Nginx Docker image and 2 Docker containers (http1 and http2) using the httpd Docker image for the demonstration.

To create the nginx1 Docker container using the Nginx Docker image, run the following command:

$ docker run -d -p 8081:80 –name nginx1 nginx

How to Stop All Docker Containers Using Ansible Ansible Docker

To create the nginx2 Docker container using the Nginx Docker image, run the following command:

$ docker run -d -p 8082:80 –name nginx2 nginx

How to Stop All Docker Containers Using Ansible Ansible Docker

To create the http1 Docker container using the httpd Docker image, run the following command:

$ docker run -d -p 8091:80 –name http1 httpd

How to Stop All Docker Containers Using Ansible Ansible Docker

To create the http2 Docker container using the httpd Docker image, run the following command:

$ docker run -d -p 8092:80 –name http2 httpd

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, the nginx1, nginx2, http1, and http2 Docker containers are running on my remote Docker host.

How to Stop All Docker Containers Using Ansible Ansible Docker

To stop all the Docker containers running an specific Docker image (let’s say, nginx), create a new Docker playbook stop_all_image_containers.yaml in the playbooks/ directory as follows:

$ nano playbooks/stop_all_image_containers.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, type in the following lines in the stop_all_image_containers.yaml file.

– hosts: docker


  user
: ansible


  tasks
:


    – name
: Get a list of all running containers


      docker_host_info
:


        containers
: True


      register
: docker_info


    – name
: Stop all containers running nginx image


      docker_container
:


        name
: {{ item.Names[0] | regex_replace(“^/”, “”) }}


        state
: stopped


      when
: item.Image == ‘nginx’


      loop
: {{ docker_info.containers }}

Once you’re done, press X followed by Y and to save the stop_all_image_containers.yaml file.

How to Stop All Docker Containers Using Ansible Ansible Docker

Here, I have added 2 tasks.

How to Stop All Docker Containers Using Ansible Ansible Docker

The first task uses the Ansible docker_host_info module to get a list of all the running Docker containers and stores it in the docker_info variable.

How to Stop All Docker Containers Using Ansible Ansible Docker

The second task loops through the docker_info.containers array finds the running Docker container names and runs the docker_container module for each running container only if the container’s image name is Nginx. Then, it stops the matched Docker containers.

How to Stop All Docker Containers Using Ansible Ansible Docker

In the second task, the following line is used to check if the Docker container is running the Nginx image. You can change it to a different Docker image name if you want.

How to Stop All Docker Containers Using Ansible Ansible Docker

Now, run the stop_all_image_containers.yaml playbook as follows:

$ ansible-playbook playbooks/stop_all_image_containers.yaml

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, only the Nginx containers (nginx1 and nginx2) are modified (stopped). The httpd containers (http1 and http2) are skipped.

How to Stop All Docker Containers Using Ansible Ansible Docker

As you can see, no Nginx Docker containers are running on my remote Docker host. Only the httpd Docker containers (http1 and http2) is running.

How to Stop All Docker Containers Using Ansible Ansible Docker

You can list all the Docker containers (running, paused, stopped) with the following command:

As you can see, the stopped Docker containers nginx1 and nginx2 are listed.

How to Stop All Docker Containers Using Ansible Ansible Docker

Conclusion:

In this article, I have shown you how to stop a running Docker container, all the Docker containers running a specific Docker image, and all the running Docker containers of your Docker host. To do that, I have used the Ansible docker_container and docker_host_info modules in this article.

To learn more about these modules, visit the following Ansible official documentation pages.

[1] docker_container – Manage docker containers


[2] docker_host_info – Retrieves facts about docker host and lists of objects of the services

About the author

How to Stop All Docker Containers Using Ansible Ansible Docker

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.