2017-08-08 7 views
0

このエラーはECONNREFUSEDと同じです。しかし、実装方法が異なる場合、ここで別の質問をします。ここで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 

は私が知っているストアデータベース情報

{ 
"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" 
} 
} 

そしてDB

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

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

を接続するためにSequelizeを使用するために使用されるdatabase.jsonファイルです私がコンテナでアプリケーションを実行すると、両方ではありませんロカホストではhostを変更する必要がありますが、ここでどのように変更することができます。私はhostからpostgresにアップデートしました。それは動作しますが、解決策は私が探したいものではありません。

ところで、ここでどのようにDBを作成できますか?

postgres_1 | FATAL: database "starflow" does not exist

+0

どのような解決策が必要ですか? 'ホスト'を 'postgres'に変更するのが最善の方法ですから –

+0

ドッカー用の新しい環境を作る必要はありますか?ローカルでサーバーを実行するための開発のみを使用します。 –

+0

Dockerは新しい環境で動作します。コンテナが初めて実行されているときにその環境を設定する必要があります。 – Ayushya

答えて

1

あなたがする必要がある2つのものがあります。 1つはあなたのアプリケーションをDBのネットワーク上に移動し、その方法でDBはホスト上で利用可能になります。これには、サービスに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サービスを実行しています。このようにして、両方ともlocalhost上でお互いにアクセスすることができ、環境設定で変更する必要はありません。

これは、開発環境またはテスト環境でのみ使用し、本番環境では使用しないことをお勧めします。あなたが生産に使用されるドッキングウィンドウの配置を開発しているのであれば、画像

方法の以下のドキュメントに従って別のデータベースを作成するために、Postgresの画像をカスタマイズするには、このアプローチ

次を使用しないでくださいあなたは、この1つに由来画像内の追加の初期化を行う/ docker-下* .SHスクリプトを一つ以上の* .SQL、* .sql.gz、またはを追加したい場合は、このイメージ

を拡張しますentrypoint-initdb.d(必要に応じてディレクトリを作成する)エントリポイントがinitdbを呼び出してデフォルトのpostgresユーザとデータベースを作成した後、* .sqlファイルが実行され、そのディレクトリにある* .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 

が参照:

例えば、/docker-entrypoint-initdb.d/init-user-db.shするために、次の追加、追加のユーザーとデータベースを追加しますhttps://hub.docker.com/_/postgres/

0

hostは、異なる環境で変更する必要はありません。これには、docker-compose.yamlに定義されているpgsqlサービスの名前を割り当てる必要があります。この場合はpostgresです。あなたはハードコードにあなたのdatabase.jsonファイル内の環境固有のパラメータを持っていないことを望んでいる場合、あなたは別のdatabase.jsonファイルにそれらを分割し、追加の環境固有のコンファイルを使用してdocker-compose.ymlを拡張することができ、言っ

たとえば、database.jsondb-dev.json,db-staging.jsonおよびdb-prod.jsonに分割することができます。

次に、さまざまなファイルをマウントする環境固有のComposeファイルを定義します。例えば、これらの作曲のファイルは、彼らが唯一の関連volumes断片から構成されているではありません完全な作曲の定義は

# 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/ 

お知らせしていること。

は次にあなたができることによって、あなたの元docker-compose.yamlを拡張:

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

あなたが作曲docsでこれについての詳細を読むことができます。

関連する問題