Skip to content

Commit

Permalink
build: update docker for api gateway service
Browse files Browse the repository at this point in the history
Add `docker-compose.prod.yml` for the containerization of production application.
  • Loading branch information
ckng0221 committed Jan 5, 2024
1 parent 6ab11ba commit 1cf3903
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 2 deletions.
49 changes: 49 additions & 0 deletions apps/Dockerfile.apigateway
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM node:alpine As development

ARG service

WORKDIR /usr/src/app

COPY package*.json ./

WORKDIR /usr/src/app/apps/${service}

COPY apps/${service}/package*.json ./

WORKDIR /usr/src/app

RUN npm install

COPY . .

RUN npm run build:${service}


FROM node:alpine as production

ARG service
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
ENV SERVICE ${service}

WORKDIR /usr/src/app

COPY package*.json ./

WORKDIR /usr/src/app/apps/${service}

COPY apps/${service}/package*.json ./

WORKDIR /usr/src/app

RUN npm install --omit=dev
# RUN echo ${service}

# COPY . .

COPY --from=development /usr/src/app/dist ./dist

CMD node dist/apps/$SERVICE/server.js

# docker build --build-arg="service=apigateway" -f ./apps/Dockerfile.apigateway . -t libraryapp-apigateway
# docker run -p 8080:8080 libraryapp/apigateway
4 changes: 3 additions & 1 deletion apps/Dockerfile.service
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ RUN npm run build ${service}

FROM node:alpine as production

ARG service
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
ENV SERVICE ${service}

WORKDIR /usr/src/app

Expand All @@ -27,4 +29,4 @@ RUN npm install --omit=dev

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/apps/${service}/main"]
CMD node dist/apps/$SERVICE/main
4 changes: 3 additions & 1 deletion apps/Dockerfile.view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ RUN npm run build:ui

FROM node:alpine as production

ARG service
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
ENV SERVICE ${service}

WORKDIR /usr/src/app

Expand All @@ -35,4 +37,4 @@ RUN npm install --omit=dev

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/apps/${service}/main"]
CMD node dist/apps/$SERVICE/main
176 changes: 176 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# NOTE: The ports of internal services (eg. book, customer, payment, borrowing) are not exposed in production

services:
# app services
book:
build:
context: .
dockerfile: ./apps/Dockerfile.service
target: production
args:
service: book
env_file:
- ./apps/book/.env
environment:
- MONGODB_URI=mongodb://mongodb:27017/libraryapp-book
- RABBIT_MQ_URI=amqp://rabbitmq:5672
depends_on:
- mongodb
- rabbitmq
# ports:
# - '8001:8001'
networks:
- node-network
links:
- mongodb
- rabbitmq
customer:
build:
context: .
dockerfile: ./apps/Dockerfile.service
target: production
args:
service: customer
env_file:
- ./apps/customer/.env
environment:
- MONGODB_URI=mongodb://mongodb:27017/libraryapp-customer
depends_on:
- mongodb
# ports:
# - '8002:8002'
networks:
- node-network
links:
- mongodb
borrowing:
build:
context: .
dockerfile: ./apps/Dockerfile.service
target: production
args:
service: borrowing
env_file:
- ./apps/borrowing/.env
environment:
- MONGODB_URI=mongodb://mongodb:27017/libraryapp-borrowing
- RABBIT_MQ_URI=amqp://rabbitmq:5672
depends_on:
- mongodb
- rabbitmq
# ports:
# - '8003:8003'
networks:
- node-network
links:
- mongodb
- rabbitmq
payment:
build:
context: .
dockerfile: ./apps/Dockerfile.service
target: production
args:
service: payment
env_file:
- ./apps/payment/.env
environment:
- MONGODB_URI=mongodb://mongodb:27017/libraryapp-payment
- RABBIT_MQ_URI=amqp://rabbitmq:5672
depends_on:
- mongodb
- rabbitmq
# ports:
# - '8004:8004'
networks:
- node-network
links:
- mongodb
- rabbitmq
notification:
build:
context: .
dockerfile: ./apps/Dockerfile.service
target: production
args:
service: notification
env_file:
- ./apps/notification/.env
environment:
- RABBIT_MQ_URI=amqp://rabbitmq:5672
depends_on:
- rabbitmq
# ports:
# - '8005:8005'
networks:
- node-network
links:
- rabbitmq
view:
build:
context: .
dockerfile: ./apps/Dockerfile.view
args:
service: view
env_file:
- ./apps/view/.env
depends_on:
- book
- customer
- borrowing
- payment
- notification
ports:
- '8000:8000'
networks:
- node-network
apigateway:
build:
context: .
dockerfile: ./apps/Dockerfile.apigateway
target: production
args:
service: apigateway
env_file:
- ./apps/apigateway/.env
depends_on:
- view
ports:
- '8080:8080'
networks:
- node-network
links:
- book
- customer
- borrowing
- payment
environment:
- BASEURL_BOOK=http://book:8001
- BASEURL_CUSTOMER=http://customer:8002
- BASEURL_BORROWING=http://borrowing:8003
- BASEURL_PAYMENT=http://payment:8004

# Dependencies
rabbitmq:
image: rabbitmq
ports:
- '5672:5672'
networks:
- node-network
mongodb:
image: mongo:6
volumes:
- mongodb_data_container:/data/db
ports:
- '27017:27017'
networks:
- node-network

volumes:
mongodb_data_container:
networks:
node-network:
driver: bridge
# docker compose -f docker-compose.prod.yml up -d
# docker compose -f docker-compose.prod.yml up -d --build
# docker compose -f docker-compose.prod.yml down
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
# app services
book:
image: libraryapp-dev-book
build:
context: .
dockerfile: ./apps/Dockerfile.service
Expand All @@ -27,6 +28,7 @@ services:
- mongodb
- rabbitmq
customer:
image: libraryapp-dev-customer
build:
context: .
dockerfile: ./apps/Dockerfile.service
Expand All @@ -50,6 +52,7 @@ services:
links:
- mongodb
borrowing:
image: libraryapp-dev-borrowing
build:
context: .
dockerfile: ./apps/Dockerfile.service
Expand All @@ -76,6 +79,7 @@ services:
- mongodb
- rabbitmq
payment:
image: libraryapp-dev-payment
build:
context: .
dockerfile: ./apps/Dockerfile.service
Expand All @@ -102,6 +106,7 @@ services:
- mongodb
- rabbitmq
notification:
image: libraryapp-dev-notification
build:
context: .
dockerfile: ./apps/Dockerfile.service
Expand All @@ -125,6 +130,7 @@ services:
links:
- rabbitmq
view:
image: libraryapp-dev-view
build:
context: .
dockerfile: ./apps/Dockerfile.view
Expand All @@ -134,6 +140,12 @@ services:
command: npm run start:dev view
env_file:
- ./apps/view/.env
depends_on:
- book
- customer
- borrowing
- payment
- notification
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
Expand All @@ -142,6 +154,37 @@ services:
networks:
- node-network

apigateway:
image: libraryapp-dev-apigateway
build:
context: .
dockerfile: ./apps/Dockerfile.apigateway
target: development
args:
service: apigateway
command: npm run dev:apigateway
env_file:
- ./apps/apigateway/.env
depends_on:
- view
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- '8080:8080'
networks:
- node-network
links:
- book
- customer
- borrowing
- payment
environment:
- BASEURL_BOOK=http://book:8001
- BASEURL_CUSTOMER=http://customer:8002
- BASEURL_BORROWING=http://borrowing:8003
- BASEURL_PAYMENT=http://payment:8004

# Dependencies
rabbitmq:
image: rabbitmq
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"build:ui": "turbo build --filter=ui",
"build:apigateway": "turbo build --filter=apigateway",
"lint:ui": "turbo lint --filter=ui",
"dev:apigateway": "turbo dev --filter=apigateway",
"lint:apigateway": "turbo lint --filter=apigateway"
},
"dependencies": {
Expand Down

0 comments on commit 1cf3903

Please sign in to comment.