Deploy Docker Image to AWS EC2 in 5 minutes
Tags: aws, ec2, docker, containers
Published: 2024-06-12 18:35:05

From Development to Staging in 5 Minutes…

Introduction
------------

As we know, there are many ways to run your Docker image on the Cloud. I have previously written about this and introduced two methods: **“How to Deploy a Docker Image to Amazon ECR and Run It on Amazon EC2”** & **“How to Deploy a Docker Image on AWS and Run it on Fargate”.**

The first method involves managing the infrastructure, security patches, network security, etc., while the second method allows AWS to handle the infrastructure, simplifying many processes for us. **However,** **I will write about this in future articles.**

In this article, I will show you the easiest way to move your Docker image from the **Development** to the **Staging** environment. This process takes approximately **5 minutes**. We will use **Docker** on our local machine, **DockerHub** and **AWS**.


![title-image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evk3gukgi7bea2ucok7m.png)

Prerequisites
-------------

1.  Create [AWS Account](https://aws.amazon.com/resources/create-account/). I already have an AWS Account and I won’t be creating a new one.
    
2.  Create a [DockerHub](https://hub.docker.com/signup) account I already have and I won’t be creating a new one.
    
3.  Create a [GitHub Account](https://docs.github.com/en/get-started/quickstart/creating-an-account-on-github) I already have GitHub and I won’t be creating a new one.
    
4.  Download [Visual Studio Code](https://code.visualstudio.com/download) or another code editor.
    
5.  Download and install [Docker.](https://www.docker.com/products/docker-desktop/)
    

Clone the Application to your Local machine
-------------------------------------------

To make it easier, we will use my [**simple-node-app**](https://github.com/srebreni3/simple-nodejs-app), which you need to clone to your local machine.

Go to your terminal and follow these steps:

```bash

git clone https://github.com/srebreni3/simple-nodejs-app

```

Then navigate to the project directory.


![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rf97pecwekxygv2qf3vw.png)

Go to your code editor, and you will see this:


![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2n0babv1ge2auelvsdvs.png)

We have **app.js, package.json,** and **Dockerfile**, it’s all we need.

I don’t want to explain the code of the application again, go to this [**article**](https://aws.plainenglish.io/how-to-deploy-docker-image-to-ecr-and-run-it-on-ec2-49523d39cc57) and you will find everything about it.

> Let’s summarize: **If you follow these steps, you will have an application on your Local machine, and that Local machine will be your Development environment. Feel free to change the code if you want.**

Push the Docker image to Docker Hub
-----------------------------------

We need to create a Docker image for our application. Navigate to the directory where the application is located. **If you are using an M1 or newer chip, use the following command to create the Docker image:**

```bash

docker buildx build --platform linux/amd64 -t node-app .

```

**If you are not using M1 or newer**, use this command to create a Docker image:

```bash

docker build -t node-app.

```


![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z83p9kmq4jbuo89v9a7c.png)

**When you have successfully created a Docker image, our next step is to push that image on Docker Hub.**

**Go to Docker Hub and create a new repository.**


![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1qb47wzbv5rnu260woz.png)


![5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5gkqr7s8p0kyld9trdl.png)

**Let’s summarize: With these steps, you can push your Docker image to Docker Hub. It’s preferable to push it to the Private repository rather than the Public repository.**

**Create an EC2 Instance and install Docker on it**

**Go to the EC2 dashboard in AWS Console and click the Launch instance button.**


![6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8h827o1bdxa4h3egcxc.png)

**2\. Name your instance, for example: node-docker-server. The image should be Amazon Linux 2023. If you want a free tier-eligible instance, choose t2.micro.**


![7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxyhr73zod4v4t8c07x0.png)

**3\. We would like to login to the instance, and we will create a key pair. Security group should have open inbound ports 22 and 3000.**


![8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emko8h6vzj94ldt86dll.png)

**4\. For storage, I will leave default settings, I don’t need more than 8 GiB of storage. Don’t forget: Free tier eligible customers can get up to 30 GB of EBS General Purpose (SSD) or Magnetic storage.**


![8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6wxemcoe5aruipjrjo7.png)

**5\. Under Configure Storage, click the Advanced details.**


![9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbsym8mo5ddarchokd6t.png)

Go to the bottom of the page, and you will see the User data window. Paste this code into User data:

```bash

#!/bin/bash

sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker

```


![10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ckxtn84ygf9uudqtv8p.png)

From the right side, click the **Launch instance** button.

> Let’s summarize: **With these steps, you can create an EC2 instance on AWS to set up your staging environment using User data to install Docker on the instance.**

Deploy an image from Docker Hub to AWS EC2
------------------------------------------

The first step is to connect to the EC2 instance. I will use AWS Console this time, but you can also connect through your Local machine from the terminal. Check out my older articles and you will find how to connect through a terminal from a Local machine.


![11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bro8ujnabw4gbt2ws2fp.png)


![12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvftka1bzfm25y9nmopy.png)


![13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aupwg9e3elmfcw9b010p.png)

Switch to sudo mode with this command:

```bash

sudo su

```

Check the Docker with:

```bash

docker --version

```


![14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02inu8g9adydnfeglrtm.png)

Everything is set up, the next step is to deploy the image from Docker Hub. Use this command:

```bash

docker run -d -p 3000:3000 <docker-hub-repo-name> 

```


![15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyl311ynee3ytv2hgwhc.png)

The image has been successfully created. Check the Public IP of your Instance with a 3000 port.


![16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpfgka5jcw0o8f6f7lyi.png)

**We got our Docker application on the EC2 instance.**

> Let’s summarize: **Thanks to AWS, we can create a server and deploy whatever we want to that server in just a few seconds. This is an example of deploying from Docker Hub to staging environment (EC2 instance) in a few seconds.**

Conclusion
----------

If you prefer to run your production on EC2 instead of ECS using Docker images, one of the easiest ways to transfer images from the Development environment to the Staging environment is by using this method. Although this method has its advantages and disadvantages, it is, in my opinion, the simplest way so far. I hope it proves useful to someone.