Deploy Docker To Ec2

Deploy Docker To Ec2

Reading time1 min
#Cloud#DevOps#Containers#Docker#AWS#EC2

Mastering the Art of Seamless Docker Deployment on AWS EC2: A Practical Guide

Forget one-size-fits-all cloud deployment scripts. This guide dives into intelligently tailoring Docker deployment on EC2 instances, revealing overlooked configurations and real-world pitfalls that can make or break your infrastructure uptime and performance.

Deploying Docker containers on EC2 empowers developers with scalable, controlled, and replicable environments essential for modern application delivery and infrastructure management. Achieving this efficiently can markedly reduce deployment errors and maximize resource utilization.


Why Deploy Docker on AWS EC2?

Before diving into the how, it’s worth clearly stating why EC2 is often preferred for running Docker containers:

  • Full control over your infrastructure and runtime environment.
  • Ability to customize instances with specific OS versions and packages.
  • Integration with AWS ecosystem services (VPC, CloudWatch, IAM).
  • Makes it easy to scale up/down by spinning new instances.
  • Supports persistent storage options such as EBS volumes.

By mastering a seamless setup, you keep your deployments consistent across environments, making troubleshooting and updates a breeze.


Step 1: Spin Up Your EC2 Instance

Let’s start by launching an EC2 instance optimized to run Docker:

  1. Log into AWS Console → EC2 Dashboard → Launch Instance.
  2. Choose an AMI - Amazon Linux 2 or Ubuntu 22.04 LTS are excellent options for Docker support.
  3. Select instance type — t3.medium is a balanced choice for testing/staging; use larger types for production.
  4. Configure network settings: ensure the security group opens port 22 (SSH) plus any custom ports your containerized app might expose (e.g., 80, 443).
  5. Attach necessary IAM roles if your app needs AWS permissions (e.g., S3 access).
  6. Review and launch.

Step 2: Install Docker on Your EC2 Instance

Once your instance is running, SSH into it:

ssh -i your-key.pem ec2-user@your-ec2-public-ip

For Amazon Linux 2:

sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user

For Ubuntu:

sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu

Log out and back in to apply the docker group permissions or use sudo for docker commands.

Verify installation:

docker version
docker info

Step 3: Prepare Your Docker Image

You have two options here:

  • Build locally and push to a remote registry (Docker Hub or AWS ECR).
  • Build directly on the EC2 instance if your build context isn’t large.

Example: Building locally & pushing to Docker Hub

Assuming you have a simple Dockerfile in your project folder:

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install --production

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]

To build and push:

docker build -t your-dockerhub-username/myapp:latest .
docker login
docker push your-dockerhub-username/myapp:latest

Step 4: Pull & Run Your Container on EC2

Back in your SSH session to EC2, pull your image:

docker pull your-dockerhub-username/myapp:latest

Run the container with port mapping:

docker run -d --name myapp-container -p 80:3000 your-dockerhub-username/myapp:latest

Here we map port 3000 inside the container to port 80 on the host’s public IP so users can reach it via http://your-ec2-public-ip.


Step 5: Automate Deployment with a Shell Script

Manual commands are great for debugging but tedious at scale. Here’s a practical shell script to update the app seamlessly:

#!/bin/bash

APP_NAME="myapp-container"
IMAGE="your-dockerhub-username/myapp:latest"

echo "Pulling latest image..."
docker pull $IMAGE

if [ $(docker ps -q -f name=$APP_NAME) ]; then
    echo "Stopping running container..."
    docker stop $APP_NAME && docker rm $APP_NAME
fi

echo "Starting container..."
docker run -d --name $APP_NAME -p 80:3000 $IMAGE

echo "Deployment complete."

Save this as deploy.sh, make executable chmod +x deploy.sh, then simply run it after pushing new images.


Important Real-world Tips & Pitfalls

Use Data Volumes for Persistent Storage

By default, anything inside a container is ephemeral. For logs or uploads needing persistence, mount a directory from the host:

docker run -d --name myapp-container -p 80:3000 \
-v /home/ec2-user/mydata:/app/data \
your-dockerhub-username/myapp:latest

Monitor Resource Usage

Docker containers share host resources—monitor CPU/memory via CloudWatch or inside the instance using docker stats.

Automate Instance Initialization with User Data

To save time when scaling or replacing instances, automate Docker installation and startup using EC2 User Data scripts so new instances are “deployment-ready” immediately.

Security Group & Firewall Settings

Don’t forget AWS security groups must allow inbound traffic on ports exposed by containers.

Avoid Running Containers as Root

For security hardening inside containers, specify non-root users in your Dockerfile or at runtime when possible.


Wrapping Up

Deploying Docker on AWS EC2 gives you versatility far beyond native ECS services while retaining full infrastructure control. The key lies in setting up clean, repeatable deployments bolstered by automation scripts — minimizing manual errors and accelerating delivery cycles.

By following this practical guide from instance setup through container orchestration basics, you’ll be confidently running resilient apps within minutes — all tailored specifically to YOUR workload needs instead of relying solely on generic scripts.

Have you deployed complex multi-container setups? Share what worked (or didn’t) in the comments below — let’s keep mastering seamless deployment together!