docker-为nodeJS上的Postgres组成ECONNREFUSED

这个错误与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周围。 它的工作原理,但解决scheme不是我想find的。

顺便说一句,如何在这里创build数据库。

postgres_1 | FATAL: database "starflow" does not exist

有两件事你需要做的。 一个是将您的应用程序移动到数据库的networking上,这样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 

请注意,端口将移动到提供networking的服务。 我们在postgresnetworking上运行server服务。 这样,两者都可以在本地主机上访问彼此,并且在环境configuration中不需要更改。

这是build议只在开发或testing环境,而不是在生产。 因此,如果您正在开发将在生产中使用的docker部署,请不要使用此方法

接下来定制postgres图像来创build一个不同的数据库,请按照下面的图像文件

如何扩展这个图像

如果您希望在从此映像派生的映像中执行额外的初始化操作,请在/docker-entrypoint-initdb.d下添加一个或多个* .sql,* .sql.gz或* .sh脚本(必要时创build该目录)。 在入口点调用initdb创build默认的postgres用户和数据库之后,它将运行任何* .sql文件,并在该目录中find任何* .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/

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

也就是说,如果你希望不需要在你的database.json文件中硬编码任何特定于环境的参数,你可以将它们分成不同的database.json文件,并且使用额外的环境扩展你docker-compose.yml文件,特定的撰写文件。

例如,你可以将你的database.json拆分成db-dev.jsondb-prod.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 

您可以在撰写文档中阅读更多关于此的信息。