CLOUD SQL 과 NODE.JS Docker Compose로 개발환경 구성하기

taeng·2023년 8월 24일
1

Docker Compose의 장점

  • 다중 컨테이너 애플리케이션을 관리할수 있고 웹 애플리케이션,데이터베이스 등 여러컨테이너로 구성된 복잡한 애플리케이션을 한번에 관리 할 수있습니다.
  • 환경의 일관성이 있어 다른환경에서도 애플리케이션을 쉽게 실행하고 테스트할 수 있습니다.
  • 컨터이너간의 통신 및 네트워크 설정을 관리할 수 있어 서로 다른 컨테이너 간의 통신을 정의하고 설정 할 수 있습니다

Docker Compose file 생성

version: '3.9' # Docker Compose 파일 버전
services:
  cloudsql-proxy:
    container_name: cloudsql-proxy # 컨테이너이름
    image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.5.0 #cloud sql 도커 이미지
    command: <INSTANCE_NAME:> --credentials-file=<SERVICE_ACCOUNT_KEY PATH> --address 0.0.0.0 --port <PORT> # 컨테이너 실행 명령어
    ports:
      - 127.0.0.1:<Local PORT>:<proxy에 띄운 PORT> # 포트 매핑 
    volumes:
      - <SERVICE ACCOUNT KEY PATH>:/secret/credentials.json # 로컬 파일을 컨테이너 내부로 마운트
    restart: always  # 컨테이너가 종료될 때 항상 다시 시작
  
  #node applcation
  app:
    build:
      context: .
      # 작업소스 도커파일
      dockerfile: ./Dockerfile
    depends_on: 
      - cloudsql-proxy
    ports:
      - "8000:8000"
    volumes: 
      - ./:/var/www

gcp cloud sql에 접속하기 위해서는 sql 설정에서 승인된 네트워크에 본인 네트워크를 통해서 접속을 하거나 proxy를 다운받아 접속하는 방법이있습니다. 여기서는 proxy이미지를 사용 하였습니다.

compose file내용 정리

version : 3.9

  • docker compose의 파일 버전을 작성해 줍니다.

services

  • compose에 사용될 컨테이너들을 적어줍니다.

  • cloudsql-proxy ,app 순서대로 실행 됩니다.

    cloudsql-proxy

    -image
    • 최신버전의 cloud-sql-proxy의 이미지를 선택합니다.
    -command
    • proxy image에 명령어를 입력해 줍니다.

    • DB Instance는 PROJECT_ID:REGION:INSTANCE 잘 모르겠을시 cloud sql에서확인

    app

    -build
    • docker build를 실행
    - context
    • build 를실행할 디렉토리로 .으로 둘시 현재 위치에서 진행됩니다.
    -dockerfile
    • build시 사용될 dokcerfule입니다.
    -depends_on
    • 의존도를 나타내며 cloudsql-proxy가 진행된 이후 진행됩니다.
    -ports
    • port를 매핑해줍니다.
    -volume
    • 로컬디렉토리와 컨데이너의 디렉토리를 연결합니다.

Docker-Compose 명령어

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove resources
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show version information and quit

실행명령어

  • 일반적인 스타트
 $ docker-compose up
  • 백그라운드 재생시
 $ docker-compose up -d
  • dockerfile 수정시
 $ docker-compose up --build

후기

굳이 클라우드에서 인증아이피 등록이나 프록시 설정없이 인증파일만 들고있으면 어디서든 cloud sql에 접속과 소스를 한번에 도커에 돌려 테스트 할 수 있게 해봤습니다. 사용해보니 생각보다 compose가 간단하고 편리하여 재밌게 했던것같다. 기존 도커파일에 있던것들과 엉킬시 반복행위를 할수 있을것같아 도커 컴포즈 파일과 도커파일을 잘 확인해야 할 것 같다. ex) compose volume으로 소스를 디렉토리에 옮긴후 도커파일에서 copy명령 등

참고

https://towardsdatascience.com/how-to-connect-to-gcp-cloud-sql-with-cloud-sql-auth-proxy-in-docker-99bdf810c498

profile
주니어 백엔드 개발자 공부 정리

0개의 댓글