Skill-Test-1: Microservices-Task: Containerize three services: user-service | product-service | gateway-service
Microservices Task - Dockerized Node.js Project
This project demonstrates a microservices architecture using Node.js, Docker, and Docker Compose. It includes three services:
user-service Manages user data 3000 product-service Manages product data 3001 gateway-service API Gateway routing requests to backend services 3003
All services are containerized using Docker and connected via a shared network (microservices-net).
Folder Structure
Microservices-Task/
│── docker-compose.yml
└── microservices/
├── user-service/
│ ├── Dockerfile
│ └── app.js
├── product-service/
│ ├── Dockerfile
│ └── app.js
└── gateway-service/
├── Dockerfile
└── app.js
Prerequisites
Docker Desktop installed and running
Docker Compose (comes with Docker Desktop)
Node.js (for building Docker images if needed locally)
Dockerfile Overview
Each microservice contains a Dockerfile:
Example structure:
Base image
FROM node:18-alpine
Set working directory
WORKDIR /app
Copy package files and install dependencies
COPY package*.json ./
RUN npm install
Copy project files
COPY . .
Expose service port
EXPOSE 3000 # Change per service
Start the application
CMD ["node", "app.js"]
Each service exposes its own port (user-service:3000, product-service:3001, gateway-service:3003).
docker-compose.yml defines all services, their build paths, ports, and network:
Network: microservices-net (bridge network)
Depends_on: Ensures gateway starts after backend services.
Run all services together using Docker Compose.
Commands
-
Build images docker compose build
-
Start services docker compose up -d
-
Stop services docker compose down
-
View logs docker compose logs -f gateway-service
-
Test APIs
Users API:
Products API:
http://localhost:3001/products
Gateway routes:
http://localhost:3003/api/users http://localhost:3003/api/products
Healthcheck (Optional)
Verify microservices connectivity via gateway:
curl http://localhost:3003/api/users curl http://localhost:3003/api/products curl http://localhost:3003/api/orders
Returns JSON if services are healthy.
The requirement to create network for containers to run the gateway microservice to communicate with other services as containers are network-isolated:
it is required in case of containers are created with multiple Dockerfile for communicate between containers
commands
docker network create microservices-net
docker run -d --name user-service --network microservices-net -p 3000:3000 user-service
docker run -d --name product-service --network microservices-net -p 3001:3001 product-service
docker run -d --name order-service --network microservices-net -p 3002:3002 order-service
docker run -d --name gateway-service --network microservices-net -p 3003:3003 gateway-service
- Base URL:
http://localhost:3000
- Endpoints:
- List Users:
Or open in your browser: http://localhost:3000/users
curl http://localhost:3000/users
- List Users:
- Base URL:
http://localhost:3001
- Endpoints:
- List Products:
Or open in your browser: http://localhost:3001/products
curl http://localhost:3001/products
- List Products:
- Base URL:
http://localhost:3002
- Endpoints:
- List Orders:
Or open in your browser: http://localhost:3002/orders
curl http://localhost:3002/orders
- List Orders:
- Base URL:
http://localhost:3003/api
- Endpoints:
- Users:
curl http://localhost:3003/api/users
- Products:
curl http://localhost:3003/api/products
- Users:
from creation of containers from Dockerfile to docker-compose.yml (multi-container creation) along with network and step-by-step methods
running container locally for gateway
created docker network for containers
docker network with connected containers
gateway container running locally for user-service
Testing other microservices from gateway service
-Rahul Sharma