Installing Docker on Amazon Linux 2 is a straightforward process that opens up a world of possibilities for containerization. Let’s dive into the steps, ensuring you have a smooth experience from start to finish.

Step 1: Update Your System

First things first: you want to make sure your system is up to date. Run the following command to perform a quick update:

sudo yum update -y

Step 2: Install Docker

Once your system is prepped, installing Docker is as simple as executing:

sudo yum install -y docker

Step 3: Configure User Permissions

To streamline your workflow, it’s beneficial to add the ec2-user to the Docker group. This allows you to run Docker commands without needing to prefix them with sudo. Just remember: after making this change, log out and log back in for it to take effect.

sudo usermod -a -G docker ec2-user

Step 4: Enable and Start Docker Service

Now, let’s ensure that Docker starts automatically upon system boot and then initiate the service:

sudo systemctl enable docker
sudo service docker start

Step 5: Verify Your Installation

To confirm that Docker is running as expected, you can check the version and list any running containers with the following commands:

docker -v
docker ps -a

You should see output similar to this:

[ec2-user@ip-172-27-0-223 ~]$ docker -v
Docker version 18.09.9-ce, build 039a7df
[ec2-user@ip-172-27-0-223 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[ec2-user@ip-172-27-0-223 ~]$

Important Note

Keep in mind that Amazon Linux tends to ship with an older version of the Docker engine. At the time of this writing, the latest Docker version is 19.03, while Amazon’s package offers 18.09. This disparity could affect certain features or compatibility, so always check the version you need for your projects.

Step 6: Install Docker Compose

If you’re looking to manage multi-container Docker applications, Docker Compose is your friend. You can install it using the following command:

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose

Wrapping Up

Congratulations! You now have Docker (and optionally Docker Compose) installed on your Amazon Linux 2 instance. This setup will empower you to build, ship, and run applications in containers, enhancing your workflows and development processes.

As you embark on this journey with Docker, remember: every container you create is a step towards a more efficient and modernized infrastructure. Happy containerizing!