# How to deploy a Docker container on an Amazon EC2 Instance

Imagine you've just started learning how to create Docker containers on your local machine. Now, what if you could do the same on your EC2 instance without worrying about whether your PC will crash? Don’t get me wrong about having a personal lab for practice and all. It’s just that cloud infrastructure has made many things super flexible, especially if you’re learning new technologies or want to master existing ones.

Docker containers, Images, and everything about Microservices have gained so much popularity in different industry domains, due to their persistent nature.

This article shows you how to do the following:

* **How to create an EC2 instance**
    
* **SSH into an EC2 instance**
    
* **Install Docker Desktop on an EC2 instance**
    
* **Run a container application on an EC2 instance**
    

## Step 1: Create/Provision an EC2 Instance

AWS offers several ways to create EC2 instances, such as using the console, CLI, CDK, Python boto3, CloudFormation, and Terraform. For simplicity in this lab, we will use the console.

**Access EC2 in the AWS Console**  
In the AWS Management Console, search for **EC2** and select the service. Click **"Launch Instance"** to begin creating a new virtual server.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752279345477/168a3da8-b5f4-4862-9b60-376a5db86ba4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752279358541/dcbf692a-799c-47b5-b217-e74ae4b772a7.png align="center")

**Choose an Amazon Machine Image (AMI)**  
Select an **Amazon Linux AMI**, which comes with pre-configured settings for your OS and application environment. For this lab, we will choose the **Amazon Linux** OS.

**Select an Instance Type**  
For small-scale applications or testing, a **t2.micro** instance is a cost-effective option.

**Configure Security Group**  
Set up a security group to manage traffic to your instance. Add rules to allow:

* HTTP (port 80) & Port 8080 (We will use this for the container host port )
    
* HTTPS (port 443)
    

**Create a Key Pair**  
Generate and download a secure **SSH key pair**. You'll use this to remotely access your EC2 instance. Store the private key file in a secure location.

**Launch the Instance**  
Once the above steps are complete, launch your instance. After deployment, go to the **EC2 Instances** page to find your instance’s **public IP address**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752279744843/ff7a8dbe-2cbd-49b0-93fd-8fc20d4e51a9.png align="center")

## Step 2: Install Docker on your EC2 Instance

**NOTE**: This section of the lab assumes that you are now familiar with Provisioning EC2 Instances and can manage EC2 Instances.

**Connect to Your EC2 Instance via SSH**

Use your terminal (Mac/Linux) or an SSH client (like PuTTY for Windows) to connect:

From your terminal, go to the directory where your EC2 key pair was downloaded. In this lab, my key pair is named. `docker-container.pem` After that, run the following command:

**NOTE: Replace the below IP Address with your EC2 instance's Public IPv4 address**

```powershell
chmod 400 docker-container.pem
ssh -i docker-container.pem ec2-user@13.222.94.175
```

**Install Docker on EC2**

* When you SSH into your EC2 Instance and establish a connection, then update the package manager index by running the following commands in white color:
    

```powershell
sudo yum update -y
```

* Install the Docker package by running the following command:
    

```powershell
sudo yum install docker
```

* Start and enable the Docker service by running the following command:
    

```powershell
sudo systemctl start docker
sudo systemctl enable docker
```

* Add your EC2 instance user to the Docker group. This will let you run Docker commands without getting access denied errors.
    

> You'll need to log out and back in for group changes to take effect.

```powershell
sudo usermod -aG docker ec2-user
```

* Verify that Docker is running by running the following command:
    

```powershell
sudo service docker status
```

You should see something like the below output

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752281671337/adf72cf6-4aeb-4f49-830b-dec970a8a1e1.png align="center")

After completing the steps above, Docker should be installed and running on your Amazon EC2 instance. You can now use Docker to run and manage containers on your EC2 instance.

## Step 3: Run a Docker Container

Great job so far! You've successfully created an instance, connected via SSH, and installed Docker Desktop on the instance. Now, we're at the final part of this lab: running a sample NGINX Docker container. P.S.: To learn more about Docker and Docker containers, please visit the [Docker-wiki-page.](https://docs.docker.com/get-started/)

* Let’s run an NGINX container on port **8080** and name it `my-nginx-container`:
    

```powershell
docker run -d --name my-nginx-container -p 8080:80  nginx
```

Let me break down the above command

* `-d` Runs the container in detached mode (in the background)
    
* `—name` allows you to name your container whatever you prefer. In this lab, my container is named`my-nginx-container`:
    
* `-p 8080:80` maps the instance’s port `8080` to the container’s port `80`
    

You can now open a browser and visit your EC2 instance's **public IP** with port 8080 to see the NGINX welcome page. Here is my URL: `http://13.222.94.175:8080` By the time you read this, I will have disconnected the EC2 instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752283268651/4d34ce71-d80a-4d61-8022-3d57ac9a2493.png align="center")

**Here are some useful Docker commands to monitor and manage your container:**

* List Running Containers `docker ps`
    
* List all Containers, including stopped containers : `docker ps -a`
    
* Stop the Container: `docker stop my-nginx-container`
    
* Start the Container Again: `docker start my-nginx-container`
    
* Restart the Container: `docker restart my-nginx-container`
    

Yay! Congratulations, you have just completed this lab.
