Modern application development relies heavily on containers for scalability, flexibility, and efficiency. Docker and Amazon Elastic Container Registry (ECR) are powerful tools in this domain. In this blog post, we’ll guide you through creating a custom Nginx Docker image, testing it locally, and deploying it to Amazon ECR. Let’s dive in!

Introduction to Docker and Amazon ECR

Overview of Docker and Its Role in Modern Development

Docker simplifies application deployment by packaging your code and dependencies into lightweight containers. These containers ensure consistency across development, testing, and production environments, making them indispensable in DevOps pipelines.

Understanding Amazon Elastic Container Registry (ECR)

Amazon ECR is a fully managed container image registry that integrates seamlessly with AWS services. It provides a secure, scalable repository to store Docker images, enabling efficient deployment to services like Amazon ECS and EKS.

Preparing Your Environment for Docker

Setting Up Docker on Your Local Machine

Before creating a custom Nginx image, install Docker on your machine:

  • Windows/Mac: Download Docker Desktop from Docker’s website.
  • Linux: Use your distribution’s package manager (apt, yum, etc.) to install Docker.

Verify the installation by running the following:

docker –version

Creating a Working Directory for Your Project

Organize your files by creating a project directory:

mkdir custom-nginx && cd custom-nginx

Building a Custom Nginx Docker Image

Pulling the Latest Nginx Image from Docker Hub

Begin by pulling the base Nginx image:

docker pull nginx:latest

Modifying the Nginx Configuration for Custom Content

Create a Dockerfile in your project directory:

FROM nginx:latest

COPY custom-content /usr/share/nginx/html

EXPOSE 80

Add custom HTML files in a custom-content folder to serve personalized content.

Running Your Custom Nginx Container Locally

Build the Docker image:

docker build -t custom-nginx .

Run the container with port mapping:

docker run -d -p 8080:80 custom-nginx

Verifying the Container Operation via Browser

Visit http://localhost:8080 in your browser to see the custom content.

Introducing Amazon ECR and Its Benefits

Why Use Amazon ECR for Docker Image Storage?

Amazon ECR offers:

  • Secure image storage with encryption.
  • Seamless integration with AWS services.
  • Automatic image replication for regional availability.

Creating Your First ECR Repository

  1. Log in to the AWS Management Console.
  2. Navigate to the ECR service.
  3. Create a new repository:
    • Choose a repository name (e.g., custom-nginx).
    • Select visibility (private or public).

Pushing Your Custom Docker Image to Amazon ECR

Tagging Your Docker Image for ECR

Tag your local image for the ECR repository:

docker tag custom-nginx:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/custom-nginx:latest

Authenticating Docker with Your AWS Account

Authenticate Docker with ECR:

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

Pushing the Image to Your ECR Repository

Push your Docker image:

docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/custom-nginx:latest

Verifying Your Docker Image in Amazon ECR

  1. Open the AWS Management Console.
  2. Navigate to your ECR repository.
  3. Confirm that your image is uploaded and accessible.

Conclusion

You’ve successfully created a custom Nginx Docker image, tested it locally, and pushed it to Amazon ECR. This workflow is a foundational step in deploying containerized applications on AWS. With Amazon ECR, your Docker images are secure, scalable, and ready for integration with AWS services like ECS, EKS, or Fargate.

References

Pushing a Docker image to an Amazon ECR private repository

Tutorial: Create a pipeline with an Amazon ECR source and ECS-to-CodeDeploy deployment