How to Install Docker on Amazon Linux 2 Instance

How to Install Docker on Amazon Linux 2 Instance

How to Install Docker on Amazon Linux 2 Instance Step by Step

Dear Reader, I hope you are doing well. Today, I am here to help you install docker on Amazon Linux 2 EC2 instance in a step-by-step manner. Additionally, we’ll see how to install docker-compose on Amazon Linux 2 instance as well.

Therefore, if you are looking for “How do I install Docker and Docker Compose on Amazon Linux 2?”, you have come to the right place.

You can also check out my other post to install Nginx, Apache, Java, Git, Ansible, PHP 8.1, PHP 8.2 etc. on Amazon Linux 2.

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Background

Before installing docker and docker-compose on our Amazon Linux 2 system, let’s understand a bit about these terms.

Amazon Linux 2: Amazon Linux 2 is a very very popular Linux AMI provided by AWS itself and is also free tier eligible. In simple words, If I say, Amazon Linux 2 is a Linux operating system by Amazon Web Services.

Docker: Docker is an open-source platform that lets you package and run an application in a loosely isolated environment called a container. In simple words, it enables you to build, deploy, run, update and manage containers easily.

Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications.

Now that we have got know them a bit, let’s proceed to install docker and docker-compose on our Amazon Linux 2 EC2 Instance

Prerequisite

To follow this tutorial, you will need the following-

Steps to Install Docker on Amazon Linux 2 Instance

  1. Launch an Amazon Linux 2 Instance
  2. Connect to your Amazon Linux 2 instance
  3. Update System With the Latest Package
  4. Search for Docker Package
  5. Find the Package Info
  6. Install Docker on Amazon Linux 2
  7. Check Docker Version
  8. Check Docker Running Status
  9. Start & Enable Docker
  10. Run Docker Command without sudo
  11. Run a Simple Docker Image
  12. Install docker-compose on Amazon Linux 2(Optional)

Let’s get started …

Step 1: Launch an Amazon Linux 2 Instance

Before you install docker on an Amazon Linux 2 instance, you need one instance up and running. Simply launch an EC2 instance with a Linux 2 AMI and you should be good to go.

In case you need help in launching an EC2 instance, here is a step-by-step tutorial to launch an Amazon Linux 2 instance on AWS: How to Launch EC2 Instance Step by Step in AWS

Step 2: Connect to your Amazon Linux 2 instance

To run any command on your EC2 instance, you need to be connected to your instance. Once your instance is up and running, connect to your EC2 instance.

You can either use the EC2 instance connect that lets you SSH into instances from the browser itself.

Or, if you want SSH from your local machine, feel free to use the below command from your terminal.

ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip

Replace parameters as mentioned below-

  • /path/my-key-pair.pem: Path of your .pem file
  • instance-public-ip: Public IP of your instance that you can grab from the EC2 details screen

Connect to Our Instance using EC2 Instance Connect

For this tutorial, I am using SSH from the browser option itself.

1. Select your instance and click Connect as highlighted below

How to Install Apache Web Server on Amazon Linux 2

2. Once, you click Connect, you will see a screen with the default username for a Linux 2 instance

Verify that ec2-user is showing in the username field and click Connect.

A new browser window will open and you will be connected to your instance like below.

How to Install Apache Web Server on Amazon Linux 2

Awesome !!!

Now you are ready to run commands on your EC2 instance. Let’s move to the docker installation part in the next step.

Step 3: Update System With the Latest Package

sudo yum update

It’s always recommended to download the latest update before installing any software on your instance.

Run sudo yum update command to update your system with the latest packages.

How to Install Docker on Amazon Linux 2 Instance 1

If there is any update available, it will be downloaded. If nothing is there, the command will finish with the message No packages marked for update as shown above.

Step 4: Search for Docker Package

sudo yum search docker

Run the above command to find the docker package available on your Amazon Linux 2 system.

How to Install Docker on Amazon Linux 2 Instance 2

As seen in the screenshot, we did find the package and hence we know the package name now.

Step 5: Find the Package Info

Let’s find the package info including the repository in which the docker package is residing. Then we will see how can you install it from that repository.

sudo yum info docker.x86_64
How to Install Docker on Amazon Linux 2 Instance 3

When you see the info of the package, you see all the details like its version, repository it’s in etc. Let’s install it in the next step.

Step 6: Install Docker on Amazon Linux 2

From the above command, we know that docker is present in amzn2extra-docker/2/x86_64 repository.

By default, an amazon Linux 2 Ec2 instance has below-enabled repository.

sudo yum repolist all
How to Install Docker on Amazon Linux 2 Instance 4

As you can see amzn2extra-docker/2/x86_64 repo is enabled by default. When you install software using Yum on amazon Linux 2, it looks for the package in all the enabled repositories.

So the command to install docker on Amazon Linux 2 comes down to –

sudo yum install docker -y

Type the above command in the terminal and hit enter. It will install docker and all its dependencies.

Note: Please note that I have given -y so that it gets installed without prompting me whether to install it or not. You can choose to skip it if you want to see first what’s getting installed and then reply to the prompt accordingly.

How to Install Docker on Amazon Linux 2 Instance 5

Step 7: Check Docker Version

Check the docker version using the below command-

docker version
How to Install Docker on Amazon Linux 2 Instance 12

Step 8: Check Docker Running Status

Now that we have installed it, let’s check the status using the –

sudo systemctl status docker
How to Install Docker on Amazon Linux 2 Instance 6

The screenshot confirms that we have just installed docker and it’s not yet running. We need to start it.

Step 9: Start & Enable Docker

Start Docker Service

sudo service docker start

or 

sudo systemctl start docker.service
Command to start docker on linux 2

The above command starts docker and you can check the running status again by using the command sudo systemctl status docker.

Also enable docker using the below command so that every time your EC2 instance restarts, docker will start automatically.

sudo systemctl enable docker

Now check the status again using sudo systemctl status docker command. This time you will see it’s running as expected.

How to Install Docker on Amazon Linux 2 Instance 7

Step 10: Run Docker Command without sudo

While working with docker, there are many docker commands that you will use, some of the important ones are-

  • docker build: Builds an image from a Dockerfile.
  • docker run: Runs a container from an image.
  • docker images: Lists all of the images that you have.
  • docker ps: Lists all of the running containers.
  • docker stop: Stops a running container.
  • docker remove: Removes a container.

In your terminal, just type docker and hit enter to see all the available commands to you.

If you try to run any docker command for example docker ps, you will get a permission denied issue like the below-

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get “http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json”: dial unix /var/run/docker.sock: connect: permission denied

How to Install Docker on Amazon Linux 2 Instance 8

As you can see in the screenshot, the second time I ran the same command using sudo and things worked fine.

However, every time using sudo while running a docker command may not be convenient. At the same time is not recommended for security purposes. So it’s better to configure docker commands to work without sudo.

You can use below two commands to make docker commands work without sudo-

sudo usermod -a -G docker ec2-user
newgrp docker

Command Explanation:

  1. The command sudo usermod -a -G docker ec2-user adds the user ec2-user to the docker group. This allows the ec2-user user to run docker commands without having to use sudo.
  2. Usually, a logout/login is required for group membership to take effect. Alternatively, you can run newgrp docker command. So that changes to groups come to effect on an immediate basis.

As soon as you are done with the above two commands. You can run docker commands without sudo as shown in the below screenshot

How to Install Docker on Amazon Linux 2 Instance 9

Step 11: Run a Simple Docker Image

Let’s use docker run hello-world to run a pretty straightforward hello-world container. This command will run the official Docker image for “hello-world”. It’s a very simple image that prints “Hello, world!” to the console.

As seen in the below screenshot, as soon as you type this command and hit enter, Docker will first check to see if the “hello-world” image is already on our instance.

Since we don’t have any such image, Docker will download the image from the Docker Hub registry. Once the image has been downloaded, Docker will create a container from the image and start the container. You can see the same thing happening step by step in the screenshot below.

 docker run hello-world
How to Install Docker on Amazon Linux 2 Instance 10

Congratulation !!!

You have installed docker on Amazon Linux 2 EC2 Instance. If you were just looking for that, we are done. However, if need docker-compose as well, you can install it as shown below-

Step 12: Install docker-compose on Amazon Linux 2(Optional)

Install pip first on your Amazon Linux 2. Search for pip using yum search pip command and then run

yum search pip
sudo yum install python3-pip -y
sudo pip3 install docker-compose
docker-compose --version

Or

sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose version
Docker Compose Version

Other important commands:

Here are a few more commands that you might find useful during your journey with Docker.

1. Start Docker

sudo systemctl start docker.service

2. Restart Docker

sudo systemctl restart docker.service

3. Stop Docker

sudo systemctl stop docker.service

4. Prevent Docker from loading on the system boot

sudo systemctl disable docker.service

5. Force Docker to refresh configuration files

sudo systemctl reload docker.service 

Conclusion:

In this post, we learned to install Docker on Amazon Linux 2 EC2 Instance. We learnt a bit about Linux 2 and Docker and Docker Compose.

Then, we connected to the EC2 instance using a browser-based connect and we used a set of commands to install the Docker and docker compose on our Amazon Linux 2 EC2 instance.

We verified the version of docker and docker-compose. Also, we started a very simple hello world to verify everything.

Were you able to install docker and docker-compose on your Amazon Linux 2 instance? Feel free to drop a comment in case you face any issues or just to share feedback.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Leave a Reply

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