Github Actions中连接Postgres被拒绝

7
我有一个使用Postgres作为默认数据库的Django应用程序,它在Docker中运行。我还使用Github actions作为持续集成工具。当我在本地使用命令运行测试时:
docker-compose run --rm app sh -c "python manage.py wait_for_db && pytest  -s -v"

一切正常。但是,当我在GitHub操作中使用相同的命令时,会出现以下错误:

E       django.db.utils.OperationalError: connection to server at "postgres" (172.18.0.2), port 5432 failed: Connection refused
E           Is the server running on that host and accepting TCP/IP connections?

/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py:122: OperationalError

这是我的 Github 工作流代码:

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.10
      uses: actions/setup-python@v2
      with:
        python-version: "3.10"
    - name: Install docker-compose
      run: |
        pip install docker-compose

    - name: Test with pytest
      run: |
        docker-compose run --rm app sh -c "python manage.py wait_for_db && pytest  -s -v"

我的docker-compose代码:
version: "3"

services:

  postgres:
    image: postgres:14-alpine
    environment:
      - POSTGRES_DB=app
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=123
    ports:
      - "5432:5432"

  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command:
      sh -c "python manage.py wait_for_db &&
        python manage.py migrate &&
        python manage.py runserver 0.0.0.0:8000"
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_NAME=app
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=123
    depends_on:
      - postgres

我的Dockerfile:

FROM python:3.10.3-alpine
MAINTAINER restlin1212

ENV PYTHONUNBUFFERED 1
ENV DJANGO_SETTINGS_MODULE=app.settings

RUN pip install pipenv
COPY ./Pipfile /Pipfile
COPY ./Pipfile.lock /Pipfile.lock
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps gcc libc-dev linux-headers postgresql-dev
RUN pipenv install --system --deploy
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D newuser
USER newuser

有人可以帮我解决这个问题吗?


这个回答解决了你的问题吗?GitHub Actions:如何在GitHub Actions中连接到Postgres - rethab
你能想办法让它工作了吗? - maslak
2
@maslak,问题出在wait_for_db脚本上,由于某些原因它在github actions中无法正常工作。我更改了脚本并解决了问题。 - Ivan Buruyane
是的,在我的情况下,我只是正确地配置了健康检查 :P - maslak
1个回答

1

我曾经遇到过同样的问题,后来在docker-compose.yml文件中添加了'healthcheck'。

db:
image: postgres:14-alpine
volumes:
  - dev-db-data:/var/lib/postgresql/data
healthcheck:
  test: ["CMD-SHELL", "pg_isready", "-q", "-d", "{YOUR_DATABASE_NAME}", "-U", "{YOUR_DATABASE_USERNAME}" ]
  interval: 5s
  timeout: 5s
  retries: 5

我还将“链接”添加到我的主应用程序服务中:

depends_on:
  db:
    condition: service_healthy
links:
  - db

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接