Automating Docker with Ansible

Docker is now a very common and ubiquitous tool. In production, it is often managed by an orchestrator (or at least it should be, in the majority of cases), but in development, environments are often used directly.

With Ansible, you can easily manage your Docker instance.

Since we are going to manage a Docker instance, we need to make sure we have one at hand and that the docker command on our machine is configured properly. We need to do this to ensure this is enough to run docker images on the Terminal. Let's say you get a result similar to the following:

REPOSITORY TAG IMAGE ID CREATED SIZE

This means that everything is working properly. More lines may be provided as output if you have already-cloned images.

On the other hand, let's say it returns something like this:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

This means that we don't have a Docker daemon running or that our Docker console has been configured incorrectly.

Also, it's important to ensure that you have the docker Python module since Ansible will try to use it to communicate with the Docker daemon. Let's take a look:

  1. First of all, we need to create a playbook called start-docker-container.yaml that will contain the following code:
---
- hosts: localhost
tasks:
- name: Start a container with a command
docker_container:
name: test-container
image: alpine
command:
- echo
- "Hello, World!"
  1. Now that we have the Ansible playbook, we just need to execute it:
$ ansible-playbook start-docker-container.yaml

As you may expect, it will give you an output similar to the following:

PLAY [localhost] *********************************************************************

TASK [Gathering Facts] ***************************************************************
ok: [localhost]

TASK [Start a container with a command] **********************************************
changed: [localhost]

PLAY RECAP ***************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  1. We can now check that our command executed properly, as follows:
$ docker container list -a

This will show the container that was run:

CONTAINER ID IMAGE  COMMAND              CREATED       STATUS                        PORTS NAMES
c706ec55fc0d alpine "echo Hello, World!" 3 minutes ago Exited (0) About a minute ago test-container

This proves that a container was executed.

To check that the echo command was executed, we can run the following code:

$ docker logs c706ec55fc0d

This will return the following output:

Hello, World!

In this section, we executed the docker_container module. This is not the only module Ansible has to control the Docker daemon, but it is probably one of the most widely used since it's used to control containers running on Docker.

Other modules include the following:

  • docker_config: Used to change the configurations of the Docker daemon
  • docker_container_info: Used to gather information from (inspect) a container
  • docker_network: Used to manage Docker networking configuration

There are also many modules that start with docker_ but are actually used to manage Docker Swarm clusters, and not Docker instances. Some examples are as follows:

  • docker_node: Used to manage a node in a Docker Swarm cluster
  • docker_node_info: Used to retrieve information about a specific node in a Docker Swarm cluster
  • docker_swarm_info: Used to retrieve information about a Docker Swarm cluster

As we will see in the next section, there many more modules that can be used to manage containers that are orchestrated in various ways.

Now that you have learned how to automate Docker with Ansible, you will explore container-focused modules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.37.68