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.
- You Can Access the docker Image from https://hub.docker.com/r/harshwardhan07/my-app/tags
- AWS EC2 instance launched
- Key pair file (
docker-server.pem) downloaded - Basic understanding of Docker and AWS
- EC2 Instance Setup
- Docker Installation
- MongoDB Setup
- Application Deployment
- Security Group Configuration
- Verification
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.pemssh -i ~/.ssh/docker-server.pem ec2-user@<public-ip-address>sudo yum updatesudo yum install dockersudo service docker start
ps aux | grep dockersudo usermod -aG docker $USERNote: 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>docker ps
docker logindocker network create mongo-networkdocker run -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
--name mongodb \
--net mongo-network \
mongodocker 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-expressNavigate to: http://<your-ec2-public-ip>:8081
Credentials:
- Username:
admin - Password:
pass
Database Setup:
- Create database:
user-account - Create collection:
users
docker run -d \
-p 3000:3000 \
--net mongo-network \
--name my-app \
harshwardhan07/my-app:1.0docker images
docker psAdd 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)
After configuring the Security Group, access your application:
Application URL: http://<your-ec2-public-ip>:3000
🎉 Yay! now running on EC2!
-
Permission denied for SSH key:
- Ensure key permissions are set to 400:
chmod 400 ~/.ssh/docker-server.pem
- Ensure key permissions are set to 400:
-
Docker commands require sudo:
- Make sure you've added your user to the docker group and reconnected
-
Application not accessible:
- Verify Security Group inbound rules
- Check if containers are running:
docker ps
-
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.




