簡體   English   中英

nodeJS 上 Postgres 的 docker-compose ECONNREFUSED

[英]docker-compose ECONNREFUSED for Postgres on nodeJS

此錯誤與ECONNREFUSED相同。 但是實現方式不一樣,這里我再問一個問題。

這是docker-compose.yml文件

version: '3'

services:
  server:
    build:
      context: .
    volumes:
      # Mounts the project directory on the host to /app inside the container,
      # allowing you to modify the code without having to rebuild the image.
      - .:/app
      # Just specify a path and let the Engine create a volume.
      # Data present in the base image at the specified mount point will be copied
      # over to the new volume upon volume initialization.
      # node_modules from this new volume will be used and not from your local dev env.
      - /app/node_modules/

    # Expose ports [HOST:CONTAINER}
    ports:
      - "4040:4040"

    # Set environment variables from this file
    env_file:
      - .env

    # Overwrite any env var defined in .env file (if required)
    environment:
      - NODE_ENV=development

    # Link to containers in another service.
    # Links also express dependency between services in the same way as depends_on,
    # so they determine the order of service startup.
    links:
      - postgres
  postgres:
    image: "postgres:9.6"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: 123456
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres

這是我用來存儲數據庫信息的database.json文件

{
"development": {
    "username": "postgres",
    "password": "123456",
    "database": "mydb",
    "host": "127.0.0.1",
    "dialect": "postgres",
    "pool": {
        "max": 100,
        "min": 0,
        "idle": 10000
    }
},
"test": {
    "username": "postgres",
    "password": "123456",
    "database": "mytestdb",
    "host": "127.0.0.1",
    "dialect": "postgres"
},
"production": {
    "username": "postgres",
    "password": "123456",
    "database": "mydb",
    "host": "127.0.0.1",
    "dialect": "postgres"
}
}

並使用 Sequelize 連接 DB

import database from '../../config/database.json'

const sequelize = new Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, dbConfig)

我知道當我在容器中運行應用程序時,它們不是都在 locahost 上,那么我必須更改host ,但是我可以如何更改這里。 我通過將host更新為postgres 它有效,但解決方案不是我想要的。

順便說一句,我如何在這里創建數據庫。

postgres_1 | FATAL: database "starflow" does not exist

您需要做兩件事。 一種是將您的應用程序移動到數據庫的網絡上,這樣數據庫就可以在主機上使用。 這需要向您的服務添加 network_mode。 查看更新的 yaml

version: '3'

services:
  server:
    build:
      context: .
    volumes:
      # Mounts the project directory on the host to /app inside the container,
      # allowing you to modify the code without having to rebuild the image.
      - .:/app
      # Just specify a path and let the Engine create a volume.
      # Data present in the base image at the specified mount point will be copied
      # over to the new volume upon volume initialization.
      # node_modules from this new volume will be used and not from your local dev env.
      - /app/node_modules/

    # Expose ports [HOST:CONTAINER}
    # ports:
    #   - "4040:4040"
    
    network_mode: service:postgres

    # Set environment variables from this file
    env_file:
      - .env

    # Overwrite any env var defined in .env file (if required)
    environment:
      - NODE_ENV=development

    # Link to containers in another service.
    # Links also express dependency between services in the same way as depends_on,
    # so they determine the order of service startup.
    links:
      - postgres
  postgres:
    image: "postgres:9.6"
    ports:
      - "5432:5432"
      - "4040:4040"
    environment:
      POSTGRES_PASSWORD: 123456
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres

請注意,端口將移至提供網絡的服務。 我們在postgres網絡上運行server服務。 這樣,兩者都可以在本地主機上相互訪問,並且不需要在您的環境配置中進行更改。

僅在開發或測試環境中建議這樣做,而不建議在生產環境中使用。 因此,如果您正在開發將在生產中使用的 docker 部署,請不要使用這種方法

接下來自定義 postgres 圖像以創建不同的數據庫,請遵循以下圖像文檔

如何擴展此圖像

如果您想在從該鏡像派生的鏡像中進行額外的初始化,請在 /docker-entrypoint-initdb.d 下添加一個或多個 *.sql、*.sql.gz 或 *.sh 腳本(如有必要,創建目錄)。 在入口點調用 initdb 以創建默認的 postgres 用戶和數據庫后,它將運行任何 *.sql 文件並在該目錄中找到任何 *.sh 腳本以在啟動服務之前進行進一步的初始化。

例如,要添加額外的用戶和數據庫,請將以下內容添加到 /docker-entrypoint-initdb.d/init-user-db.sh:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

有關更多詳細信息,請參閱https://hub.docker.com/_/postgres/

你的host不應該在不同的環境中改變。 應該為它分配docker-compose.yaml定義的 pgsql 服務的名稱,在這種情況下,它是postgres

也就是說,如果您希望不必在database.json文件中硬編碼任何特定於環境的參數,您可以將它們拆分為不同的database.json文件,並使用額外的環境擴展您docker-compose.yml特定的撰寫文件。

例如,您可以將拆分database.jsondb-dev.jsondb-staging.jsondb-prod.json

然后定義特定於環境的 Compose 文件來裝載不同的文件。 例如,

# dbconfig-dev.yml
services:
  server:
      volumes:    
        - ./config/db-dev.json:/app/

# dbconfig-staging.yml
services:
  server:
      volumes:    
        - ./config/db-staging.json:/app/

# dbconfig-prod.yml
services:
  server:
      volumes:    
        - ./config/db-prod.json:/app/

請注意,這些 Compose 文件不是完整的 Compose 定義,因為它們僅包含相關的volumes片段。

然后您可以通過執行以下操作來擴展您的原始docker-compose.yaml

$ docker-compose -f docker-compose.yaml -f dbconfig-dev.yaml up

您可以在 Compose文檔 中閱讀有關此內容的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM