Install Docker Aws Ubuntu

Install Docker on Ubuntu 20.04. Before installing Kubernetes, we have to install Docker. Docker is a container system that is used to create containers (you can say VMs) that are used to run the applications. Follow the below steps to install Docker in Ubuntu. Execute the below mentioned commands on both worker and master nodes.

A Docker swarm consists of multiple Docker hosts which run in swarm mode and act as managers and workers (which run swarm services). A given Docker host can be a manager, a worker, or perform both roles. When you create a service, you define its optimal state (number of replicas, network and storage resources available to it, ports the service exposes to the outside world, and more). Docker works to maintain that desired state. For instance, if a worker node becomes unavailable, Docker schedules that node’s tasks on other nodes. A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container.

One of the key advantages of swarm services over standalone containers is that you can modify a service’s configuration, including the networks and volumes it is connected to, without the need to manually restart the service. Docker will update the configuration, stop the service tasks with the out of date configuration, and create new ones matching the desired configuration.

When Docker is running in swarm mode, you can still run standalone containers on any of the Docker hosts participating in the swarm, as well as swarm services. A key difference between standalone containers and swarm services is that only swarm managers can manage a swarm, while standalone containers can be started on any daemon. Docker daemons can participate in a swarm as managers, workers, or both.

Install Docker Aws Ubuntu

Concepts related to Docker swarm

Concepts related to Docker swarm are detailed below.


  1. These instructions are intended for installing Docker. I’ll be working from a Liquid Web Core Managed Ubuntu 14.04 LTS server, and I’ll be logged in as root. Step 1: Installation of Docker. First, you’ll follow a simple best practice: ensuring the list of available packages is up to date before installing anything new.
  2. Amazon Linux 2 - install docker & docker-compose using 'sudo amazon-linux-extras' command. UPDATE (March 2020, thanks @ic): I don't know the exact AMI version but yum install docker now works on the latest Amazon Linux 2. The instructions below may still be relevant depending on the vintage AMI you are using. Amazon changed the install in Linux 2.
  3. AWS CLI provides direct access to the public API (Application Programming Interface) of Amazon Web Services. Since it’s a command line tool, you can also use it to create scripts for automating your Amazon Web Services. In this article, I will show you many ways to install AWS CLI tool on your Ubuntu 18.04 LTS operating system.
  4. Step 3: Installing Docker. Now let’s install Docker on Ubuntu 20.04. Run the following command in the terminal window: sudo apt install Type y and hit Enter to confirm the installation. Once the install is completed, the output notifies you Docker has been installed. Step 4: Checking Docker Installation.

A node is an instance of the Docker engine participating in the swarm. You can run one or more nodes on a single physical computer or cloud server, but production swarm deployments typically include Docker nodes distributed across multiple physical and cloud machines.

Manager nodes also perform the orchestration and cluster management functions required to maintain the desired state of the swarm. Manager nodes elect a single leader to conduct orchestration tasks.

Worker nodes receive and execute tasks dispatched from manager nodes. By default manager nodes also run services as worker nodes, but you can configure them to run manager tasks exclusively and be manager-only nodes. An agent runs on each worker node and reports on the tasks assigned to it. The worker node notifies the manager node of the current state of its assigned tasks so that the manager can maintain the desired state of each worker.

Services and tasks

A service is the definition of the tasks to execute on the manager or worker nodes. It is the central structure of the swarm system and the primary root of user interaction with the swarm.

When you create a service, you specify which container image to use and which commands to execute inside running containers. In the replicated services model, the swarm manager distributes a specific number of replica tasks among the nodes based upon the scale you set in the desired state.

For global services, the swarm runs one task for the service on every available node in the cluster.

A task carries a Docker container and the commands to run inside the container. It is the atomic scheduling unit of swarm. Manager nodes assign tasks to worker nodes according to the number of replicas set in the service scale. Once a task is assigned to a node, it cannot move to another node. It can only run on the assigned node or fail.

Load balancing

The swarm manager uses ingress load balancing to expose the services you want to make available externally to the swarm. The swarm manager can automatically assign the service a PublishedPort or you can configure a PublishedPort for the service. You can specify any unused port. If you do not specify a port, the swarm manager assigns the service a port in the 30000-32767 range.

External components, such as cloud load balancers, can access the service on the PublishedPort of any node in the cluster whether or not the node is currently running the task for the service. All nodes in the swarm route ingress connections to a running task instance.

Swarm mode has an internal DNS component that automatically assigns each service in the swarm a DNS entry. The swarm manager uses internal load balancing to distribute requests among services within the cluster based upon the DNS name of the service.

Deploying Docker Swarm on AWS

In order to apply these concepts we are going to set up a Docker Swarm on AWS, running three AWS EC2 t2.micro instances.

We are going to go through the following activities:

  • initializing a cluster of Docker Engines in swarm mode
  • adding nodes to the swarm
  • deploying application services to the swarm
  • managing the swarm once you have everything running

We are going to launch and install Docker on three Ubuntu Linux EC2 instances, so if you need to refresh the steps, please check this post about EC2.


We will use the AWS CLI to automate this, but you can use AWS Management Console. As we want to install and deploy docker automatically when the EC2 instance is launched we will create a bootstrap.txt file with the following content:

From our post about AWS EC2, we know the commands we need to run using AWS CLI is:

Run this command three times so we have three EC2 instances, 1 for Swarm Manager and other 2 for Swarm Workers.

Install Docker On Aws Ec2 Ubuntu 18.04

We will use the AWS Management console to name them, Swarm Manager and Swarm Worker 1 and 2. You should see something similar to this on your AWS Console:

Install Docker Ec2 Ubuntu

Also make sure the three EC2 instances belong to the same Security group and communication is allowed within the Security Group. You should have a rule which allows the communication between the hosts like the one shown below:

After you complete the tutorial setup steps, you’re ready to create a swarm. Make sure the Docker Engine daemon is started on the host machines.
We are going to connect to our Swarm manager and initiate the Swarm, advertising the private ip address of the EC2 instance that will be the manager:

Lets check docker info is recognising the server as manager:

Run the docker node ls command to view information about nodes:

Joining the Docker Swarm Cluster

Once you’ve created a swarm with a manager node, you’re ready to add worker nodes.

Let’s connect to the other two EC2 instances and join them to the Swarm

Run the command produced by the docker swarm init output from the Create a swarm step to create a worker node joined to the existing swarm:

After running the command you should see this output:

Let’s run docker node ls now on the manager and check the output

Now that we have a functional Docker Swarm, we can deploy a service.

Deploy a service to the swarm

Open a terminal and ssh into the machine where you run your manager node.

Run the following command:

The docker service create command creates the service.
The –name flag names the service helloworld.
The –replicas flag specifies the desired state of 1 running instance.
The arguments alpine ping define the service as an Alpine Linux container that executes the command ping

Run docker service ls to see the list of running services:

Install docker on aws ec2 ubuntu 20.04

Scale the service in the swarm

Install Aws Cli Docker Ubuntu

Once you have deployed a service to a swarm, you are ready to use the Docker CLI to scale the number of containers in the service. Containers running in a service are called “tasks.”

Run the following command to change the desired state of the service running in the swarm:

We will try this with a scale of 5

Run docker service ps helloworld to see the updated task list:

You can also run docker ps on the workers and manager to see the containers running:

Now that we have learnt how to create a swarm and run a service on it, we can delete the service

Delete the service running on the swarm

Run docker service rm helloworld to remove the helloworld service.

Check the containers have been shutdown with docker ps: