docker-compose为nodeJS上的Postgres ECONNREFUSED

时间:2017-08-08 17:43:47

标签: node.js postgresql docker docker-compose sequelize.js

此错误与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连接数据库

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

2 个答案:

答案 0 :(得分:1)

您需要做两件事。一种是在数据库的网络上移动您的应用程序,这样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

请注意,端口将移动到提供网络的服务。我们在server网络上运行postgres服务。这样,两者都可以在localhost上相互访问,并且环境配置中不需要进行任何更改。

  

建议仅在开发或测试环境中使用,而不是在生产环境中使用。因此,如果您正在开发将在生产中使用的docker部署,那么DON&#t; T使用这种方法

接下来自定义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/

答案 1 :(得分:0)

您的host不应该在不同的环境中进行更改。应为您分配docker-compose.yaml中定义的pgsql服务的名称,在这种情况下,它是postgres

也就是说,如果您希望不必在database.json文件中对任何特定于环境的参数进行硬编码,则可以将它们拆分为不同的database.json文件,并扩展您的docker-compose.yml文件。 1}}附加特定于环境的撰写文件。

例如,您可以将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

中详细了解相关信息