Skip to content

Commit

Permalink
build: 개발용 docker compose 파일 (#67)
Browse files Browse the repository at this point in the history
* build: dev dockerfile

* docs: `.env` 예제 추가

* build: compose 내에서 DB 연결

컨테이너간에는 컨테이너명으로 DB에 접근해야 함, 참고: https://old.reddit.com/r/docker/comments/jsshgu/help_nodjs_mysql_dockercompose_connect/gc1ktqr/

* docs: docker compose 사용법 정리
  • Loading branch information
scarf005 authored Oct 8, 2024
1 parent ff53fe3 commit 8ec909b
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 42seoul_club_42jiphyeonjeon_dev 캔버스 참고:
# https://42born2code.slack.com/canvas/C03BAMF3727

MYSQL_DATABASE=jip_serv
MYSQL_USER=saseo
MYSQL_PASSWORD=
MYSQL_ROOT_PASSWORD=
MYSQL_HOST="127.0.0.1"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
### Node ###
node_modules/

.env*
!.env.example
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,67 @@
# backend-nest

6차 개발팀은 nestjs 로 새로 작성하기로 했습니다

## 개발 환경 설정

### 환경 변수

[.env.example](./.env.example)의 내용을 참고하여 루트 디렉토리에 `.env` 파일을 생성합니다.

### 백엔드

```sh
$ brew install corepack
$ corepack enable
```

[corepack](https://github.com/nodejs/corepack?tab=readme-ov-file#how-to-install)으로 pnpm을 설치합니다.

> [!NOTE]
> mac OS 환경에서는 `corepack` 패키지가 `yarn``pnpm` 패키지와 충돌이 있을 수 있습니다.
> 이 경우 두 패키지를 모두 삭제하고 `corepack`을 설치해야 합니다.
```sh
$ pnpm install
```

프로젝트 의존성을 설치합니다.

### DB

```sh
$ docker compose -f compose-dev.yml up --remove-orphans
# (다른 터미널에서)
$ docker compose -f compose-dev.yml exec database /bin/sh
# mysql -h 127.0.0.1 -P 3306 -u root -p
mysql> use jip_serv;
mysql> source /내려받은/DB/덤프/파일/경로.sql;
```

초기 실행 시 [DB 덤프 파일](https://discord.com/channels/1277878039090565139/1277878039593619468/1278599701532377088)을 사용해 [데이터베이스를 초기화해야 합니다.](https://stackoverflow.com/questions/17666249/how-to-import-an-sql-file-using-the-command-line-in-mysql)

```
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jip_serv |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
```

다음과 같이 데이터베이스가 생성되어 있는지 확인합니다.

### docker compose 실행

```sh
$ pnpm dev
```

위 명령어로 개발용 docker compose를 실행합니다.

![](./swagger.webp)

<http://localhost:3000/api> 경로에 접근하여 API 명세가 올바르게 표시되는지 확인합니다.
6 changes: 6 additions & 0 deletions backend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:22-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

WORKDIR /app
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
},
"packageManager": "[email protected]+sha512.4abf725084d7bcbafbd728bfc7bee61f2f791f977fd87542b3579dcb23504d170d46337945e4c66485cd12d588a0c0e570ed9c477e7ccdd8507cf05f3f92eaca"
}
29 changes: 29 additions & 0 deletions compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
database:
platform: linux/x86_64
container_name: database
image: mysql:8.0
environment:
- TZ=Asia/Seoul

ports:
- 3306:3306

env_file: .env

backend:
container_name: backend
restart: on-failure
build:
context: backend
dockerfile: Dockerfile.dev

entrypoint: ["pnpm", "run", "start:dev"]
volumes:
- ./backend:/app
- ./backend/logs:/app/backend/logs
ports:
- 3000:3000
environment:
- MYSQL_HOST=database
env_file: .env
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "6차 개발팀은 nestjs 로 새로 작성하기로 했습니다",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"dev": "docker compose -f compose-dev.yml up --remove-orphans"
},
"keywords": [],
"author": "",
Expand Down
Binary file added swagger.webp
Binary file not shown.

0 comments on commit 8ec909b

Please sign in to comment.