DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Ansible and the Pre-Container Arts
  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Orange Pi Cluster With Docker Swarm and MariaDB
  • Kubernetes Cluster Setup on Ubuntu, Explained

Trending

  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • How to Merge HTML Documents in Java
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Install and Setup Docker Using Ansible on Ubuntu 18.04 (Part 2)

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.

By 
Sudip Sengupta user avatar
Sudip Sengupta
DZone Core CORE ·
Jul. 22, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
15.9K Views

Join the DZone community and get the full member experience.

Join For Free

In 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:

  1. Ansible prefers aptitude package manager over the default apt.
  2. Install the required system packages like python3-pip, curl, and other such packages.
  3. Install Docker GPG APT key to the system and add the official Docker repository to the apt source.
  4. Install Docker on the remote machine.
  5. Install Python Docker module via pip.
  6. 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:

Shell
 




x


 
1
$ mkdir docker_server && cd $_ 
2
$ mkdir vars && cd $_ && touch default.yml 
3
$ cd .. && touch main.yml


The directory layout should look like:

Plain Text
 




xxxxxxxxxx
1


 
1
docker_server/ 
2
|-- main.yml 
3
`-- vars
4
   `-- default.yml 
5
 
          
6
1 directory, 2 files


Let’s see what each of these files are:

  1. docker_server: This is the project root directory containing all variable files and main playbook.
  2. vars/default.yml: Variable file resides in vars directory through which you are going to customize the playbook settings.
  3. 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:

Shell
 




xxxxxxxxxx
1


 
1
$ cd docke_server && nano vars/default.yml


Copy the below lines and paste it in vars/default.yml:

YAML
 




xxxxxxxxxx
1


 
1
--- 
2
containers: 2 
3
container_name: docker_ubuntu 
4
container_image: ubuntu:18.04 
5
container_command: sleep 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.

YAML
 




xxxxxxxxxx
1
44


 
1
---
2
 - hosts: all
3
   become: true
4
   vars_files:
5
    - vars/default.yml  
6
 
          
7
tasks:
8
   - name: Install aptitude using apt
9
     apt: name=aptitude state=latest update_cache=yes force_apt_get=yes
10
 
          
11
    - name: Install required system packages
12
      apt: name={{ item }} state=latest update_cache=yes
13
      loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']
14
 
          
15
    - name: Add Docker GPG apt Key
16
      apt_key:
17
       url: https://download.docker.com/linux/ubuntu/gpg
18
       state: present
19
 
          
20
    - name: Add Docker Repository
21
      apt_repository:
22
       repo: deb https://download.docker.com/linux/ubuntu bionic stable
23
       state: present
24
 
          
25
    - name: Update apt and install docker-ce
26
      apt: update_cache=yes name=docker-ce state=latest
27
 
          
28
    - name: Install Docker Module for Python
29
      pip:
30
       name: docker
31
 
          
32
    - name: Pull default Docker image
33
      docker_image:
34
       name: "{{ container_image }}"
35
       source: pull
36
 
          
37
    - name: Create default containers
38
      docker_container:
39
       name: "{{ container_name }}{{ item }}"
40
       image: "{{ container_image }}"
41
       command: "{{ container_command }}"
42
       state: present
43
     with_sequence: count={{ containers }}
44
 
          


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:

Shell
 




xxxxxxxxxx
1


 
1
$ ansible-playbook main.yml -l remote1 -u root


You will see a similar output:

Plain Text
 




xxxxxxxxxx
1
22


 
1
... 
2
TASK [Add Docker GPG apt Key] ************************************************************************************** 
3
changed: [remote1] 
4
 
          
5
TASK [Add Docker Repository] ************************************************************************************** 
6
changed: [remote1] 
7
 
          
8
TASK [Update apt and install docker-ce] ************************************************************************************** 
9
changed: [remote1] 
10
 
          
11
TASK [Install Docker Module for Python] ************************************************************************************** 
12
changed: [remote1] 
13
 
          
14
TASK [Pull default Docker image] ************************************************************************************** 
15
changed: [remote1] 
16
 
          
17
TASK [Create default containers] ************************************************************************************** 
18
changed: [remote1] => (item=1) 
19
changed: [remote1] => (item=2) 
20
 
          
21
PLAY RECAP ************************************************************************************** 
22
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:

Shell
 




xxxxxxxxxx
1


 
1
$ ssh -i remote1-key.pem -p 4576 remote1@youripaddresshere 
2
$ 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:

Plain Text
 




xxxxxxxxxx
1


 
1
CONTAINER ID   IMAGE     COMMAND      CREATED        STATUS    PORTS  NAMES 
2
t3gejb7o82dy   ubuntu    "sleep 1d"   3 minutes ago  Created          docker_ubuntu1 
3
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.

Docker (software) Ansible (software) ubuntu remote Plain text

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Ansible and the Pre-Container Arts
  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Orange Pi Cluster With Docker Swarm and MariaDB
  • Kubernetes Cluster Setup on Ubuntu, Explained

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!