這個錯誤與ECONNREFUSED相同。但是實施方式不同,我會在這裏再提一個問題。docker-compose ECONNREFUSED for nodeg上的Postgres
這裏是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 | FATAL: database "starflow" does not exist
那麼你想要什麼解決方案?因爲將'host'改爲'postgres'是最好的方式 –
我需要爲docker創建一個新環境嗎?只想使用開發在本地運行服務器。 –
Docker本身運行在一個新的環境中。而且您需要在容器首次運行時配置該環境。 – Ayushya