How to Run a Docker Container on AWS EC2 and Access It Externally
Adeniyi Olanrewaju
Posted on July 30, 2024
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.
- 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.
Set up the Instance
- Give the instance a name and Select the "Ubuntu Server 20.04 LTS" Amazon Machine Image (AMI)
- Choose the "t2.micro" instance type, which is sufficient for this tutorial and eligible for the free tier.
- Create a key pair and download the .pem file. You will need this to SSH into your instance.
- Configure the security group to Allow HTTP, SSH and HTTPS.
- 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
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
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.
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
- Install Docker: Install Docker on your Ubuntu instance using the following command:
sudo apt-get install docker.io -y
- Start Docker Service: After installing Docker, start the Docker service with:
sudo systemctl start docker
- Enable Docker Service: To ensure Docker starts automatically when the instance boots, enable the Docker service:
sudo systemctl enable docker
- 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)
Apply the changes immediately without logging out by running:
newgrp docker
Run the PostgreSQL Docker Container
- Pull PostgreSQL Image: Download the PostgreSQL Docker image:
docker pull postgres
- Confirm that the images has been downloaded:
docker images
- 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
The image below illustrates the process. I'll use these credentials to demonstrate how to connect to your Docker container.
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.
- Navigate to the 'Security Groups' tab under 'Network & Security,' identify the relevant security group, and click on it.
- Click on the edit inbound rules button.
- 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).
- 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
- Input the name of the server (it can be any name)
- Navigate to the 'Connection' tab and fill out the connection form using the credentials you used when creating the PostgreSQL container.
- Save the changes and connect to the PostgreSQL database.
Now let us try to create a table in the database and add data
- Expand the 'Database' tab and navigate to the 'Tables' tab.
Right-click on the
Tables
tab and selectCreate
.Under the
General
tab, enter the table name. Then, switch to theColumns
tab to define the columns for the table.
- Save the changes to complete the operation.
Viewing Data in the Database
- Right-click the newly created table to view its details.
- As we can see, the table is empty with no data. Let's proceed to add some data into it.
- 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');
- Run the previous query again to view the newly added datas.
SELECT * FROM public.users
ORDER BY id ASC
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.
Posted on July 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.