Install and Setup Docker Using Ansible on Ubuntu 18.04 (Part 2)
Take a look at this step by step tutorial that demonstrates how to set up Docker with Ansible and Ubuntu.
Join the DZone community and get the full member experience.
Join For FreeIn the last guide, you learned how to set up, install, and configure Ansible on Ubuntu 18.04. Now, you will use the Ansible to install and set Docker on a remote machine. To begin this guide, you need the following:
- One Ansible Control Node: You need Ansible installed and configured machine.
- One or more Ansible Hots: At least one remote host with Ubuntu 18.04 with sudo permissions.
Please make sure that your Ansible control node is able to connect to your Ansible remote machines. To test the connection, you can use ansible all -m ping command.
Creating Playbook for Operations
You will be using Ansible Playbook to perform a set of actions on your Ansible remote machine which are as following:
- Ansible prefers
aptitudepackage manager over the defaultapt. - Install the required system packages like
python3-pip,curl, and other such packages. - Install Docker GPG APT key to the system and add the official Docker repository to the apt source.
- Install Docker on the remote machine.
- Install Python Docker module via
pip. - Pull an image from Docker Registry.
Once you are through with this guide, you will be running a defined number of containers on your remote host. Let’s begin this guide.
Create an Ansible Playbook:
First, you’ve to create a working directory where all your files will reside:
$ mkdir docker_server && cd $_
$ mkdir vars && cd $_ && touch default.yml
$ cd .. && touch main.yml
The directory layout should look like:
xxxxxxxxxx
docker_server/
|-- main.yml
`-- vars
`-- default.yml
1 directory, 2 files
Let’s see what each of these files are:
docker_server: This is the project root directory containing all variable files and main playbook.vars/default.yml: Variable file resides invarsdirectory through which you are going to customize the playbook settings.main.yml: Here, you are going to define the task that is going to execute on the remote server.
vars/default.yml
Now first begin with the playbook’s variable file. Here you are going to customize your Docker setup. Open vars/default.yml in your editor of choice:
xxxxxxxxxx
$ cd docke_server && nano vars/default.yml
Copy the below lines and paste it in vars/default.yml:
xxxxxxxxxx
---
containers2
container_namedocker_ubuntu
container_imageubuntu18.04
container_commandsleep 1d
A brief explanation of each of these variables:
containers: You can define n number of containers you want to launch. Just make sure that your remote system has enough juice to run it smoothly.container_name: This variable is used to name the running containers.container_image: Image that you use when creating containers.container_command: Command that is going to run inside the new containers.
main.yml
In this file, you are going to define all tasks, where you are going to define the group of servers that should be targeted with privilege sudo. Here you are also going to load the vars/default.yml variable file you created previously. Again paste the following lines, make sure that file is in a format that follows the YAML standards.
xxxxxxxxxx
---
hostsall
becometrue
vars_files
vars/default.yml
tasks
nameInstall aptitude using apt
aptname=aptitude state=latest update_cache=yes force_apt_get=yes
nameInstall required system packages
aptname= item state=latest update_cache=yes
loop 'apt-transport-https' 'ca-certificates' 'curl' 'software-properties-common' 'python3-pip' 'virtualenv' 'python3-setuptools'
nameAdd Docker GPG apt Key
apt_key
urlhttps//download.docker.com/linux/ubuntu/gpg
statepresent
nameAdd Docker Repository
apt_repository
repodeb https//download.docker.com/linux/ubuntu bionic stable
statepresent
nameUpdate apt and install docker-ce
aptupdate_cache=yes name=docker-ce state=latest
nameInstall Docker Module for Python
pip
namedocker
namePull default Docker image
docker_image
name"{{ container_image }}"
sourcepull
nameCreate default containers
docker_container
name"{{ container_name }}{{ item }}"
image"{{ container_image }}"
command"{{ container_command }}"
statepresent
with_sequencecount= containers
Execute The Ansible Playbook:
Now, execute the playbook you created previously. For example, our playbook is on remote1, and you are going to connect it as the root user, then use the following command:
xxxxxxxxxx
$ ansible-playbook main.yml -l remote1 -u root
You will see a similar output:
xxxxxxxxxx
...
TASK [Add Docker GPG apt Key] **************************************************************************************
changed: [remote1]
TASK [Add Docker Repository] **************************************************************************************
changed: [remote1]
TASK [Update apt and install docker-ce] **************************************************************************************
changed: [remote1]
TASK [Install Docker Module for Python] **************************************************************************************
changed: [remote1]
TASK [Pull default Docker image] **************************************************************************************
changed: [remote1]
TASK [Create default containers] **************************************************************************************
changed: [remote1] => (item=1)
changed: [remote1] => (item=2)
PLAY RECAP **************************************************************************************
remote1 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Once your playbook is finished running, you can log in to your remote server via SSH and confirm if Docker container was created successfully:
xxxxxxxxxx
$ ssh -i remote1-key.pem -p 4576 remote1@youripaddresshere
$ sudo docker ps -a
Flag -i to include your private key and -p to specify the port number SSH is listening.
You should see output similar to the following:
xxxxxxxxxx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
t3gejb7o82dy ubuntu "sleep 1d" 3 minutes ago Created docker_ubuntu1
9df96gced2fg ubuntu "sleep 1d" 3 minutes ago Created docker_ubuntu2
Conclusion:
In this guide, you used Ansible to automate the process of installing and setting up Docker on a remote server. You can modify the playbook as per your need and workflow; it is also recommended that you do visit Ansible user guide for docker_container module.
Published at DZone with permission of Sudip Sengupta. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments