gpt4 book ai didi

node.js - docker-compose ECONNREFUSED 用于 nodeJS 上的 Postgres

转载 作者:行者123 更新时间:2023-11-29 13:18:33 26 4
gpt4 key购买 nike

此错误与 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 |致命:数据库“starflow”不存在

最佳答案

您需要做两件事。一种是将您的应用程序移动到数据库的网络上,这样数据库就可以在主机上使用。这需要为您的服务添加一个 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 服务。这样,两者都可以在本地主机上相互访问,并且无需更改您的环境配置。

This is recommended only in development or testing environment and not in production. So if you are developing docker deployment that would be used in production, DON'T use this approach

接下来要自定义 postgres 图像以创建不同的数据库,请遵循图像的以下文档

如何扩展这张图片

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

For example, to add an additional user and database, add the following to /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/

关于node.js - docker-compose ECONNREFUSED 用于 nodeJS 上的 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com