node.js - Node Postgres Docker

标签 node.js postgresql docker sequelize.js

我尝试使用我的node.js 应用程序设置Docker,该应用程序使用sequelize 连接到postgres。

const sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USER,
    process.env.DB_PASS,
    {
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        dialect: 'postgres',
    },
);

在我的 .env 文件中我声明 DB_HOST=postgres(这是 docker-conpose.yml 中声明的服务的名称)和 DB_PORT=5432 在所有其他环境变量中。

我的 Dockerfile 如下所示:

FROM node:8.6.0

# Working directory for application
WORKDIR /usr/src/app
EXPOSE 8080
COPY . /usr/src/app

# In this file I create a user and a DB and give him the privlages 
ADD init.sql /docker-entrypoint-initdb.d/
RUN npm install

我的 docker-compose.yml 如下所示:

version: "2"
services:
  postgres:
    image: "postgres:9.4"
    restart: always
    ports:
      - "5432:5432"
    env_file:
      - .env
  node:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    command: ["npm", "start"]

当我 docker-compose up 时,我收到 Sequelize 无法连接到数据库的错误。

Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431

有人可以帮我解决这个错误吗?

所有 Docker 日志:

WARNING: Image for service node was built because it did not already exist. To rebuild this image you must use docker-compose build or docker-compose up --build.

Creating graphqlpostgrestemplate_postgres_1 ...

Creating graphqlpostgrestemplate_postgres_1 ... done

Creating graphqlpostgrestemplate_node_1 ...

Creating graphqlpostgrestemplate_node_1 ... done

Attaching to graphqlpostgrestemplate_postgres_1, graphqlpostgrestemplate_node_1

postgres_1 | The files belonging to this database system will be owned by user "postgres".

postgres_1 | This user must also own the server process.

postgres_1 |

postgres_1 | The database cluster will be initialized with locale "en_US.utf8".

postgres_1 | The default database encoding has accordingly been set to "UTF8".

postgres_1 | The default text search configuration will be set to "english".

postgres_1 |

postgres_1 | Data page checksums are disabled.

postgres_1 |

postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok

postgres_1 | creating subdirectories ... ok

postgres_1 | selecting default max_connections ... 100

postgres_1 | selecting default shared_buffers ... 128MB

postgres_1 | selecting dynamic shared memory implementation ... posix

postgres_1 | creating configuration files ... ok

postgres_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok

postgres_1 | initializing pg_authid ... ok

postgres_1 | initializing dependencies ... ok

postgres_1 | creating system views ... ok

node_1 | npm info it worked if it ends with ok

node_1 | npm info using npm@5.3.0

node_1 | npm info using node@v8.6.0

postgres_1 | loading system objects' descriptions ... ok

node_1 | npm info lifecycle graphql-postgres-template@1.0.0~prestart: graphql-postgres-template@1.0.0

node_1 | npm info lifecycle graphql-postgres-template@1.0.0~start: graphql-postgres-template@1.0.0

node_1 |

node_1 | > graphql-postgres-template@1.0.0 start /usr/src/app

node_1 | > nodemon --exec babel-node index.js

node_1 |

postgres_1 | creating collations ... ok

postgres_1 | creating conversions ... ok

postgres_1 | creating dictionaries ... ok

postgres_1 | setting privileges on built-in objects ... ok

postgres_1 | creating information schema ... ok

postgres_1 | loading PL/pgSQL server-side language ... ok

node_1 | [nodemon] 1.12.1

node_1 | [nodemon] to restart at any time, enter rs

node_1 | [nodemon] watching: .

node_1 | [nodemon] starting babel-node index.js

postgres_1 | vacuuming database template1 ... ok

postgres_1 | copying template1 to template0 ... ok

postgres_1 | copying template1 to postgres ... ok

postgres_1 | syncing data to disk ... ok

postgres_1 |

postgres_1 | Success. You can now start the database server using:

postgres_1 |

postgres_1 | postgres -D /var/lib/postgresql/data

postgres_1 | or

postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start

postgres_1 |

postgres_1 |

postgres_1 | WARNING: enabling "trust" authentication for local connections

postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or

postgres_1 | --auth-local and --auth-host, the next time you run initdb.

postgres_1 | ****************************************************

postgres_1 | WARNING: No password has been set for the database.

postgres_1 | This will allow anyone with access to the

postgres_1 | Postgres port to access your database. In

postgres_1 | Docker's default configuration, this is

postgres_1 | effectively any other container on the same

postgres_1 | system.

postgres_1 |

postgres_1 | Use "-e POSTGRES_PASSWORD=password" to set

postgres_1 | it in "docker run".

postgres_1 | ****************************************************

postgres_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address

postgres_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.

postgres_1 | LOG: database system was shut down at 2017-10-10 12:17:15 UTC

postgres_1 | LOG: MultiXact member wraparound protections are now enabled

postgres_1 | LOG: database system is ready to accept connections

postgres_1 | LOG: autovacuum launcher started

postgres_1 | done

postgres_1 | server started

postgres_1 | ALTER ROLE

postgres_1 |

postgres_1 |

postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

postgres_1 |

postgres_1 | waiting for server to shut down....LOG: received fast shutdown request

postgres_1 | LOG: aborting any active transactions

postgres_1 | LOG: autovacuum launcher shutting down

postgres_1 | LOG: shutting down

postgres_1 | LOG: database system is shut down

node_1 | Tue, 10 Oct 2017 12:17:16 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:236:13

node_1 | WARNING: No configurations found in configuration directory:/usr/src/app/config

node_1 | WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.

node_1 | Tue, 10 Oct 2017 12:17:17 GMT body-parser deprecated undefined extended: provide extended option at index.js:53:30

node_1 | Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431

node_1 | at connection.connect.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24)

node_1 | at Connection.connectingErrorHandler (/usr/src/app/node_modules/pg/lib/client.js:123:14)

node_1 | at emitOne (events.js:115:13)

node_1 | at Connection.emit (events.js:210:7)

node_1 | at Socket. (/usr/src/app/node_modules/pg/lib/connection.js:71:10)

node_1 | at emitOne (events.js:115:13)

node_1 | at Socket.emit (events.js:210:7)

node_1 | at emitErrorNT (internal/streams/destroy.js:64:8)

node_1 | at _combinedTickCallback (internal/process/next_tick.js:138:11)

node_1 | at process._tickDomainCallback (internal/process/next_tick.js:218:9)

node_1 | [nodemon] clean exit - waiting for changes before restart

postgres_1 | done

postgres_1 | server stopped

postgres_1 |

postgres_1 | PostgreSQL init process complete; ready for start up.

postgres_1 |

postgres_1 | LOG: database system was shut down at 2017-10-10 12:17:16 UTC

postgres_1 | LOG: MultiXact member wraparound protections are now enabled

postgres_1 | LOG: database system is ready to accept connections

postgres_1 | LOG: autovacuum launcher started

最佳答案

在您的Docker-compose服务node中添加link配置选项指向服务postgres,如下所示:

node:
  links:
    - postgres

然后您可以使用名称服务 postgres 连接到 postgresdb

关于node.js - Node Postgres Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46666597/

相关文章:

mysql - Docker,运行带端口转发的mysql容器一启动就立即停止

python - 清理坏行的 Postgres 表

python - 为什么我的 Flask 应用程序会给出此响应 "class User(db.model): AttributeError: ' SQLAlchemy' 对象没有属性 'model' “?

javascript - 取消订阅 Node.JS 中的 Rethink DB Outside 函数

node.js - 使用express和sockjs进行用户身份验证

python - SQLAlchemy 临时表痛苦的解决方案?

python-3.x - aiodocker 异步创建容器

docker - 在 Alpine Docker 容器 : "loadinternal: cannot find runtime/cgo" 中从源代码编译 Go 时出错

javascript - 升级 reactJS 后出现非法访问错误

javascript - 同时分配和检查并有一个很好的语法错误