This is a simple Node.js application that demonstrates building and running an app using Docker Compose alongside MongoDB and Mongo Express, with support for deploying the app to AWS ECR and EC2.
- Node.js application (
my-app) - MongoDB database
- Mongo Express web interface for MongoDB
- Fully containerized setup with Docker Compose
- CI/CD pipeline for ECR and EC2 deployments
-
AWS account with:
- ECR repository
- IAM user with push/pull permissions
-
SSH access to the EC2 instance
- Clone the repository:
git clone <repository-url>
cd <repository-folder>- Create a
.envfile to store your secrets:
MONGO_USERNAME=your-mongo-username
MONGO_PASSWORD=your-mongo-password
DB_PASSWORD=your-app-db-passwordUse Docker Compose to build and run all services:
docker compose --env-file .env up --buildmy-appwill be built from the local Dockerfile.mongodbandmongo-expressimages will be pulled from Docker Hub.
Access services:
- Node.js app: http://localhost:3000
- Mongo Express: http://localhost:8081
- MongoDB runs internally on port 27017.
Stop services:
docker compose down- Build the Docker image and tag it with the ECR repository URI:
docker build -t my-app:latest ./app
docker tag my-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<region>.amazonaws.com/my-app:latest- Login to ECR:
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<region>.amazonaws.com- Push the image:
docker push <AWS_ACCOUNT_ID>.dkr.ecr.<region>.amazonaws.com/my-app:latestTip: For CI/CD, use GitHub Actions with secrets for AWS credentials and dynamic image tagging.
- SSH into your EC2 instance:
ssh -i <key.pem> ec2-user@<EC2_PUBLIC_IP>- Pull the latest image from ECR:
docker login -u AWS -p $(aws ecr get-login-password --region <region>) <AWS_ACCOUNT_ID>.dkr.ecr.<region>.amazonaws.com
docker pull <AWS_ACCOUNT_ID>.dkr.ecr.<region>.amazonaws.com/my-app:latest- Update
docker-compose.ymlon EC2 to reference the ECR image:
my-app:
image: <AWS_ACCOUNT_ID>.dkr.ecr.<region>.amazonaws.com/my-app:latest
ports:
- 3000:3000
environment:
DB_PASSWORD: ${DB_PASSWORD}- Run Docker Compose:
export DB_PASSWORD=<your-app-db-password>
docker compose up -d- MongoDB and Mongo Express can either be local on EC2 or connected via your existing Docker Compose setup.
- To update the app later:
docker compose pull
docker compose up -d- Secrets: Never commit
.envfiles with passwords. Use GitHub Secrets for CI/CD. - Dynamic versioning: Use image tags like
1.0.${GITHUB_RUN_NUMBER}for unique builds. - CI/CD: Your GitHub Actions workflow can automate build, push to ECR, and trigger deployment on EC2 via SSH.
This README now covers local development, ECR deployment, and EC2 deployment, making it ready for both dev and production workflows.