Skip to content

Deploy applications on AWS EC2 using Docker for seamless and scalable containerized deployments. This repository provides scripts and configuration files to automate the process of setting up Docker containers on an Amazon EC2 instance, making it easier to host and manage your applications in the cloud.

Notifications You must be signed in to change notification settings

HarshwardhanPatil07/EC2-Docker-Deployment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EC2 Docker Deployment Guide

I tried it myself and provided all the steps for you as well as me so both of us can learn. Let's learn together! You can check application running below.

Cutie

Prerequisites

  • AWS EC2 instance launched
  • Key pair file (docker-server.pem) downloaded
  • Basic understanding of Docker and AWS

Table of Contents

  1. EC2 Instance Setup
  2. Docker Installation
  3. MongoDB Setup
  4. Application Deployment
  5. Security Group Configuration
  6. Verification

EC2 Instance Setup

1. Prepare SSH Key

Move your key pair file to the SSH directory and set proper permissions:

mv Downloads/docker-server.pem ~/.ssh/
ls -l .ssh/docker-server.pem
chmod 400 .ssh/docker-server.pem

2. Connect to EC2 Instance

ssh -i ~/.ssh/docker-server.pem ec2-user@<public-ip-address>

EC2 Connection

Docker Installation

1. Update System Packages

sudo yum update

2. Install Docker

sudo yum install docker

3. Start Docker Service

sudo service docker start
ps aux | grep docker

4. Add User to Docker Group

sudo usermod -aG docker $USER

Note: You need to exit and reconnect for the group changes to take effect.

exit
ssh -i ~/.ssh/docker-server.pem ec2-user@<public-ip-address>

5. Verify Docker Installation

docker ps
docker login

MongoDB Setup

1. Create Docker Network

docker network create mongo-network

2. Start MongoDB Container

docker run -d \
  -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=password \
  --name mongodb \
  --net mongo-network \
  mongo

3. Start Mongo Express (Web UI)

docker run -d \
  -p 8081:8081 \
  -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
  -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
  --net mongo-network \
  --name mongo-express \
  -e ME_CONFIG_MONGODB_SERVER=mongodb \
  -e ME_CONFIG_MONGODB_URL=mongodb://mongodb:27017 \
  mongo-express

4. Access Mongo Express

Navigate to: http://<your-ec2-public-ip>:8081

Credentials:

  • Username: admin
  • Password: pass

Database Setup:

  • Create database: user-account
  • Create collection: users

Application Deployment

1. Deploy Application Container

docker run -d \
  -p 3000:3000 \
  --net mongo-network \
  --name my-app \
  harshwardhan07/my-app:1.0

2. Verify Deployment

docker images
docker ps

Docker Images

Security Group Configuration

Add an inbound rule for port 3000 (same for port 8081 and 22) in your EC2 Security Group:

  • Type: Custom TCP
  • Port: 3000
  • Source: 0.0.0.0/0 (or your specific IP range)

Inbound Rule Configuration

Verification

After configuring the Security Group, access your application:

Application URL: http://<your-ec2-public-ip>:3000

🎉 Yay! now running on EC2!

Successfully Running Application

EC2 Instance Overview

EC2 Instance

Troubleshooting

Common Issues

  1. Permission denied for SSH key:

    • Ensure key permissions are set to 400: chmod 400 ~/.ssh/docker-server.pem
  2. Docker commands require sudo:

    • Make sure you've added your user to the docker group and reconnected
  3. Application not accessible:

    • Verify Security Group inbound rules
    • Check if containers are running: docker ps
  4. MongoDB connection issues:

    • Ensure all containers are on the same network
    • Verify MongoDB container is running

Note: Replace <your-ec2-public-ip> with your actual EC2 instance's public IP address throughout the commands.

About

Deploy applications on AWS EC2 using Docker for seamless and scalable containerized deployments. This repository provides scripts and configuration files to automate the process of setting up Docker containers on an Amazon EC2 instance, making it easier to host and manage your applications in the cloud.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published