Learn how to deploy your app or server to AWS EC2 using Docker. This step-by-step guide covers Dockerizing your app, pushing it to AWS ECR, and running it on an EC2 instance. Perfect for developers looking to go beyond local deployments and harness AWS cloud capabilities.
Requirements:
-
A working application/server
-
An Amazon Web Services account
-
Basic understanding of how to use a terminal
-
Basic Docker knowledge
-
Basic AWS knowledge
Docker:
What is docker? A beautiful way to package your application.... to keep it simple
-
Dockerize your app
a. Install Docker if you don't have it already - https://www.docker.com/b. Create a "Dockerfile" at the root of your repo
c. Setup your Dockerfile to install needed libraries and copy files needed for app to run.
Here is sample for a Python Fast Api. Notice how:
i. Dockerfile syntax is very similar to what you would run in your terminal
ii. We copy our app folder which contains our server code,
iii. We install all the needed libraries to run our app
iv. Finally we run the server.
FROM debian:trixie-slim WORKDIR /app COPY ./app ./app RUN apt-get update && apt-get install -y \ python3-pip \ RUN python3 -m pip install fastapi uvicorn EXPOSE 80 CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]d. Build your docker image by running
docker build -t your-imageat the root of your repo. Once the build is done you should be able to see this Docker image in your Docker desktop app or rundocker image lsto view it in your terminal -
Test your dockerized app locally
a. Awesome! you got your docker image. Let's make sure it runs. Execute
docker run -d -p 8000:8000 --name your-container your-image. Notice how:i. We passed
your-imageso docker knows to run a container using the image you just built.ii. We are mapping
port 8000(your machine): port 8000(docker container)because in our docker file we run our Fast Api on port 8000 with uvicorn and we want to be able to also access this on our host machine using that same port 8000iii. We use the
-dflag so we run in detached mode, which means your terminal will not tail the running docker containing allowing you to run other commands in the same terminal window.b. Check if your container is running in the Docker desktop app or run
docker psc. Hit any of your app endpoints from a browser or Postman on port 8000
Amazon Elastic Container Registry (ECR)
-
Install AWS CLI https://aws.amazon.com/cli/. We will use this to upload our docker image to AWS
-
Setup authorization for your aws cli
a. Create a
.awsfolder at the home directory of your machine this is (~) in your terminal you can go there in your terminal usingcd ~or if you're on a mac you can go from "Macintosh HD(drive) ➝ Users ➝ Your account folder (Home folder)"b. create/update the "
config" file to point to your AWS region (you can find this at the top right when logged into the AWS management console in your browser).The config file content should look like this:
[default] region = eu-west-2 output = jsonc. create/update the "
credentials" file to hold your access keys. You can create access keys by opening "IAM" in the AWS management console and finding the "My security credentials" section or clicking on your account at the top right while in the AWS management console then going to Security credentials. Then create an access key on this page.d. Once you have your access keys you can create/update the content of the credentials file in the .aws folder to look like this:
[default] aws_access_key_id = SOMEKEY aws_secret_access_key = someKey -
Now open Amazon Elastic Container Registry on the AWS manage console in your browser and create a repository
-
Select your repository and click on the button to "View push comands" this will guide you on how to push your docker image to ECR using the AWS CLI.
Amazon Elastic Compute Cloud (EC2)
Now let's run the dockerized server on EC2
-
Create an IAM role to allow your EC2 instance to access ECR
a. Go to IAM in the AWS management console and create a new policy which gives EC2 containers read access to ECR.
b. Create a new Role and attach the ECR read access policy you just created to it. We will use this for our EC2 instance
-
Go to EC2 and create a new instance. Let's use the default Amazon Linux image for this tutorial. Ensure the architecture you select matches the system you built your docker image on this is either x86 or Arm. In the Firewall (Security Group) section make sure you allow SSH, HTTP and HTTPS traffic, you can modify these later in the security group settings later if you wish to, but we want to allow access from the internet and we want to be able to SSH into the server to set things up so these are important.
a. Once your EC2 instance is ready go to details page of the instance and edit the security settings to add the IAM Role we created to give the instance access to ECR
b. Now click the "Connect" button and use the "EC2 Instance Connect" section to ssh into your instance. This will open an in browser terminal.
Setting up the EC2 instance
For this part of the tutorial I am going to assume you created an Amazon Linux EC2 instance. AWS Linux has "yum" as its package manager so we will use that to install docker.
-
Update the package database -
sudo yum check-update -
Install docker -
sudo yum install docker -
Start docker daemon -
sudo systemctl start docker -
verify docker is running -
sudo systemctl status docker
-
Make docker boot on every system reboot/start -
sudo systemctl enable docker -
Allow running docker commands without sudo -
sudo usermod -aG docker $(whoami)(you'll need to log out and back in, or runnewgrp docker, for this to take effect) -
Pull your docker image from ECR (thanks to the IAM role we attached to instance)
a. Login using the same command on the ECR repo in the "View push commands" section -
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin [your-image.dkr.ecr.us-west-2.amazonaws.com](<your-aws-account>.dkr.ecr.<region>.amazonaws.com)b. Pull your docker image using the final command on the ECR repo in the "View push commands" section but instead of doing a push we will do a pull -
docker pull [https://<your-image-info>.dkr.ecr.us-west-2.amazonaws.com/<your-image>:latest](<your-aws-account>.dkr.ecr.<region>.amazonaws.com/<your-image>:latest) -
Now check your docker images with -
docker image ls -
Now run your docker image with -
docker run -d -p 80:8000 --restart=unless-stopped --name some-container-name your-image-id -
Now go back to your EC2 instance details and try accessing your server using the public ip address or DNS on http
Awesome! We're up and running. The next step will be enabling HTTPS for your endpoint in AWS if required, but this will be covered in a separate article.
Written by

Victor Eglein Komlah
Software Engineer
Victor is a software engineer with a focus on building scalable and reliable web and mobile applications. He has a passion for creating high-quality software solutions that are user-friendly and easy to maintain. Victor is also an advocate for clean code and best practices in software development.
View profile →
