← Back to Projects

Docker Nginx Container with AWS ECR

Containerize web applications with Docker and deploy to Amazon Elastic Container Registry

Tools: Docker, Nginx, AWS ECR, AWS CLI
Base Image: Alpine Linux
Container Orchestration

Overview

This project demonstrates the complete workflow of containerizing a web application using Docker and deploying it to Amazon Elastic Container Registry (ECR). The company's landing page is dockerized using Alpine Linux as the lightweight base image, configured with Nginx web server, and made accessible to other users through AWS ECR.

Jenkins Thumbnail

Project Objectives:

Prerequisites

Project Structure

The project directory contains the structure below:

project structure

The default.conf file configures Nginx to serve content from /var/www/html/ on port 80.

Step-by-Step Implementation

1Write the Dockerfile

Create a Dockerfile that defines the base image, installs Nginx, and configures the web server with your website files and custom Nginx configuration.

# Create the Dockerfile
vim Dockerfile
dockerfile
Dockerfile Breakdown:
  • FROM alpine:3.13 - Uses Alpine Linux 3.13 as the lightweight base image
  • RUN apk upgrade - Updates all packages in the base image
  • RUN apk add nginx - Installs Nginx web server
  • COPY default.conf - Replaces default Nginx configuration
  • RUN mkdir - Creates directory for website content
  • WORKDIR - Sets the working directory
  • COPY --chown - Copies website files with correct ownership
  • EXPOSE 80 - Opens port 80 for HTTP traffic
  • CMD - Starts Nginx in daemon-off mode for container compatibility

2Build the Docker Image

Build the Docker image using the Dockerfile and tag it with a meaningful name:

# Build the image from the current directory
docker build . -t web

# Verify the image was created successfully
docker images

This command builds the image using the Dockerfile in the current directory and tags it as web. The build process will execute all instructions in the Dockerfile and create a reusable image.

dockerimage

3Launch the Container

Launch a container using the newly created image, mapping port 80 on the container to port 80 on the host:

# Run the container in detached mode with port mapping
docker run -dt -p 80:80 --name web01 web

# Verify the container is running
docker ps

The -dt flags run the container in detached mode (background), and -p 80:80 maps the container's port 80 to the host's port 80, making the website accessible.

4Test the Configuration

Verify that the containerized website is working correctly:

# Test locally using curl
curl localhost

# Access via web browser
# Navigate to: http://localhost or http://your-server-ip

The curl command should return the HTML content

localhost

and accessing the IP address in a web browser should display your landing page.

browser

5Push the Image to AWS ECR

After successful testing, push the Docker image to Amazon ECR for centralized storage and distribution.

5.1 Install and Configure AWS CLI:

# Install AWS CLI
sudo apt-get install awscli

# Configure AWS credentials
aws configure

Enter your AWS Access Key ID, AWS Secret Access Key, and default region when prompted.

5.2 Get AWS Account ID:

# Retrieve your AWS account ID
aws sts get-caller-identity
aws-cred

Note the account ID from the output - you'll need it for subsequent steps.

5.3 Authenticate Docker with AWS ECR:

# Authenticate Docker to AWS ECR
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin <*account-ID*>.dkr.ecr.eu-west-2.amazonaws.com
Note: Replace <account-ID> with your actual AWS account ID and adjust the region (eu-west-2) to match your AWS region.

5.4 Create ECR Repository:

# Create a new ECR repository named 'web'
aws ecr create-repository --repository-name web
ecr repo

5.5 Tag the Docker Image:

# Tag the image for ECR
docker tag web:latest <*account-ID*>.dkr.ecr.eu-west-2.amazonaws.com/web:latest

5.6 Push the Image to ECR:

# Push the image to AWS ECR
docker push <*account-ID*>.dkr.ecr.eu-west-2.amazonaws.com/web:latest
docker push

5.7 Verify the Image in AWS Console:

Navigate to the AWS ECR console to confirm the image has been successfully uploaded:

aws console
aws console

Key Achievements

Benefits of This Approach

Technologies Used

Docker Nginx Alpine Linux AWS ECR AWS CLI Bash Container Orchestration DevOps

Next Steps

To further enhance this deployment workflow, consider:

Conclusion

This project demonstrates a complete containerization and deployment workflow using industry-standard tools. By containerizing the web application with Docker and deploying to AWS ECR, we've created a reproducible, scalable, and portable deployment solution.

The use of Alpine Linux ensures minimal resource consumption, while AWS ECR provides enterprise-grade container image management with built-in security, scalability, and integration with other AWS services. This approach forms the foundation for modern cloud-native application deployment strategies.

← Back to Projects Get in Touch