【发布时间】:2022-06-28 19:36:32
【问题描述】:
我正在尝试在 github 操作中使用 docker 连接到 Postgres。这是我的 github 操作文件:
name: Carbon Link backend server CI
on:
push:
branches: [ 'develop', 'PC-3-setup-github-actions-on-pr-request' ]
pull_request:
branches: [ 'develop', 'PC-3-setup-github-actions-on-pr-request' ]
jobs:
# label of the container job
postgres-test:
runs-on: ubuntu-latest
container: node:latest
services:
postgres:
image: postgres:latest
# `POSTGRES_HOST` is `postgres`
env:
POSTGRES_DB: carbonlink
POSTGRES_PASSWORD: password
# optional (defaults to `5432`)
POSTGRES_PORT: 5432
# optional (defaults to `postgres`)
POSTGRES_USER: postgres
ports:
- 5432:5432
# set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
NODE_ENV: test
DATABASE_URL: postgresql://postgres:password@localhost:5432/carbonlink
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v2
- name: Disable husky preparation
run: npm set-script prepare ''
- name: Install dependencies
run: npm install
- name: Migrate
run: npm run db:migrate
- name: Run tests
run: npm test
一切都符合这个命令
Run npm run db:migrate
我得到这个回复:
express-sequel-passport@1.0.0 db:migrate ./node_modules/.bin/sequelize db:migrate
Sequelize CLI [节点:17.9.0,CLI:6.4.1,ORM:6.3.5]
已加载配置文件“config/config.js”。 使用环境“测试”。
错误:连接 ECONNREFUSED 127.0.0.1:5432
这是我的 config.js 文件
require('dotenv').config();
module.exports = {
"development": {
"username": process.env.POSTGRES_USER,
"password": process.env.POSTGRES_PASSWORD,
"database": process.env.POSTGRES_DB,
"host": process.env.POSTGRES_HOST,
"dialect": process.env.POSTGRES_DIALECT
},
"test": {
"url": "postgresql://postgres:password@localhost:5432/carbonlink",
"dialect": 'postgres'
},
"production": {
"username": process.env.POSTGRES_USER,
"password": process.env.POSTGRES_PASSWORD,
"database": process.env.POSTGRES_DB,
"host": process.env.POSTGRES_HOST,
"dialect": process.env.POSTGRES_DIALECT
}
}
这里是 docker-compose 文件供参考:
version: '3'
services:
postgres:
container_name: pgDocker
image: postgres:latest
environment:
- POSTGRES_USERNAME
- POSTGRES_PASSWORD
- POSTGRES_DB
- POSTGRES_TEST_DB
ports:
- 5432:5432
volumes:
- ./data/:/var/lib/postgresql/data
- ./create_test_db.sh:/docker-entrypoint-initdb.d/create_test_db.sh
pgadmin:
container_name: pgadminDocker
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL
- PGADMIN_DEFAULT_PASSWORD
ports:
- 5555:80
depends_on:
- postgres
volumes:
- ./setup/servers.json:/pgadmin4/servers.json
我在本地连接的方式是执行 docker-compose up,它将使用 postgres 启动容器。
我不明白为什么我仍然没有连接到 postgres。我应该做另一个步骤吗?谢谢!
-
只是一个观察:您在 docker-compose 文件中使用
POSTGRES_USERNAME
作为环境名称,而不是POSTGRES_USER
。 -
这不应该是问题,因为当我执行
docker-compose up
时它在本地工作@ -
hmmm - 在 yaml 中使用服务应该启动容器并设置共享网络,以便主机可以使用它。也许尝试在迁移步骤之前的一个步骤中列出正在运行的容器?由于健康检查间隔,是否有可能在启动和迁移之间的某个时间关闭?只是猜测
-
我使用
act
命令在本地运行它并且测试运行,可能是因为我在localhost上运行所以它在本地运行但不在github服务器中?