Docker

How to Run Docker Containers in AWS EC2 Instances?

How to Run Docker Containers in AWS EC2 Instances?
In: Docker, NetDevOps

Welcome to yet another Docker-themed post. In this post, we'll look at how to run Docker images in AWS EC2 instances. It's a straightforward process that combines Docker and AWS. Let's get to it.

Overview

Normally, I'm used to running containers directly on my MacOS laptop. However, I recently faced a situation where multiple individuals and systems needed to access the container. Naturally, it's not feasible to keep my laptop on 24/7 for this purpose, so I decided to shift to AWS. For this guide, I'll be using Amazon Linux 2 EC2 instance.

Please ensure that you choose Amazon Linux 2 and not Amazon Linux 2023 AMI.

Once the instance is up and running, SSH into it and run the following commands.

sudo yum update -y
sudo amazon-linux-extras install docker

sudo service docker start
sudo systemctl enable docker

sudo usermod -a -G docker ec2-user

! REBOOT !

docker version

Let's break down what these commands are doing

  1. sudo yum update -y: This command updates all the packages to their latest versions on your Amazon Linux 2 instance. It's always a good practice to start with an updated system.
  2. sudo amazon-linux-extras install docker: This is a specific command for Amazon Linux 2 that installs Docker. Amazon Linux 2 provides additional software through the amazon-linux-extras tool. In this case, we're using it to install Docker.
  3. sudo service docker start: This command starts the Docker service on the instance.
  4. sudo systemctl enable docker: Ensures that Docker will start up automatically with the system every time your instance boots. systemctl is a tool for controlling the initialization of system services, and by using the enable command, we're setting Docker to auto-start.
  5. sudo usermod -a -G docker ec2-user: This command adds the default ec2-user to the Docker group. By doing this, you allow ec2-user to run Docker commands without needing sudo for each command.
  6. ! REBOOT !: This isn't a command but a reminder. After making these configurations, it's a good idea to reboot your instance to ensure all changes are properly applied.
  7. docker version: Once the system is back up after the reboot, this command lets you check the installed version of Docker to confirm that everything is set up correctly.
ec2-user@ip-10-10-15-8 ~]$ docker version
Client:
 Version:           20.10.23
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        7155243
 Built:             Tue Apr 11 22:56:36 2023
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

!! Truncated !!

Run the Apache HTTPD Container

For this demonstration, we'll use the httpd Docker image, which is an official image for the Apache HTTP Server.

[ec2-user@ip-10-10-15-8 ~]$ docker run -d --network host --name web-server httpd

Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
360eba32fa65: Pull complete 
2832a695827e: Pull complete 
b57c1299d233: Pull complete 
45a0ea29816d: Pull complete 
8c226ac2053e: Pull complete 
Digest: sha256:0adbefbee65176b93e4135f5c133072e2fb963187b00df565e6c73814894af5c
Status: Downloaded newer image for httpd:latest
985852dd960a598e50f2e6a98a65b4d89a1cef6fdcb88b49db366e5fd5d0ad5b

When we issue the command docker run -d --network host --name web-server httpd, we are instructing Docker to launch a new container based on the httpd image, which is an official image for the Apache HTTP Server. The -d flag ensures that the container runs in detached mode, meaning it operates in the background and won't tie up the terminal with log outputs. By specifying --network host, we're telling the container to use the host network driver. This means that the Apache server in the container will directly bind to the host's IP address and port.

Lastly, the --name web-server portion of the command provides our container with a specific name, "web-server", rather than leaving Docker to assign a random default name.

Finally, you can navigate to http://docker_host_ip:80 to view the web-page

Conclusion

Running Docker on AWS EC2 is easy and useful. It lets us use containers on a cloud server, making our apps available to everyone without keeping our own computers on all the time.

Creating a container image for use on Amazon ECS - Amazon Elastic Container Service
Amazon ECS uses Docker images in task definitions to launch containers. Docker is a technology that provides the tools for you to build, run, test, and deploy distributed applications in containers. Docker provides a walkthrough on deploying containers on Amazon ECS. For more information, see
Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
Comments
More from Packetswitch
Table of Contents
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.