NestJS TDD (입고 알림) - 5. Run application on Docker

이게되네·2021년 9월 20일
0

5-1. Dockerfile 작성

dockerfile을 작성한다.

# First stage: build and test
FROM node:10-alpine as nodebuild    # Define the base image
WORKDIR /app                        # Define where we put the files
COPY . .                            # Copy all files from local host folder to image
RUN npm install && \                # Install dependencies
    npm run build && \              # Build the solution
    npm run test && \               # Run the tests
    npm run coverage                # Report on coverage

# Second stage: assemble the runtime image
FROM node:10-alpine as noderun      # Define base image
WORKDIR /app                        # Define work directory
COPY --from=nodebuild /app/dist/src/ ./ # Copy binaries resulting from stage build
COPY package*.json ./               # Copy dependency registry
RUN npm install --only=prod         # Install only production dependencies
EXPOSE 8000
ENTRYPOINT node /app/index.js       # Define how to start the app.

5-2. image build

test를 돌릴 image와 runtime 이미지를 따로 build한다.

> docker build . -t noti
> docker build --target nodebuild . -t noti-test:latest

5-3. run on container

그리고 test Image를 container로 실행하고, 결과 파일들을 받아온다.

> docker run --name noti-test noti-test
> docker cp noti-test:/app/coverage ./results

그리고 runtime image를 container로 실행한다.

> docker run -d --name noti-run noti

참고

Using Docker Multi-Stage Builds To Build And Test Applications

5-4. 이러한 도커 수행 과정을 docker-compose로 구성해보자.

version: '3.8'

services:
  test:
    container_name: noti-test
    image: noti-test:latest
    build:
      context: .
      target: nodebuild
      dockerfile: ./Dockerfile
  run:
    container_name: noti
    image: noti:latest
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 3000:3000
    networks:
      - noti-network
    restart: unless-stopped

networks:
  noti-network:

실행은 background에서 하도록 하자.

> docker-compose up -d test
> docker-compose up -d run

궁금한 점

❓ 아니 그런데 test를 하고 test result를 복사하려는데 어떻게 자동화 하지..?

profile
BackEnd Developer

0개의 댓글