From 2479fb8402e5e2d80c57aa9829c939d3c0dae4be Mon Sep 17 00:00:00 2001 From: Doyeon Kim Date: Thu, 29 Aug 2024 22:45:17 +0900 Subject: [PATCH 1/5] =?UTF-8?q?:hammer:=20chore:=20PostGIS=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20#14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PostgreSQL 이미지를 PostGIS 확장을 포함한 이미지로 변경 - 새로운 DB 생성시 PostGIS를 활성화하는 sql을 실행 - member, restaurant에 location 컬럼 추가 --- docker-compose.dev.yml | 3 ++- docker-compose.yml | 3 ++- docker-entrypoint-initdb.d/init.sql | 1 + src/entities/member.entity.ts | 9 ++++++++- src/entities/restaurant.entity.ts | 9 ++++++++- 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 docker-entrypoint-initdb.d/init.sql diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 96f04a5..4b3fe65 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -1,11 +1,12 @@ services: db: container_name: matjum_db - image: postgres:16.4-alpine + image: postgis/postgis:16-3.4-alpine restart: always env_file: - .env volumes: + - ./init.sql:/docker-entrypoint-initdb.d/init.sql - db_data_dev:/var/lib/postgresql/data ports: - '${POSTGRES_PORT}:5432' # Host:Container 내부 포트 diff --git a/docker-compose.yml b/docker-compose.yml index 67ecdb1..43763f0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,12 @@ services: db: container_name: matjum_db - image: postgres:16.4-alpine + image: postgis/postgis:16-3.4-alpine restart: always env_file: - .env volumes: + - ./init.sql:/docker-entrypoint-initdb.d/init.sql - db_data:/var/lib/postgresql/data ports: - '${POSTGRES_PORT}:5432' # Host:Container 내부 포트 diff --git a/docker-entrypoint-initdb.d/init.sql b/docker-entrypoint-initdb.d/init.sql new file mode 100644 index 0000000..04892e2 --- /dev/null +++ b/docker-entrypoint-initdb.d/init.sql @@ -0,0 +1 @@ +CREATE EXTENSION IF NOT EXISTS postgis; \ No newline at end of file diff --git a/src/entities/member.entity.ts b/src/entities/member.entity.ts index afd1894..eb5a11e 100644 --- a/src/entities/member.entity.ts +++ b/src/entities/member.entity.ts @@ -1,4 +1,4 @@ -import { Column, Entity, OneToMany } from 'typeorm'; +import { Column, Entity, OneToMany, Point } from 'typeorm'; import { BaseModel } from './base-model.entity'; import { Review } from './review.entity'; @@ -21,6 +21,13 @@ export class Member extends BaseModel { @Column({ type: 'real', nullable: true }) lat: number; + /** + * 4326 - WGS 84 좌표계, 위도와 경도를 도(degrees) 단위로 표현함, 지구의 곡률을 고려하여 정확하게 거리를 계산하기 위해 필요함 + * 기본값은 0, 평면 좌표계 + * */ + @Column({ type: 'geometry', nullable: true, srid: 4326 }) + location: Point; + @Column({ default: false }) isRecommendationEnabled: boolean; diff --git a/src/entities/restaurant.entity.ts b/src/entities/restaurant.entity.ts index caa28fc..dc41dae 100644 --- a/src/entities/restaurant.entity.ts +++ b/src/entities/restaurant.entity.ts @@ -1,4 +1,4 @@ -import { Column, Entity, OneToMany, Unique } from 'typeorm'; +import { Column, Entity, OneToMany, Point, Unique } from 'typeorm'; import { BaseModel } from './base-model.entity'; import { Review } from './review.entity'; @@ -26,6 +26,13 @@ export class Restaurant extends BaseModel { @Column({ type: 'real', nullable: true }) lat: number; + /** + * 4326 - WGS 84 좌표계, 위도와 경도를 도(degrees) 단위로 표현함, 지구의 곡률을 고려하여 정확하게 거리를 계산하기 위해 필요함 + * 기본값은 0, 평면 좌표계 + * */ + @Column({ type: 'geometry', nullable: true, srid: 4326 }) + location: Point; + @Column({ type: 'real', default: 0 }) // review 가 없을 경우 0으로 설정 rating: number; From 2424016a1d2b6a44837044d01c79c2c50b5e5654 Mon Sep 17 00:00:00 2001 From: Jisu Date: Fri, 30 Aug 2024 01:29:31 +0900 Subject: [PATCH 2/5] =?UTF-8?q?:hammer:=20chore:=20PostGIS=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=EB=90=9C=20Dockerfile=20=EC=83=9D=EC=84=B1=20#18?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 플랫폼 이슈로 multi-platform 지원하는 PostgreSQL 이미지에서 Dockerfile 생성 - 최소 설정만 넣은 Dockerfile 입니다. --- Dockerfiles/db/Dockerfile | 24 ++++++++++++++++++++++++ Dockerfiles/db/initdb-postgis.sh | 7 +++++++ Dockerfiles/db/update-postgis.sh | 16 ++++++++++++++++ Dockerfiles/server/Dockerfile | 19 +++++++++++++++++++ docker-compose.dev.yml | 3 ++- docker-compose.yml | 7 +++---- docker-entrypoint-initdb.d/init.sql | 1 - 7 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 Dockerfiles/db/Dockerfile create mode 100644 Dockerfiles/db/initdb-postgis.sh create mode 100644 Dockerfiles/db/update-postgis.sh create mode 100644 Dockerfiles/server/Dockerfile delete mode 100644 docker-entrypoint-initdb.d/init.sql diff --git a/Dockerfiles/db/Dockerfile b/Dockerfiles/db/Dockerfile new file mode 100644 index 0000000..e4175f8 --- /dev/null +++ b/Dockerfiles/db/Dockerfile @@ -0,0 +1,24 @@ +FROM postgres:16.4-bullseye + +# PostGIS 주 버전 설정 +ENV POSTGIS_MAJOR=3 +ENV POSTGIS_VERSION=3.4.2+dfsg-1.pgdg110+1 + +# 필요한 패키지 설치 및 PostGIS 설치 +# 인증서 오류 발생할 경우 ca-certificates 패키지 설치 +RUN apt-get update \ + && apt-cache showpkg postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR \ + && apt-get install -y --no-install-recommends \ + postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION \ + postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR-scripts \ + && rm -rf /var/lib/apt/lists/* + +RUN mkdir -p /docker-entrypoint-initdb.d +# PostGIS 초기화 스크립트 추가 +COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/10_postgis.sh +# PostGIS 업데이트 스크립트 추가 +COPY ./update-postgis.sh /usr/local/bin + +RUN chmod +x /docker-entrypoint-initdb.d/10_postgis.sh + +ENV POSTGRES_DB=gis diff --git a/Dockerfiles/db/initdb-postgis.sh b/Dockerfiles/db/initdb-postgis.sh new file mode 100644 index 0000000..c5e12d9 --- /dev/null +++ b/Dockerfiles/db/initdb-postgis.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e + +echo "Loading PostGIS extensions into $POSTGRES_DB" +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE EXTENSION IF NOT EXISTS postgis; +EOSQL diff --git a/Dockerfiles/db/update-postgis.sh b/Dockerfiles/db/update-postgis.sh new file mode 100644 index 0000000..48761f1 --- /dev/null +++ b/Dockerfiles/db/update-postgis.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -e + +# Perform all actions as $POSTGRES_USER +export PGUSER="$POSTGRES_USER" + +# PostGIS 버전 설정 +POSTGIS_VERSION="${POSTGIS_VERSION%%+*}" + +# $POSTGRES_DB에 PostGIS 설치, 업데이트 +echo "Installing PostGIS extension to $POSTGRES_DB" +psql --dbname="$POSTGRES_DB" -c " + -- Install PostGIS (includes raster) + CREATE EXTENSION IF NOT EXISTS postgis VERSION '$POSTGIS_VERSION'; + ALTER EXTENSION postgis UPDATE TO '$POSTGIS_VERSION'; +" diff --git a/Dockerfiles/server/Dockerfile b/Dockerfiles/server/Dockerfile new file mode 100644 index 0000000..aeb50b2 --- /dev/null +++ b/Dockerfiles/server/Dockerfile @@ -0,0 +1,19 @@ +FROM node:20-alpine AS builder + +WORKDIR /usr/app + +COPY . . + +RUN npm i && npm run build && npm prune --production + + +FROM node:20-alpine + +WORKDIR /usr/app + +# 실행에 필요한 파일인 dist, node_modules 만 copy +COPY --from=builder /usr/app/dist ./dist + +COPY --from=builder /usr/app/node_modules ./node_modules + +ENTRYPOINT ["node", "/usr/app/dist/main"] diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 4b3fe65..0cf2063 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -1,7 +1,8 @@ services: db: container_name: matjum_db - image: postgis/postgis:16-3.4-alpine + build: + context: ./Dockerfiles/postgis restart: always env_file: - .env diff --git a/docker-compose.yml b/docker-compose.yml index 43763f0..d15aff6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,12 @@ services: db: container_name: matjum_db - image: postgis/postgis:16-3.4-alpine + build: + context: ./Dockerfiles/postgis restart: always env_file: - .env volumes: - - ./init.sql:/docker-entrypoint-initdb.d/init.sql - db_data:/var/lib/postgresql/data ports: - '${POSTGRES_PORT}:5432' # Host:Container 내부 포트 @@ -21,8 +21,7 @@ services: db: condition: service_healthy build: - context: . - dockerfile: Dockerfile + context: ./Dockerfiles/server env_file: - .env ports: diff --git a/docker-entrypoint-initdb.d/init.sql b/docker-entrypoint-initdb.d/init.sql deleted file mode 100644 index 04892e2..0000000 --- a/docker-entrypoint-initdb.d/init.sql +++ /dev/null @@ -1 +0,0 @@ -CREATE EXTENSION IF NOT EXISTS postgis; \ No newline at end of file From a339f8bcb96bfb39cee03bc1909b6bff6655043c Mon Sep 17 00:00:00 2001 From: Jisu Date: Fri, 30 Aug 2024 01:48:44 +0900 Subject: [PATCH 3/5] =?UTF-8?q?:bug:=20fix:=20docker=20compose=20productio?= =?UTF-8?q?n=20=EC=8B=A4=ED=96=89=20=EC=95=88=EB=90=98=EB=8D=98=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - .env.sample 참고해서 .env의 host 를 맞게 작성하셔야 작동합니다. --- .env.sample | 5 +++-- Dockerfile | 19 ------------------- README.md | 7 ++++++- docker-compose.yml | 13 +++++++++++-- 4 files changed, 20 insertions(+), 24 deletions(-) delete mode 100644 Dockerfile diff --git a/.env.sample b/.env.sample index 5cd8a6d..aa51925 100644 --- a/.env.sample +++ b/.env.sample @@ -1,5 +1,5 @@ # node server -NODE_ENV=production +NODE_ENV=development # postgres 설정 @@ -8,7 +8,8 @@ POSTGRES_PORT=5432 # postgres host -POSTGRES_HOST= +# dev - localhost, production - matjum-db 라고 써주세요. +POSTGRES_HOST=loclahost # postgres database 이름 POSTGRES_DB= diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index aeb50b2..0000000 --- a/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM node:20-alpine AS builder - -WORKDIR /usr/app - -COPY . . - -RUN npm i && npm run build && npm prune --production - - -FROM node:20-alpine - -WORKDIR /usr/app - -# 실행에 필요한 파일인 dist, node_modules 만 copy -COPY --from=builder /usr/app/dist ./dist - -COPY --from=builder /usr/app/node_modules ./node_modules - -ENTRYPOINT ["node", "/usr/app/dist/main"] diff --git a/README.md b/README.md index 0cd5805..cb09bbb 100644 --- a/README.md +++ b/README.md @@ -55,5 +55,10 @@ ## 컨테이너 종료 -- `npm run docker:dev:down` 혹은 `npm run docker:prod:down` 명령어로 컨테이너를 종료할 수 있습니다. +- `npm run docker:dev:stop` 혹은 `npm run docker:prod:stop` 명령어로 컨테이너를 종료할 수 있습니다. - `docker stop` 이나 `docker compose down -f <파일명>` 명령어로도 종료할 수 있습니다. + +## docker volume 삭제해야 할 때 (DB 테이블 변경 등) + +- database 다 날아가니 주의해서 사용해주세요. +- `docker volume rm matjum_db_data` 명령어로 삭제합니다. diff --git a/docker-compose.yml b/docker-compose.yml index d15aff6..15796ad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: db: - container_name: matjum_db + container_name: matjum-db build: context: ./Dockerfiles/postgis restart: always @@ -15,8 +15,10 @@ services: interval: 10s timeout: 5s retries: 5 + networks: + - matjum-network server: - container_name: matjum_server + container_name: matjum-server depends_on: db: condition: service_healthy @@ -28,6 +30,13 @@ services: - '3000:3000' init: true restart: unless-stopped # 예기치 않은 종료 시 재시작 + networks: + - matjum-network volumes: db_data: + +networks: + matjum-network: + name: matjum-network + driver: bridge From 2e2cdde2c0ccb4f9e2d220d4acb0762500bd7c80 Mon Sep 17 00:00:00 2001 From: Jisu Date: Fri, 30 Aug 2024 01:56:33 +0900 Subject: [PATCH 4/5] =?UTF-8?q?:bug:=20fix:=20build=20=EA=B2=BD=EB=A1=9C?= =?UTF-8?q?=20postgis->db=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.dev.yml | 2 +- docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 0cf2063..05bf028 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -2,7 +2,7 @@ services: db: container_name: matjum_db build: - context: ./Dockerfiles/postgis + context: ./Dockerfiles/db restart: always env_file: - .env diff --git a/docker-compose.yml b/docker-compose.yml index 15796ad..5750e97 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ services: db: container_name: matjum-db build: - context: ./Dockerfiles/postgis + context: ./Dockerfiles/db restart: always env_file: - .env From 2b9857a90c9c47cf31196eacb123cf4e1e651fd3 Mon Sep 17 00:00:00 2001 From: Doyeon Kim Date: Fri, 30 Aug 2024 02:26:02 +0900 Subject: [PATCH 5/5] =?UTF-8?q?:hammer:=20chore:=20lon,=20lat=20=EC=BB=AC?= =?UTF-8?q?=EB=9F=BC=20=EC=82=AD=EC=A0=9C=20#14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - location 컬럼에서 위경도 데이터를 조회할 수 있어 lon, lat 컬럼을 따로 두지 않아도 됨 --- src/entities/member.entity.ts | 6 ------ src/entities/restaurant.entity.ts | 7 ------- 2 files changed, 13 deletions(-) diff --git a/src/entities/member.entity.ts b/src/entities/member.entity.ts index eb5a11e..2b4e032 100644 --- a/src/entities/member.entity.ts +++ b/src/entities/member.entity.ts @@ -15,12 +15,6 @@ export class Member extends BaseModel { @Column({ type: 'varchar', length: 255, select: false }) password: string; - @Column({ type: 'real', nullable: true }) - lon: number; - - @Column({ type: 'real', nullable: true }) - lat: number; - /** * 4326 - WGS 84 좌표계, 위도와 경도를 도(degrees) 단위로 표현함, 지구의 곡률을 고려하여 정확하게 거리를 계산하기 위해 필요함 * 기본값은 0, 평면 좌표계 diff --git a/src/entities/restaurant.entity.ts b/src/entities/restaurant.entity.ts index dc41dae..1352fff 100644 --- a/src/entities/restaurant.entity.ts +++ b/src/entities/restaurant.entity.ts @@ -19,13 +19,6 @@ export class Restaurant extends BaseModel { @Column({ type: 'varchar', length: 512, nullable: true }) address: string; - // real: 실수형 데이터 타입, 4byte - @Column({ type: 'real', nullable: true }) - lon: number; - - @Column({ type: 'real', nullable: true }) - lat: number; - /** * 4326 - WGS 84 좌표계, 위도와 경도를 도(degrees) 단위로 표현함, 지구의 곡률을 고려하여 정확하게 거리를 계산하기 위해 필요함 * 기본값은 0, 평면 좌표계