Deploying a Docker Image to AWS EC2

Welcome back!

We're now set up with Docker and EC2, and it's time to deploy our Docker image to AWS. There are many ways to deploy Docker containers, such as Docker Compose or AWS Elastic Container Service (ECS). However, ECS is a paid service and has a more complex setup. For a single container node project, deploying directly to an EC2 instance using Docker is simple, cost-effective, and works with the AWS free tier.

Let’s get started!

Accessing Your EC2 Instance

  1. Log in to Your EC2 Instance: Use SSH to log into your EC2 instance.

     ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
    
  2. Verify Docker Installation: Ensure Docker is installed and running by running:

     docker --version
    

Preparing for Deployment

1. Logging into Docker Hub (If Using a Private Repository)

If your Docker image is private, log in to Docker Hub using your credentials:

docker login

Provide your Docker Hub username and password when prompted. If your repository is public, this step is not required.

2. Pulling the Docker Image

Using the Docker image you previously pushed to Docker Hub, pull the image to your EC2 instance:

docker pull <docker-hub-username>/<repository-name>:<tag>

For example:

docker pull my-docker-username/smit-b11-project:latest

Running the Docker Container

Command Overview

To run your container, you’ll use the docker run command. In production, you should configure your container with a restart policy to ensure high availability.

docker run -p 8000:8000 \
    --restart=always \
    <docker-hub-username>/<repository-name>:<tag>

Explanation of Flags:

  • -p 8000:8000: Maps port 8000 on the container to port 8000 on the EC2 instance.

  • --restart=always: Ensures the container restarts automatically if it crashes.

Example

Here’s a full command:

docker run -p 8000:8000 --restart=always my-docker-username/smit-b-11-project:latest

Why Use a Restart Policy?

Docker’s --restart flag ensures your application automatically recovers in case of a crash. The always policy restarts the container regardless of the exit code, ensuring continuous availability.

This approach aligns with Node.js production practices, which recommend:

  • Crashing immediately on uncaught errors to avoid running in an unknown state.

  • Using a restart mechanism like Docker’s restart policy to recover from errors.

By restarting the container, you ensure that your application starts fresh in a clean state.

Verifying the Deployment

Once the container is running, your application will be accessible at your EC2 instance’s public IP on port 8000.

  1. Copy your EC2 instance’s public IP address (e.g., http://<your-ec2-public-ip>:8000).

  2. Open the URL in your browser.

  3. Verify that your application is working as expected.

You can also view application logs directly in the terminal where the container is running.

Persistent Availability

To confirm the container continues running even if you close your SSH session:

  1. Exit the SSH terminal:

     exit
    
  2. Refresh your application in the browser. It should still be accessible, indicating that the container is running in the background on the EC2 instance.

Congratulations!

Your SMIT-B-11 project is now live and accessible to anyone on the internet! You have successfully deployed a production-ready Dockerized application to AWS EC2, complete with best practices for stability and availability.

Great work!