Ansible - docker_container module

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

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. This will also start the container.

- name: create and start the 'www' container from the nginx image
  docker_container:
    name: www
    image: nginx
    state: started

 

Or to start the "www" container, assuming the container exists.

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

 

Or to restart the "www" container.

- name: stop the 'www' container
  docker_container:
    name: www
    state: started
    restart: true

 

Or restart the "www" container.

- name: stop the 'www' container
  docker_container:
    name: www
    state: stopped

 

Or to delete the "www" container.

- name: delete the 'www' container
  docker_container:
    name: www
    state: absent

 


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.

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |