Create an EC2 machine on AWS
Sign in to your AWS account, go to the EC2 dashboard and launch an instance with the following configuration.
SSH using EC2 instance connect option, or MobaXterm or Putty
Install Docker
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
Install git
sudo yum install git -y
Write Dockerfile
# BUILD STAGE
FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# DEPLOY STAGE
FROM node:18-alpine
WORKDIR /app
# ✅ Corrected line
COPY --from=build /app/dist ./dist
RUN npm install -g http-server
# EXPOSE PORT
EXPOSE 8080
CMD ["http-server", "dist", "-p", "8080", "-c-1"]
Clone Application Source Code
git clone https://github.com/awais684/Dockerize-node.js-app.git
cp Dockerfile <repditory name>
cd <repository name>
Build Docker image & Run container
docker build -t <image name> .
docker images
docker run -d -p 80:8080 --name cont1 <image name>
Top comments (1)
Great explanation!