How to Install Docker on Ubuntu 22.04 LTS in AWS Cloud

Sun Oct 29, 2023

Overview

Docker Engine is an open-source containerization technology for building and containerizing your applications. 

Docker Engine acts as a client-server application with: 

  1.  A server with a long-running daemon process dockerd
  2. APIs which specify interfaces that programs can use to talk to and instruct the Docker daemon. 
  3. A command line interface (CLI) client docker

The CLI uses Docker APIs to control or interact with the Docker daemon through scripting or direct CLI commands. Many other Docker applications use the underlying API and CLI. The daemon creates and manages Docker objects, such as images, containers, networks, and volumes.

In this guide, we will walk you through the steps to install Docker on an Ubuntu 22.04 LTS-based AWS EC2 instance, enabling you to harness the benefits of containerization in the cloud.

Supported platforms

Docker Engine is available on a variety of Linux distros, macOS, and Windows 10 through Docker Desktop, and as a static binary installation.

Installation methods

You can install Docker Engine in different ways, depending on your needs:

  1. Install using Docker Desktop for Linux
  2. Set up and install Docker Engine from Docker's apt repository.
  3. Install it manually and manage upgrades manually.
  4. Use a convenience script.
In this Blog, we will set up and install Docker Engine using Docker's apt repository.

Prerequisites

Before you begin, make sure you have the following prerequisites in place:

  1. An AWS account with an EC2 instance running Ubuntu 22.04 LTS.
  2. SSH access to your AWS EC2 instance.
  3. Basic knowledge of the Linux command line.


Step 1: Connect to Your AWS EC2 Instance

Use SSH to connect to your AWS EC2 instance


Step 2: Update the System

sudo apt update -y

Step 3: Set up Docker's apt repository.

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Step 4: Install the Docker packages.

To install the latest version, run:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Running this command will install Docker and its associated components on your system, allowing you to work with Docker containers, build custom images, and manage multi-container applications using Docker Compose.

Step 5: Verifying Docker Installation

Verify that the Docker Engine installation is successful by running the hello-world image.

sudo docker run hello-world

Congratulations, You have now successfully installed and started Docker Engine.

Note: If you try the above docker command without sudo, you will get an error permission denied.

docker run hello-world

We get this error because Docker requires administrative privileges to run, and the Ubuntu user does not have the necessary permissions. 

You can resolve it by using sudo before Docker commands or adding the Ubuntu user to the docker group and applying the group membership changes.

Step 6: Manage Docker as a non-root user

The Docker daemon binds to a Unix socket, not a TCP port. By default, it's the root user that owns the Unix socket, and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don't want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group. On some Linux distributions, the system automatically creates this group when installing Docker Engine using a package manager. In that case, there is no need for you to manually create the group.

In our case, the docker group is already created during the installation of the Docker Engine. You can check by executing the following command.

cat /etc/group | grep docker

Add non-root user to the docker group

sudo usermod -aG docker $USER
when you run this command, it adds your current user to the "docker" group, allowing you to run Docker commands without having to use sudo each time.

Log out and log back in so that your group membership is re-evaluated or 

Run the following command to activate the changes to groups

newgrp docker
Verify that you can run docker commands without sudo

docker run hello-world

Step 7: Configure Docker to start on boot with systemd

To start the Docker and Containerd services automatically when your system boots up run the following commands

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

Conclusion

In this tutorial, we've covered the steps to install Docker on an Ubuntu 22.04 LTS-based AWS EC2 instance successfully 

I hope you enjoyed reading this blog and found it informative. If you have any questions or topics you'd like us to cover in future blogs, please don't hesitate to connect with me on LinkedIn. Thank you for joining us on this Docker journey,

Sampath Siva Kumar Boddeti
AWS & Terraform Certified

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
DevOpsHub 2024 Privacy policy Terms of use Contact us Refund policy