Building on the Web series, this article demonstrates how to create a repeatable Docker environment using Ansible. When combined with the other articles in the series, these steps enable the development of a robust, replicable Hugo environment. Additionally, this approach lays the foundation for building other environments with cloud-native technologies such as Docker.
Prerequisites #
ansible
installed locally- Target server (physical/VM) w/ssh configured.
Building a network accessible server is outside the scope of this article. Optionally, this can be run against a local machine.
Ansible Playbook Overview #
- Installs prerequisites
- Adds Docker’s official GPG key and repository.
- Installs:
docker-ce
docker-ce-cli
containerd.io
docker-compose-plugin
docker-buildx-plugin
(optional)
---
- name: Install Docker and Docker Compose on Ubuntu
hosts: all
become: yes
vars:
ubuntu_release: "noble"
docker_user: "{{ ansible_user }}"
tasks:
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
update_cache: yes
- name: Add Docker's official GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ubuntu_release }} stable"
state: present
- name: Install Docker Engine
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
update_cache: yes
- name: Add user to the Docker group
ansible.builtin.shell: |
sudo usermod -aG docker {{ docker_user }}
- name: Ensure Docker service is started and enabled
service:
name: docker
state: started
enabled: yes
Find Ansible
docker-install.yaml
playbook on my GitHub
Save and Run Playbook #
- Save the playbook as
docker-install.yaml
. - Capture the IP address of the target server.
Use localhost in place of IP address if installing locally.
- Run the playbook:
ansible-playbook -i 10.110.1.10, docker-install.yaml -u $USER -b -K -vvv
Usage:
- -u: $USER uses current user. Replace with username if user differs on server.
- -b: become (sudo)
- -K: Prompt for sudo password.
- -vvv: Run with level of verbosity (optional).
Note! For ssh password authentication you may need to additionally pass
-k
flag to receive the ssh password authentication prompt. The test server for this article was configured using public key authentication.