我正在尝试在执行docker-compose之前从sequelize播种数据,但是播种时出现此错误。
错误:连接ECONNREFUSED 127.0.0.1:5432
这是我正在使用的命令
docker-compose run --rm app npm run seed
在执行docker-compose up sequelize时会同步模型,但是我需要在模型同步之前先连接到服务器,以便我可以执行诸如播种数据或运行迁移之类的命令。我该怎么办?
我将分享相关代码。
续集如何连接到数据库
if (process.env.NODE_ENV === "production") {
var sequelize = new Sequelize(process.env.DATABASE_URL);
} else {
var sequelize = new Sequelize(
process.env.POSTGRES_DB,
process.env.POSTGRES_USER,
"",
{
host: process.env.PSQL_HOST,
dialect: "postgres",
pool: {
max: 100,
min: 0,
idle: 200000,
// @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
acquire: 1000000,
},
}
);
}
docker-compose.yml
# docker-compose.yml
version: "3"
services:
app:
build: ./server
depends_on:
- database
ports:
- 5000:5000
environment:
PSQL_HOST: database
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_DB: ${POSTGRES_DB:-elitypescript}
entrypoint: ["/bin/bash", "./entrypoint.sh"]
client:
build: ./client
image: react_client
links:
- app
working_dir: /home/node/app/client
volumes:
- ./:/home/node/app
ports:
- 3001:3001
command: npm run start
env_file:
- ./client/.env
database:
image: postgres:9.6.8-alpine
volumes:
- database:/var/lib/postgresql/data
ports:
- 3030:5432
volumes:
database:
.entrypoint.sh
# entrypoint.sh
npm run seed # my attempt to run seed first before server kicks in. but doesn't work
npm run server
app.ts
models.sequelize.sync().then(() => {
httpServer.listen(PORT, () => {
console.log(
"App is running at http://localhost:%d in %s mode",
app.get("port"),
app.get("env")
);
console.log(" Press CTRL-C to stop\n");
});
});
答案 0 :(得分:0)
@Neo Anderson,感谢您指出config.json问题,我能够解决此问题。
所以问题是几件事。启动服务器时,Sequelize查找2个文件。
其中一个是config.json
,
.sequelizerc
const path = require('path');
module.exports = {
'config': path.resolve('config', 'config.json'),
'models-path': path.resolve('models'),
'seeders-path': path.resolve('dist', 'seeders'),
'migrations-source-path' : path.resolve('migrations'),
'migrations-compiled-path': path.resolve('dist', 'migrations')
}
和models/index.js
if (process.env.NODE_ENV === "production") {
var sequelize = new Sequelize(process.env.DATABASE_URL);
} else {
console.log(
process.env.POSTGRES_DB,
process.env.POSTGRES_USER,
process.env.PSQL_HOST
);
var sequelize = new Sequelize(
process.env.POSTGRES_DB,
process.env.POSTGRES_USER,
"",
{
host: process.env.PSQL_HOST,
dialect: "postgres",
pool: {
max: 100,
min: 0,
idle: 200000,
// @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
acquire: 1000000,
},
}
);
}
您必须做的是确保将docker-compose.yml环境变量传递到这两个文件中。
# docker-compose.yml
version: "3"
services:
app:
build: ./server
depends_on:
- database
ports:
- 5000:5000
environment:
# these variables must be passed in config.json, and models/index.js
PSQL_HOST: database
PSQL_PORT: 5430
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_DB: ${POSTGRES_DB:-elitypescript}
entrypoint: ["/bin/bash", "./entrypoint.sh"]
....
config.json
我知道我应该使用process.env,但是现在我只是在这里对docker变量进行了硬编码。因为我需要将config.json变成一个js文件。
{
"development": {
"username": "postgres",
"password": "",
"database": "elitypescript",
"host": "database",
"dialect": "postgres",
"port": "5432"
},
}
因此,现在docker正在执行种子,并从entrypoint.sh运行服务器。现在一切都按预期进行。
希望这对任何人都有帮助