Skip to content

SHArahul/Microservices-Task

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Skill-Test-1: Microservices-Task: Containerize three services: user-service | product-service | gateway-service

Containerize a microservices-based application using Node.js, Docker, and Docker Compose.

Microservices Task - Dockerized Node.js Project

Overview

This project demonstrates a microservices architecture using Node.js, Docker, and Docker Compose. It includes three services:

Service Description Port

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

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

  1. Build images docker compose build

  2. Start services docker compose up -d

  3. Stop services docker compose down

  4. View logs docker compose logs -f gateway-service

  5. Test APIs

Users API:

http://localhost:3000/users

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

Make sure network exists (safe to run even if already created)

docker network create microservices-net

Run user-service

docker run -d --name user-service --network microservices-net -p 3000:3000 user-service

Run product-service

docker run -d --name product-service --network microservices-net -p 3001:3001 product-service

Run order-service

docker run -d --name order-service --network microservices-net -p 3002:3002 order-service

Run gateway-service

docker run -d --name gateway-service --network microservices-net -p 3003:3003 gateway-service


Testing the services locally after containerization

Steps:

Services and Endpoints

User Service

  • Base URL: http://localhost:3000
  • Endpoints:

Product Service

  • Base URL: http://localhost:3001
  • Endpoints:

Order Service

  • Base URL: http://localhost:3002
  • Endpoints:

Gateway Service

  • Base URL: http://localhost:3003/api
  • Endpoints:
    • Users:
      curl http://localhost:3003/api/users
      
    • Products:
      curl http://localhost:3003/api/products
      

Screenshots:

from creation of containers from Dockerfile to docker-compose.yml (multi-container creation) along with network and step-by-step methods

user-service docker image building user-service docker image

docker run user-service docker run for user-service

Container running locally: running container locally for user-service

building product-service building product-service docker image

Running product-service running container for product service

running container locally running container locally for product-service

building gateway service building gateway-service image

running container locally for gateway running container locally for gateway-api

created docker network for containers created network for docker containers

docker network with connected containers docker network with connect containers

gateway container running locally for user-service running container for gateway-service-users

network inspect docker inspect for microservices containers

running docker compose running docker-compose

building docker compose building docker compose

Testing other microservices from gateway service testing microservices from gateway service


-Rahul Sharma

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 87.1%
  • Dockerfile 12.9%