Deploy Docker Image to AWS EC2 in 5 minutes

Ahmed Srebrenica - Jun 12 - - Dev Community

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

Prerequisites

  1. Create AWS Account. I already have an AWS Account and I won’t be creating a new one.

  2. Create a DockerHub account I already have and I won’t be creating a new one.

  3. Create a GitHub Account I already have GitHub and I won’t be creating a new one.

  4. Download Visual Studio Code or another code editor.

  5. Download and install Docker.

Clone the Application to your Local machine

To make it easier, we will use my simple-node-app, which you need to clone to your local machine.

Go to your terminal and follow these steps:


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

Enter fullscreen mode Exit fullscreen mode

Then navigate to the project directory.

1

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

2

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 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:


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

Enter fullscreen mode Exit fullscreen mode

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


docker build -t node-app.

Enter fullscreen mode Exit fullscreen mode

3

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

5

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

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

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

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

5. Under Configure Storage, click the Advanced details.

9

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


#!/bin/bash

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

Enter fullscreen mode Exit fullscreen mode

10

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

12

13

Switch to sudo mode with this command:


sudo su

Enter fullscreen mode Exit fullscreen mode

Check the Docker with:


docker --version

Enter fullscreen mode Exit fullscreen mode

14

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


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

Enter fullscreen mode Exit fullscreen mode

15

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

16

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .