用docker compose 連線到google cloud SQL - jenhaoyang/backend_blog GitHub Wiki

範本:

version: '3'

services:
  app:
    build: .
    # your own configurations for that app
    depends_on:
    - cloud-sql-proxy
    environment:
      # You must set an URL to access your database. On sequelize (nodejs) it follows this
      # format: db-type://user:password@host:port/database-name
      # the one below is for a MySQL database.
      DATABASE_URL: mysql://dbuser:dbpassword@cloud-sql-proxy:3306/database

  cloud-sql-proxy:
    image: gcr.io/cloudsql-docker/gce-proxy:1.11
    # Port: MySQL (3306), PostgreSQL (5433)
    command: /cloud_sql_proxy -instances=PROJECTID:ZONE:INSTANCE=tcp:0.0.0.0:3306
    volumes:
    # this mounts your application default credential on the container, preventing the
    # 'invalid json file "/config": google: read JWT from JSON credentials: 'type' field is "authorized_user" (expected "service_account")'
    # error if you point to the actual credential file directly
    - ~/.config:/root/.config
    ports:
    - 3306:3306

參考:
https://adilsoncarvalho.com/how-to-use-cloud-sql-proxy-on-docker-compose-f7418c53eed9