Deploy Docker To Aws Ec2

Deploy Docker To Aws Ec2

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

Mastering the Art of Deploying Docker Containers to AWS EC2 for Scalable Application Delivery

Most developers jump straight to ECS or EKS on AWS for Docker deployments, but sometimes direct EC2 deployment offers unmatched control and cost efficiency—here's how to master it.


Containerization revolutionized application delivery by making deployments consistent, lightweight, and scalable. On AWS, people often default to managed container services like ECS or EKS, which certainly streamline operations but come with additional overhead and cost. If you want more granular control over your infrastructure or want to optimize costs by managing containers directly on virtual machines, deploying Docker containers directly to AWS EC2 instances is a powerful approach.

In this blog post, I’ll walk you through the practical steps of how to deploy Docker containers on EC2 instances, giving you flexibility and control while retaining the benefits of containerization. By the end, you’ll have a clear understanding of setting up your environment, running containers, and scaling your application—all without the complexity of orchestrators.


Why Deploy Docker Directly on EC2?

  • Complete control over your environment: You manage every aspect of OS, networking, storage, and scaling.
  • Cost efficiency: Avoid service fees charged by ECS/EKS while using reserved or spot instances.
  • Simplify small-to-medium workloads: Managed solutions sometimes add unnecessary complexity.
  • Learning experience: Great way to understand container networking, volumes, and deployment pipelines more deeply.

Step 1: Launch an EC2 Instance with Docker

Choosing your instance

  • Select an instance type suited for your workload (t2.micro/t3.micro for small apps; m5.large for moderate production workloads).
  • Use Amazon Linux 2 or Ubuntu 20.04 LTS AMI as they offer excellent Docker compatibility.

Example: Launch an Amazon Linux 2 EC2 using AWS CLI

aws ec2 run-instances \
  --image-id ami-0c02fb55956c7d316 \ # Amazon Linux 2 AMI ID in us-east-1 (confirm region) \
  --count 1 \
  --instance-type t3.micro \
  --key-name YourKeyPair \
  --security-group-ids sg-xxxxxxxx \
  --subnet-id subnet-xxxxxxxx

Step 2: Install Docker on the EC2 Instance

SSH into your instance:

ssh -i YourKeyPair.pem ec2-user@ec2-public-ip

Update packages & install Docker:

sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
exit

Now reconnect so that your user group permissions are refreshed:

ssh -i YourKeyPair.pem ec2-user@ec2-public-ip
docker version  # Verify installation works without sudo

Step 3: Run Your First Docker Container

For demonstration, let’s run a simple NGINX container:

docker run -d -p 80:80 --name nginx-server nginx:latest

Test in browser using http://<your-ec2-public-ip>/. You should see the default NGINX welcome page — congratulations!


Step 4: Automate Container Deployment with a Dockerfile & Build

If you have a custom app (e.g., Node.js), create a Dockerfile locally:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

Build image locally:

docker build -t my-node-app .

Push it to a container registry — AWS ECR or Docker Hub:

# Using AWS ECR (set up repository first)
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account_id>.dkr.ecr.us-east-1.amazonaws.com

docker tag my-node-app:latest <account_id>.dkr.ecr.us-east-1.amazonaws.com/my-node-app:latest

docker push <account_id>.dkr.ecr.us-east-1.amazonaws.com/my-node-app:latest

Step 5: Pull and Run Custom Images on EC2

SSH into your EC2 instance again and do:

# Login to ECR from EC2 instance (install awscli if needed)
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account_id>.dkr.ecr.us-east-1.amazonaws.com

docker pull <account_id>.dkr.ecr.us-east-1.amazonaws.com/my-node-app:latest

docker run -d -p 3000:3000 my-node-app:latest

Visit http://<ec2-public-ip>:3000 to see your app in action.


Step 6: Handle Scaling Manually or with Scripts

Since there's no orchestration like ECS/EKS here, scaling means launching multiple EC2 instances with similar setups — possibly via Terraform/CloudFormation for IaC automation.

A simple way is:

  1. Create an AMI from a configured instance with Docker installed.
  2. Use User Data scripts in Auto Scaling Groups (ASG) to pull and run containers on boot.
  3. Configure an Elastic Load Balancer (ELB) to route traffic among instances.

User Data example snippet launching container on instance start:

#!/bin/bash
yum update -y
amazon-linux-extras install docker -y
service docker start
usermod -a -G docker ec2-user

# Login to ECR & pull image (replace with actual commands/keys/roles)
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account_id>.dkr.ecr.us-east-1.amazonaws.com

docker pull <account_id>.dkr.ecr.us-east-1.amazonaws.com/my-node-app:latest

docker run -d -p 3000:3000 my-node-app:latest 

Tips for Production Readiness

  • Security: Use IAM roles attached to EC2 instances instead of embedding credentials; configure security groups tightly.
  • Data Persistence: Mount EBS volumes if your app needs persistent data beyond container lifecycle.
  • Monitoring & Logs: Integrate CloudWatch agent or other tools like Prometheus/Grafana for monitoring.
  • Zero Downtime Deployments: Write scripts that spin up new containers before stopping old ones (manual blue-green deployments).

Conclusion

Deploying Docker containers directly onto AWS EC2 is a fantastic way for developers who want full control over their environment without leaning heavily on managed services like ECS/EKS. It can be more cost-effective for smaller deployments and offers learning value.

By following this guide:

  • You launched an EC2 instance ready for Docker usage.
  • Installed and ran both stateless and custom container apps.
  • Pushed images via AWS ECR so they are accessible remotely.
  • Learned manual scaling concepts.

While this setup requires some hands-on management compared to orchestration solutions, it also allows bespoke optimizations perfectly matched to your workload’s demands.

Happy containerizing on cloud VMs! If you have questions or want me to dive deeper into automation scripts or CI/CD pipelines next time, just let me know in the comments below.


Stay tuned for more practical DevOps tutorials delivered straight from personal experience.