DEV Community

Cover image for How to Run a Docker Container on AWS EC2 and Access It Externally
Adeniyi Olanrewaju
Adeniyi Olanrewaju

Posted on

How to Run a Docker Container on AWS EC2 and Access It Externally

Introduction

Running a Docker container on an AWS EC2 instance is a powerful way to manage and deploy your applications with ease. Docker containers create lightweight, portable, and consistent environments, making your applications more manageable and scalable.

In this article, we will specifically focus on running a PostgreSQL container. We will guide you through the steps to set up an EC2 instance, install Docker, run a PostgreSQL Docker container, and configure the necessary settings to access the database from outside AWS. This setup is perfect for testing, development, and even small-scale production environments. Let's get started!

Prior Knowledge Needed:

  • Basic understanding of Docker and containers
  • Basic knowledge of AWS EC2 instances
  • Basic command line skills
  • Understanding of PostgreSQL database

Step-by-Step Guide

Launch an EC2 Instance

  • Log in to AWS: Log in to your AWS Management Console at AWS Console.

Image description

  • Navigate to EC2: Go to the EC2 Dashboard from the services menu.
  • Launch Instance: Click on "Launch Instance" to start creating a new virtual server.

Image description

Set up the Instance

  • Give the instance a name and Select the "Ubuntu Server 20.04 LTS" Amazon Machine Image (AMI) Image description
  • Choose the "t2.micro" instance type, which is sufficient for this tutorial and eligible for the free tier. Image description
  • Create a key pair and download the .pem file. You will need this to SSH into your instance. Image description
  • Configure the security group to Allow HTTP, SSH and HTTPS.

Image description

  • Review and Launch: Review your settings, then click "Launch."

Connect to Your EC2 Instance

  • Open Terminal: On your local machine, open your terminal (use WSL on Windows)
  • Run this command to set the appropriate permissions for the .pem file
chmod 600 /path/to/your-key-pair.pem
Enter fullscreen mode Exit fullscreen mode

Replace /path/to/your-key-pair.pem with the actual path to your .pem file.

  • Connect to Instance: Use the following command to connect to your EC2 instance via SSH:
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-public-ip
Enter fullscreen mode Exit fullscreen mode

Replace /path/to/your-key-pair.pem with the path to your downloaded key pair file and your-ec2-public-ip with your instance's public IP address.
In my case, I'm already in the directory where the .pem file is located, so I'll access it directly by its name.

  • Accept the SSH Prompt: If prompted, type "yes" to continue connecting. Image description

Install Docker

  • Update Package Index: Before installing Docker, update the package index to ensure you download the latest versions of software. Run the following command:
sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
  • Install Docker: Install Docker on your Ubuntu instance using the following command:
sudo apt-get install docker.io -y
Enter fullscreen mode Exit fullscreen mode
  • Start Docker Service: After installing Docker, start the Docker service with:
sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode
  • Enable Docker Service: To ensure Docker starts automatically when the instance boots, enable the Docker service:
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode
  • Add User to Docker Group (Optional): To avoid having to use sudo every time you run a Docker command, add your user to the Docker group:
sudo usermod -aG docker $(whoami)
Enter fullscreen mode Exit fullscreen mode

Apply the changes immediately without logging out by running:

newgrp docker
Enter fullscreen mode Exit fullscreen mode

Run the PostgreSQL Docker Container

  • Pull PostgreSQL Image: Download the PostgreSQL Docker image:
docker pull postgres
Enter fullscreen mode Exit fullscreen mode
  • Confirm that the images has been downloaded:
docker images
Enter fullscreen mode Exit fullscreen mode
  • Run PostgreSQL Container: Start a new PostgreSQL container with the following command:
docker run -d -p host-port:container-port \
  --name <container_name> \
  -e POSTGRES_DB=<db_name>\
  -e POSTGRES_USER=<db_user>\
  -e POSTGRES_PASSWORD=<db_password>\
  postgres # This is the image we are using
Enter fullscreen mode Exit fullscreen mode

The image below illustrates the process. I'll use these credentials to demonstrate how to connect to your Docker container.

Image description

Now that we've set the port to 8181, we need to configure the security group to allow traffic on that port.

  • Return to your AWS instance and take note of the security group associated with it.

Image description

  • Navigate to the 'Security Groups' tab under 'Network & Security,' identify the relevant security group, and click on it.

Image description

  • Click on the edit inbound rules button.

Image description

  • Click the 'Add new rule' button. Set the Type to 'Custom TCP,' Protocol to 'TCP,' Port range to '8181,' and Source to 'Anywhere' (which allows traffic from any IP address).

Image description

  • Save the new rule and then proceed to PGAdmin, the client used to access PostgreSQL database.

Access PostgreSQL from Outside AWS

  • Open PgAdmin Client, right click on servers and register a server

Image description

  • Input the name of the server (it can be any name)

Image description

  • Navigate to the 'Connection' tab and fill out the connection form using the credentials you used when creating the PostgreSQL container.

Image description

  • Save the changes and connect to the PostgreSQL database.

Image description

Now let us try to create a table in the database and add data

  • Expand the 'Database' tab and navigate to the 'Tables' tab.

Image description

  • Right-click on the Tables tab and select Create.

  • Under the General tab, enter the table name. Then, switch to the Columns tab to define the columns for the table.

Image description

  • Save the changes to complete the operation.

Viewing Data in the Database

  • Right-click the newly created table to view its details.

Image description

  • As we can see, the table is empty with no data. Let's proceed to add some data into it.

Image description

  • Run this query in the Query Tool by clicking the play button or using the key combination Fn + F5.
INSERT INTO public.users (id, name) VALUES
(1, 'John Doe'),
(2, 'Jane Smith');
Enter fullscreen mode Exit fullscreen mode

Image description

  • Run the previous query again to view the newly added datas.
SELECT * FROM public.users
ORDER BY id ASC 
Enter fullscreen mode Exit fullscreen mode

Image description

Conclusion

You have successfully set up and run a PostgreSQL Docker container on an AWS EC2 instance. By following these steps, you can now access your PostgreSQL database from outside AWS. This setup is useful for various environments, including testing, development, and small-scale production.

Top comments (0)