Ansible - Log into docker.io Docker Hub using the docker_login module

by
Jeremy Canfield |
Updated: September 14 2023
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
docker_login 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
The docker_login module can be used to log into Docker Hub. This is often used when you want to pull and image from a private repository, which can be done using the docker_container module. In this example, the password for Docker Hub is stored in a vault encrypted file
---
- hosts: all
tasks:
- name: Log into DockerHub
docker_login:
username: john.doe
password: "{{ docker_password }}"
registry: your.private.registry.io
- name: create and start the 'foo' container from the jeremycanfield/my-image:latest image
docker_container:
name: foo
image: jeremycanfield/my-image:latest
state: started
- name: Log out of DockerHub
docker_login:
state: absent
...
Did you find this article helpful?
If so, consider buying me a coffee over at