Microservices Containerization Task
This document provides a step-by-step guide for containerizing four Node.js microservices and orchestrating them using Docker and Docker Compose. Additionally, it covers deploying the application on an AWS EC2 instance.
Containerize the Node.js microservices: User Service, Product Service, Order Service, and Gateway Service.
-
Set up Docker Compose to run the services together.
-
Deploy the containerized services on an EC2 instance.
-
Test and document the setup process.
-
Node.js installed locally.
-
Docker and Docker Compose installed.
1.1 User Service Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json . RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
1.2 Product Service Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json . RUN npm install
COPY . .
EXPOSE 3001
CMD ["npm", "start"]
1.3 Order Service Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json . RUN npm install
COPY . .
EXPOSE 3002
CMD ["npm", "start"]
1.4 Gateway Service Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json . RUN npm install
COPY . .
EXPOSE 3003
CMD ["npm", "start"]
- docker-compose.yml file to define the services and their configurations.
version: '3' services: user-service: build: ./user-service ports: - "3000:3000" networks: - app-network
product-service: build: ./product-service ports: - "3001:3001" networks: - app-network
order-service: build: ./order-service ports: - "3002:3002" networks: - app-network
gateway-service: build: ./gateway-service ports: - "3003:3003" networks: - app-network
networks: app-network: driver: bridge
- Launch an EC2 Instance
sudo apt update
sudo apt install -y docker.io
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
sudo usermod -aG docker $USER
scp -r ./microservices-containerization ec2-user@<EC2_IP>:/home/ec2-user/
SSH into the EC2 instance.
sudo docker-compose up --build
3.5 Verify the Services
Access each service using the EC2 public IP and the respective ports:
User Service: http://<EC2_IP>:3000
Product Service: http://<EC2_IP>:3001
Order Service: http://<EC2_IP>:3002
Gateway Service: http://<EC2_IP>:3003