Introduction
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.
Prerequisits
- A local computer with a Linux distribution (preferebly Ubuntu)
- A remote server (create a virtual machine in the Google Cloud and access it via SSH – you can test it for free!)
Install Ansible
First of all you need to install Ansible on your local computer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # 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
1 2 3 4 5 6 7 | 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.
1 | sudo nano /etc/ansible/hosts |
Append the following lines to the file and save changes
1 2 3 4 5 6 7 | [remote] remote_test [remote:vars] ansible_host=IP_ADDRESS_OF_VIRTUAL_MACHINE ansible_ssh_private_key_file=~/.ssh/YOUR_SSH_PRIVATE_KEY_FILE ansible_user=YOUR_USERNAME |
To test if Ansible can connect to your remote compute instance via SSH type the following command
1 | ansible remote -m ping |
The result should be
1 2 3 4 5 6 7 | 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.
1 | nano install-docker-playbook.yml |
Insert the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | --- - name: Install Docker hosts: remote vars: CTOP_VERSION: "0.7.3" DOCKER_COMPOSE_VERSION: "1.25.1" DOCKER_PACKAGES: - apt-transport-https - ca-certificates - curl - gnupg-agent - software-properties-common USER: "YOUR_USER_ON_THE_REMOTE_SERVER" tasks: - name: Update apt packages apt: update_cache: "yes" force_apt_get: "yes" - name: Install packages needed for Docker apt: name: "{{ DOCKER_PACKAGES }}" state: present force_apt_get: "yes" - name: Add Docker GPG apt Key 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 apt_repository: repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ubuntu_version.stdout }} stable" state: present - name: Update apt packages apt: update_cache: "yes" force_apt_get: "yes" - name: Install Docker apt: 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 debug: msg: "Container Output: {{hello_world_output.stdout}}" - name: Create docker group group: name: "docker" state: present - name: Adding user {{ USER }} to docker group user: name: "{{ USER }}" groups: "docker" append: "yes" - name: Install Docker Compose get_url: 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 get_url: 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
1 | 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.