Skip to main content

Ansible for Docker Deployment

Shenzhen, China

Downloading Docker Images

See docker_image Module.

---
- hosts: test
tasks:
- name: Pull Docker Image
docker_image:
name: hello-world
source: pull
state: present

Running the playbook results in an error message that the docker python library is missing:

ansible-playbook docker.yml

PLAY [test] ********************************************************************************************************

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

TASK [Pull Docker Image] ********************************************************************************************************************
fatal: [192.168.2.111]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on minion's Python /usr/bin/python3. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'docker'"}
PLAY RECAP *********************************************************************************************************
192.168.2.111 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

I will try to solve this by running pip install docker on my host system. If this works, this step has to be included in the docker playbook.

During handling of the above exception, another exception occurred: Read timed out. Increasing the timeout with pip --default-timeout=1000 install docker

Running Docker Container

See docker_container Module.

This plugin is part of the community.general collection (version 1.3.0). To install it use:

ansible-galaxy collection install community.general

Example I

To use it in a playbook, specify: community.general.docker_container:

Ansible

---
- hosts: test
tasks:
- name: Setup a Nginx Docker Container
community.general.docker_container:
name: ingress
image: nginx:stable-alpine
networks:
- name: gateway
state: started
restart_policy: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- /opt/docker-ingress/configuration/conf.d:/etc/nginx/conf.d
- /opt/docker-ingress/configuration/nginx.conf:/etc/nginx/nginx.conf
- /opt/test-static/public:/opt/test-static/public
- /opt/downloads:/opt/downloads

This is the Ansible version of the Docker-Compose NGINX Example - which is really interesting... you can actually migrate from docker-compose to Ansible. Did not think of that before:

Docker-Compose

version: "3.8"
services:

ingress:
image: nginx:stable-alpine
container_name: ingress
networks:
- gateway
ports:
- "80:80"
- "443:443"
restart: unless-stopped
volumes:
- /opt/docker-ingress/configuration/conf.d:/etc/nginx/conf.d
- /opt/docker-ingress/configuration/nginx.conf:/etc/nginx/nginx.conf
- /opt/test-static/public:/opt/test-static/public
- /opt/downloads:/opt/downloads

networks:
gateway: {}

Example II

I now want to rebuild the web app template with Docker that I build before by installing NGINX directly on the host system:

---
- hosts: test
tasks:
- name: Setup a Nginx Docker Container
docker_container:
name: ingress
image: nginx:stable-alpine
state: started
restart_policy: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- /opt/test-static/nginx/nginx.conf:/etc/nginx/nginx.conf
- /opt/test-static/nginx/conf.d/:/etc/nginx/conf.d
- /opt/test-static/nginx/ssl/:/etc/nginx/ssl
- /opt/test-static/public/:/opt/test-static/public