2017-08-08 20 views
0

這個錯誤與ECONNREFUSED相同。但是實施方式不同,我會在這裏再提一個問題。docker-compose ECONNREFUSED for nodeg上的Postgres

這裏是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,但我怎麼能在這裏改變。我更新了hostpostgres。它的工作原理,但解決方案不是我想要找到的。

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

postgres_1 | FATAL: database "starflow" does not exist

+0

那麼你想要什麼解決方案?因爲將'host'改爲'postgres'是最好的方式 –

+0

我需要爲docker創建一個新環境嗎?只想使用開發在本地運行服務器。 –

+0

Docker本身運行在一個新的環境中。而且您需要在容器首次運行時配置該環境。 – Ayushya

回答

1

有你需要做兩件事情。一種是將您的應用程序移動到數據庫的網絡上,這樣數據庫將在主機上可用。這需要爲您的服務添加一個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服務。通過這種方式,兩者都可以在本地主機上訪問對方,並且在環境配置中不需要更改。

這是建議只在開發或測試環境,而不是在生產。所以,如果你正在開發,將在生產中使用泊塢窗部署,不使用這種方法

下一頁定製Postgres的圖像來創建不同的數據庫按照圖像

怎樣的以下文件擴大這一形象

如果你想這樣做額外的初始化從這一個派生的圖像中,下/ docker-添加一個或多個* .SQL,* .sql.gz,或* .SH腳本entrypoint-initdb.d(如有必要,創建目錄)。在入口點調用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/

0

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

這就是說,如果你希望不必硬編碼在你的database.json文件中的任何特定的環境參數,你可以將它們分割成不同的database.json文件,並與其他特定環境撰寫文件擴展您的docker-compose.yml

例如,您可以將您的database.json分爲db-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/ 

注意,這些撰寫文件不完整撰寫的定義,它們只包含相關volumes片段。

那麼你可以通過做可以延長原docker-compose.yaml

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

您可以在撰寫docs閱讀更多關於這一點。