How to launch Docker containers with Ansible
A common use case for Ansible is to launch Docker containers from private images stored in Docker Hub. Once you have Docker installed, a playbook like this will get you started. This will log into your Docker Hub account, and start a container from a private image you specify:
- hosts: all
become: true
tasks:
- name: log into docker hub registry
docker_login:
email: "your-email@address"
username: "a-dockerhub-username"
password: "a-dockerhub-password"
- name: ensure a container is running
docker_container:
name: my_container
state: started
image: "my_username/my_container:master-latest"
pull: true
ports:
- "5000:5000"
We are specifying pull: true
to make sure the image is always downloaded - Docker only does this automatically for the latest
tag.