L. Faros
L. Faros

Reputation: 2008

Django fail to connect to postgres database in gitlab ci

I have the following .gitlab-ci.yml :

image: python:3.6

stages:
  - lint
  - test

services:
  - postgres:10.1-alpine

cache:
  paths:
  - /root/.local/share/virtualenvs/

before_script:
  - python -V
  - pip install pipenv
  - pipenv install --dev

lint:
  stage: lint
  script:
  - pipenv run pylint --output-format=text --load-plugins pylint_django project/ | tee pylint.txt
  - score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
  - echo "Pylint score was $score"
  - pipenv run anybadge --value=$score --file=pylint.svg pylint
  artifacts:
    paths:
      - pylint.svg

test:
  stage: test
  script:
  - pipenv run python manage.py test

And I am connecting to the database like this :

# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': '',
        'HOST': 'db', # set in docker-compose.yml
        'PORT': 5432 # default postgres port
    }
}

For now, I just have this tests in users/tests.py :

from .models import CustomUser
from django.test import TestCase

class LogInTest(TestCase):
    def setUp(self):
        self.credentials = {
            'username': 'testuser',
            'password': 'secret'}
        CustomUser.objects.create_user(**self.credentials)
    def testLogin(self):
        # send login data
        response = self.client.post('/users/login/', self.credentials, follow=True)
        # should be logged in now
        self.assertTrue(response.context['user'].is_authenticated)

Which fails with the following error :

psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known 

In development, I use the following docker-compose.yml file :

version: '3.3'

services:
  db:
    image: postgres:10.1-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    environment:
      - SECRET_KEY=changemeinprod
    depends_on:
      - db

volumes:
  postgres_data:

With this Dockerfile :

FROM python:3.6

# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev

# Copy project
COPY . /code/

I don't understand why the app cab't connect to the database in the CI but connect just fine in the development ENV with docker

Upvotes: 1

Views: 2076

Answers (1)

jwillker
jwillker

Reputation: 1035

In your docker-compose.yml you set the service name as db, and django settings.py for use db, but in gitlab will use the image name as the name of the service, postgres in case.

You have two options:

1 - Use environments variables in settings.py, some example here

2 - Set a alias in gitlab-ci.yml, like:

services: - name: postgres:10.1-alpine alias: db link: gitlab docs

Upvotes: 4

Related Questions