Tutorial – Install Docker using Ansible on a remote server


In this article you will learn how to install Docker on a remote server (in this tutorial we use Google Cloud) using Ansible without executing a single command on the remote server manually.

Docker is a virtualization software that runs on operation system level to easier isolate and manage running software and system infrastructure.

Ansible is an open source software and infrastructure automation programme which uses SSH among others to distribute it’s commands. If you want to find out more about how Ansible works click here.


Install Ansible

First of all you need to install Ansible on your local computer.

# Install ansible

sudo apt-get update

sudo apt-get install software-properties-common

sudo apt-add-repository --yes --update ppa:ansible/ansible

sudo apt-get install ansible

# Test if it's working

ansible --version

# First Command

ansible all -i localhost, --connection=local -m ping

The last command basically tells Ansible to target all systems for the inventory host localhost and use your local console (not ssh) to run the module ping.

The response from Ansible should be

localhost | SUCCESS => {

"changed": false,

"ping": "pong"


Configuring Ansible for the remote server

Make sure that you have access to your remote server and placed your public SSH key there. Then edit your hosts file in /etc/ansible/hosts. This is the default file Ansible uses to look for any declared hosts (and groups) where it should execute the given commands remotely.

sudo nano /etc/ansible/hosts

Append the following lines to the file and save changes



To test if Ansible can connect to your remote compute instance via SSH type the following command

ansible remote -m ping

The result should be

remote_test | SUCCESS => {

"changed": false,

"ping": "pong"


Create an Ansible playbook for Docker installation

Next we create a playbook for Ansible which is the normal way how to tell Ansible which commands to execute in which order on the remote server. The playbook is in a .yml file format and has a specific format to follow. You can find more out about playbooks in the official Ansible documentation.

nano install-docker-playbook.yml

Insert the following code in it.

- name: Install Docker
hosts: remote
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common

- name: Update apt packages
update_cache: "yes"
force_apt_get: "yes"

- name: Install packages needed for Docker
name: "{{ DOCKER_PACKAGES }}"
state: present
force_apt_get: "yes"

- name: Add Docker GPG apt Key
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Save the current Ubuntu release version into a variable
shell: lsb_release -cs
register: ubuntu_version

- name: Add Docker Repository
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ubuntu_version.stdout }} stable"
state: present

- name: Update apt packages
update_cache: "yes"
force_apt_get: "yes"

- name: Install Docker
name: "docker-ce"
state: present
force_apt_get: "yes"

- name: Test Docker with hello world example
shell: "docker run hello-world"
register: hello_world_output

- name: Show output of hello word example
msg: "Container Output: {{hello_world_output.stdout}}"

- name: Create docker group
name: "docker"
state: present

- name: Adding user {{ USER }} to docker group
name: "{{ USER }}"
groups: "docker"
append: "yes"

- name: Install Docker Compose
url: https://github.com/docker/compose/releases/download/{{ DOCKER_COMPOSE_VERSION }}/docker-compose-Linux-x86_64
dest: "/usr/local/bin/docker-compose"
mode: 0755

- name: Install Ctop
url: https://github.com/bcicen/ctop/releases/download/v{{ CTOP_VERSION }}/ctop-{{ CTOP_VERSION }}-linux-amd64
dest: "/usr/local/bin/ctop"
mode: 0755

- name: Reboot server in order for changes to take place
shell: "sleep 1 && reboot"
async: 1
poll: 0

This playbook basically tells Ansible to install Docker in several steps. We follow the basic Docker installation guide and also allow the specified user to execute Docker commands. Furthermore we install Docker-Compose (to be able to run multiple Docker containers at once) and Ctop a graphical user interface that shows you Docker metrics in real time.

Then execute it with

ansible-playbook install-docker-playbook.yml -l remote

Afterwards you should see some magic happen (can take a while) and in the output in your terminal you should find “Hello from Docker!” somewhere the debug message.

Congrats! You made it.

Final thoughts

As you see Ansible is a cool software to install other software remotely and scale it in the Cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.