Hosting a Docker Image in Amazon ECR and Deploying to EC2 Instances

Hosting a Docker Image in Amazon ECR and Deploying to EC2 Instances

ยท

3 min read

Amazon Elastic Container Registry (ECR) and Amazon Elastic Compute Cloud (EC2) are two powerful services offered by Amazon Web Services (AWS) that enable users to store and deploy Docker containers with ease. In this detailed guide, we will walk through the process of hosting a Docker image in Amazon ECR and deploying it to EC2 instances.

Table of Contents

  1. Overview

  2. Prerequisites

  3. Step 1: Creating a Docker Image

  4. Step 2: Hosting the Docker Image in Amazon ECR

  5. Step 3: Configuring EC2 Instances

  6. Step 4: Deploying the Docker Image to EC2 Instances

  7. Conclusion

1. Overview

Amazon ECR is a fully-managed Docker container registry that makes it easy to store, manage, and deploy Docker container images. EC2 is a web service that provides secure, resizable compute capacity in the cloud. By combining these two services, users can build scalable and reliable containerized applications.

2. Prerequisites

Before we begin, make sure you have the following:

  • An AWS account with appropriate permissions to create ECR repositories and EC2 instances.

  • Docker installed on your local machine.

  • AWS CLI installed and configured with your AWS credentials.

3. Step 1: Creating a Docker Image

First, create a Dockerfile in your project directory that defines the instructions for building your Docker image. Here is a simple example of a Dockerfile for a Node.js application:

# Use the official Node.js 14 image as the base image
FROM node:14

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose port 3000
EXPOSE 3000

# Command to run the application
CMD ["npm", "start"]

Build the Docker image using the following command:

docker build -t my-node-app .

4. Step 2: Hosting the Docker Image in Amazon ECR

4.1. Navigate to the Amazon ECR Console

  1. Sign in to the AWS Management Console.

  2. Open the Amazon ECR console at https://console.aws.amazon.com/ecr/.

  3. In the navigation pane, choose Repositories.

4.2. Create an Amazon ECR Repository

  1. In the Amazon ECR console, choose Create repository.

  2. Enter a name for your repository, for example, my-node-app, and choose Create repository.

4.3. Authenticate Docker to Your Amazon ECR Registry

Run the following command in your terminal to authenticate Docker to your Amazon ECR registry:

aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com

Replace <region> and <account-id> with your AWS region and account ID, respectively.

4.4. Tag the Docker Image

Tag your Docker image with the repository URI:

docker tag my-node-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest

4.5. Push the Docker Image to Amazon ECR

Push the Docker image to Amazon ECR using the following command:

docker push <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest

5. Step 3: Configuring EC2 Instances

Launch EC2 instances that will host your Docker containers. Make sure to select an appropriate instance type, IAM role, and security group settings. Also, ensure that the EC2 instances have the Docker engine installed.

6. Step 4: Deploying the Docker Image to EC2 Instances

SSH into your EC2 instances and pull the Docker image from Amazon ECR using the following command:

docker pull <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest

Run the Docker container on your EC2 instance:

docker run -d -p 3000:3000 <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest

7. Conclusion

In this guide, we covered the process of hosting a Docker image in Amazon ECR and deploying it to EC2 instances. By following these steps, you can leverage the scalability and reliability of AWS services to deploy your containerized applications with ease. Experiment with different configurations and services to optimize your deployment workflow and maximize your application's performance.

ย