How to Stop All Docker Containers Using Ansible

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

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

$ cd docker1/

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:

$ nano hosts

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 <Ctrl> + X followed by Y and <Enter> to save the hosts file.

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

$ nano ansible.cfg

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

[docker]
vm4.nodekite.com

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the ansible.cfg file.

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

$ ansible all -u ansible -m ping

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

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

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

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 <Ctrl> + X followed by Y and <Enter> to save the install_docker_python_lib.yaml file.

Now, run the install_docker_python_lib.yaml playbook as follows:

$ ansible-playbook playbooks/install_docker_python_lib.yaml

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

Configuring Ansible for Local Docker Host Automation:

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

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:

$ whoami

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the install_docker_python_lib.yaml file.

Now, run the install_docker_python_lib.yaml playbook as follows:

$ ansible-playbook --ask-pass --ask-become-pass
playbooks/install_docker_python_lib.yaml

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 <Enter>.

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 <Enter>.

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

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

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:

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:

Then, replace <lists-of-ansible-tasks> with the tasks you want to run and save the playbook by pressing <Ctrl> + X followed by Y and <Enter>.

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 <path-to-playbook-yaml-file>

Make sure to change <path-to-playbook-yaml-file> 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 <strong>http_server1</strong> nginx

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

$ docker ps

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

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 <Ctrl> + X followed by Y and <Enter> to save the stop_container.yaml file.

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

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

$ ansible-playbook playbooks/stop_container.yaml

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

$ docker ps

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

$ docker ps --all

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

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

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

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

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

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

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

$ docker ps

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

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: ''
        state: stopped
      loop: ''

To save the stop_all_container.yaml file, press <Ctrl> + X followed by Y and <Enter>.

Here, I have defined 2 tasks.

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.

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

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

$ ansible-playbook playbooks/stop_all_container.yaml

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

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

$ docker ps

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

$ docker ps --all

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

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

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

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

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

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

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

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

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

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

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 <Ctrl> + X followed by Y and <Enter> to save the stop_all_image_containers.yaml file.

Here, I have added 2 tasks.

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.

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.

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.

Now, run the stop_all_image_containers.yaml playbook as follows:

$ ansible-playbook playbooks/stop_all_image_containers.yaml

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

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.

$ docker ps

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

$ docker ps --all

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

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



from Linux Hint https://ift.tt/31xccQh

Post a Comment

0 Comments