AWS Builder Center

From Localhost to AWS: Automating a MERN Stack Deployment with Docker, GitHub Actions, and EC2

Struggling with manual cloud deployments? Discover how to professionalize your workflow by automating a MERN stack application deployment from your local machine to AWS EC2. This comprehensive guide covers containerizing with Docker, automating pipelines with GitHub Actions, and securing your cloud environment for a reliable, production-ready CI/CD process.

Introduction Getting an application to run successfully on your local development machine is a great milestone, but the real test begins when you want to ship it to production. Manually SSHing into an AWS EC2 instance, pulling code, and restarting node processes is tedious and prone to human error. To build a professional workflow, modern cloud developers use CI/CD (Continuous Integration and Continuous Deployment) pipelines. In this guide, we will walk through how to containerize a full-stack application with Docker, automate testing and deployment using GitHub Actions, and host it securely on AWS EC2.

Architecture Overview Our automated pipeline ensures that every time code is merged into the main branch, GitHub Actions builds a Docker container and deploys it directly to our cloud server.
[ Developer Push ] ---> ( GitHub Repository )
|
v (Trigger GitHub Actions CI/CD)
[ Build Docker Image ]
|
v
[ SSH into AWS EC2 ]
|
v
[ Run Container on Port 80/443 ]

Key Tools Used: Docker: Packages our application and its exact dependencies into a lightweight, portable container. GitHub Actions: Automates our build, test, and deployment workflow right from our repository. AWS EC2 (Elastic Compute Cloud): Provides reliable, scalable virtual server compute power in the cloud.

Step 1: Containerizing the Application with Docker To ensure consistency between your local machine and your AWS server, you need a Dockerfile in the root of your repository. Here is a clean multi-stage production Dockerfile configuration for a Node.js/Express backend:
# Stage 1: Build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .

# Stage 2: Production Run
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production

COPY package*.json ./
RUN npm ci --only=production

COPY --from=builder /app/dist ./dist

EXPOSE 5000
CMD ["node", "dist/index.js"]

Don't forget to create a .dockerignore file as well to keep your images lightweight:
node_modules
Dockerfile
.git
.env
README.md

Step 2: Preparing the AWS EC2 Environment 1. Before setting up your automation, your cloud server needs to be ready to run containers. 2. Launch an AWS EC2 instance (e.g., t3.medium running Ubuntu Server 24.04 LTS). 3. Configure your Security Group to allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). 4. Connect via SSH and install Docker and Docker Compose on the instance:
sudo apt-get update
sudo apt-get install -y docker.io docker-compose-v2
sudo usermod -aG docker $USER
newgrp docker

Step 3: Configuring GitHub Actions for Automated Deployment Instead of manually deploying, let's create a GitHub Actions workflow file. Create a file in your project directory at .github/workflows/deploy.yml:
name: CI/CD Pipeline to AWS EC2

on:
push:
branches: [ "main" ]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: your-dockerhub-username/my-app:latest

- name: Deploy to AWS EC2 via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
docker pull your-dockerhub-username/my-app:latest
docker stop my-running-app || true
docker rm my-running-app || true
docker run -d --name my-running-app -p 5000:5000 your-dockerhub-username/my-app:latest

Adding GitHub Secrets: To keep your infrastructure secure, never hardcode passwords or keys in your repository. Go to your GitHub
repository settings under Secrets and variables > Actions, and add:
  • DOCKER_HUB_USERNAME and DOCKER_HUB_ACCESS_TOKEN
  • EC2_HOST (Your EC2 Public IPv4 address)
  • EC2_SSH_KEY (The private .pem key content used to access your EC2 instance)

Best Practices for Production EC2 Deployments

  • Use Reverse Proxies (Nginx): Place Nginx in front of your Node.js container to handle SSL certificates (HTTPS via Let's Encrypt), static file caching, and secure traffic routing.
  • Environment Variables: Never commit .env files into version control. Pass runtime environment variables securely using Docker environment files (--env-file) directly on the EC2 instance.
  • CloudWatch Monitoring: Set up basic system metric alarms on AWS CloudWatch to monitor CPU utilization and memory spikes on your instance.

Conclusion By moving away from manual server configurations and building a streamlined CI/CD pipeline with Docker and GitHub Actions, you eliminate deployment bottlenecks and establish a professional production workflow. What tools do you prefer for your deployment pipelines—GitHub Actions, AWS CodePipeline, or something else? Let’s discuss in the comments below!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article