6

I have a Django app with Postgres default database, running in docker. I also use Github actions as a CI. When I run the tests locally using the command

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

everything works fine. However, I get the following error when I use the same command in 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

Here is my github workflow code:

# 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"

My docker-compose code:

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

And my 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

Could someone please help me solve the problem?

Ivan Buruyane
  • 61
  • 1
  • 2
  • Does this answer your question? [GitHub Actions: How to connect to Postgres in GithHub Actions](https://stackoverflow.com/questions/57915791/github-actions-how-to-connect-to-postgres-in-githhub-actions) – rethab Apr 07 '22 at 14:01
  • were u able to get it working somehow? – maslak Jun 20 '22 at 08:23
  • 2
    @maslak, the problem was in the wait_for_db script, it didn't work in the github actions for some reason. I changed the script and it helped – Ivan Buruyane Jun 21 '22 at 09:03
  • yeah, in my case I just configured healthchecks properly :P – maslak Jun 21 '22 at 09:13

1 Answers1

1

I had the same problem and add 'healthcheck' to 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

Also added 'linkes' to my main app service:

depends_on:
  db:
    condition: service_healthy
links:
  - db