Github Actions 中与 postgres 的连接被拒绝

问题描述 投票:0回答:2

我有一个带有 Postgres 默认数据库的 Django 应用程序,在 docker 中运行。我还使用 Github actions 作为 CI。当我使用命令在本地运行测试时

docker-compose run --rm app sh -c "python manage.py wait_for_db && pytest  -s -v"

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

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

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

django postgresql docker pytest github-actions
2个回答
1
投票

我遇到了同样的问题,并将“healthcheck”添加到 docker-compose.yml。

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

0
投票

amir jamali,你的解决方案完全适合我。

© www.soinside.com 2019 - 2024. All rights reserved.