Bootstrap FreeKB - Ansible - Create Docker container using the docker_container module
Ansible - Create Docker container using the docker_container module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

docker_container is part of the community.docker collection. Typically, the community.docker collection is not included in the default Ansible collections. The ansible-galaxy collection install command can be used to install the community.docker collection.

ansible-galaxy collection install community.docker

 

Before you can use the docker_container module, the Python docker module must be installed on the managed node, which can be done using PIP. The dnf module can be used to install PIP and then the pip module can be used to install the Python docker module.

---
- hosts: all
  tasks:
  - name: install pip
    dnf:
      name: pip
      state: present

  - name: pip install docker
    pip:
      name: docker
      state: latest
...

 

Here is how the docker_container module could be used to create the "www" container from the nginx image.

  • If the image does not exist, this will attempt to find and pull the image from the public https://hub.docker.com/ repository. If the image is found, this will also then attempt to create and start a container, using the image.
---
- hosts: all
  tasks:
  - name: create and start the 'www' container from the nginx image
    docker_container:
      name: www
      image: nginx
      state: started
...

 

Or, you can be more specific and include an image tag, such as "latest".

---
- hosts: all
  tasks:
  - name: create and start the 'www' container from the nginx image
    docker_container:
      name: www
      image: nginx:latest
      state: started
...

 

If the container already exists, you do not need to use the image key. Here is how you can start the container.

---
- hosts: all
  tasks:
  - name: start the 'www' container
    docker_container:
      name: www
      state: started
...

 

Or to restart the container.

---
- hosts: all
  tasks:
  - name: stop the 'www' container
    docker_container:
      name: www
      state: started
      restart: true
...

 

Or to stop the container.

---
- hosts: all
  tasks:
  - name: stop the 'www' container
    docker_container:
      name: www
      state: stopped
...

 

Or to delete the container.

---
- hosts: all
  tasks:
  - name: delete the 'www' container
    docker_container:
      name: www
      state: absent
...

 


Remove and Detach

detact: true can be used to run the container in the background and cleanup: true can be used to remove the container after it has completed.

---
- hosts: all
  tasks:
  - docker_container:
      name: www
      state: started
      detach: true
      cleanup: true
...

 


Ports

In this example, the Docker system will listed on port 8080 and the Docker container will listen on port 80.

- name: start the 'www' container
  docker_container:
    name: www
    state: started
    ports:
    - "8080:80"

 

This will create an entry in iptables to allow connections to the container on port 8080. 

~]# iptables --list --numeric
Chain INPUT (policy ACCEPT 110 packets, 9880 bytes)
 target     prot opt     source              destination
ACCEPT     all  --       0.0.0.0./0          172.17.0.3  tcp dpt:8080

 

In this example, the IP address of the Docker system is included.

- name: start the 'www' container
  docker_container:
    name: www
    state: started
    ports:
    - "10.20.0.2:8080:80"

 

Which would create an entry in iptables with the destination IP address.

~]# iptables --list --numeric
Chain INPUT (policy ACCEPT 110 packets, 9880 bytes)
 target     prot opt     source              destination
ACCEPT     all  --       0.0.0.0./0          172.17.0.3  tcp dpt:8080

 


Volumes

In this example, the /usr/local/foo directory on the Docker system will be mounted to the /app directory in the Docker container.

- name: start the 'www' container
  docker_container:
    name: www
    state: started
    volumes:
    - /usr/local/foo:/app

 


Network

In this example, the container will use the "bridge" network.

- name: start the 'www' container
  docker_container:
    name: www
    state: started
    network_mode: 'bridge'

 

And in this example, the IP address is defined. purge_networks: true and networks_cli_compabile: false are used so that a deprecation warning does not appear in the output.

- name: start the 'www' container
  docker_container:
    name: www
    state: started
    network_mode: 'bridge'
  networks:
    - name: foo-network
      ipv4_address: '172.20.0.2'
  purge_networks: true
  networks_cli_compatible: false

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 1c43ec in the box below so that we can be sure you are a human.