I have a setup with docker-compose that builds web container with Django and postgresql container with postgresql. All works fine, and I have no reason to think it will not work in production Postgres is a recent addition, before it was sqlite3 that ran inside web container.
What I am stuck with is the tests. I have gitlab repo where I push changes and each push triggers a test pipeline that is configured with .gitlab_ci.yml. I can not figure out what exactly I have to change in that file (or/and other files involved) for that (TEST!) environment to have web and db talking to each other.
No mater what I do (that includes the original untouched versions) the pipeline invariably dies with the same error:
django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
The error happens when the following Dockerfile diretive is executed:
RUN python manage.py migrate
I do not care for the tests if postgresql runs inside the web container or in another container, as long as they find each other. In production however it will like on my local machine: two containers, web and postgresql.
My files:
Original .gitlab-ci.yml
image: docker:stable
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
CONTAINER_TEST_IMAGE: registry.gitlab.com/<my_repo>:$CI_COMMIT_REF_SLUG
CONTAINER_RELEASE_IMAGE: registry.gitlab.com/<my_repo>:latest
CONTAINER_DEVELOP_IMAGE: registry.gitlab.com/<my_repo>:develop
services:
- docker:dind
stages:
- test
test:
stage: test
script:
- docker pull postgres:9.6
- docker run --name db -d postgres:9.6
- sleep 5
- docker build --pull -t test_image .
- docker run --net=host test_image /bin/bash -c "coverage run --source='.' manage.py test --verbosity 2 && coverage html && coverage report"
artifacts:
paths:
- htmlcov/
expire_in: 5 days
Modified .gitlab-ci.yml
image: docker:stable
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
CONTAINER_TEST_IMAGE: registry.gitlab.com/<my_repo>:$CI_COMMIT_REF_SLUG
CONTAINER_RELEASE_IMAGE: registry.gitlab.com/<my_repo>:latest
CONTAINER_DEVELOP_IMAGE: registry.gitlab.com/<my_repo>:develop
POSTGRES_DB:<my_db_name>
POSTGRES_USER:<my_user>
POSTGRES_PASSWORD: <my_password>
services:
- docker:dind
- postgres:9.6
stages:
- test
test:
stage: test
script:
- docker pull postgres:9.6
- docker run --name db -d postgres:9.6
- sleep 5
- docker build --pull -t test_image .
#here in DB_HOST I also tried 'db', '<my_db_name>' and couple of other options instead of 'localhost', the error is the same
- docker run --net=host -e DB_HOST=localhost test_image /bin/bash -c "coverage run --source='.' manage.py test --verbosity 2 && coverage html && coverage report"
artifacts:
paths:
- htmlcov/
expire_in: 5 days
My settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<my_db_name>',
'USER': '<my_user>',
'PASSWORD': '<my_password>',
'HOST': 'db',
'PORT': '',
#yes, I tried port 5432 and 5432:5432, makes no difference
}
}
Dockerfile
# Dockerfile
FROM python:3.6
MAINTAINER me
# The enviroment variable ensures that the python output is set straight
# to the terminal with out buffering it first
ENV PYTHONUNBUFFERED 1
# install nginx
RUN apt-get update
RUN apt-get -y install nginx gettext-base libsasl2-dev libldap2-dev libssl-dev
#overwrite configuration files
COPY helper_functions/nginx_docker.template.conf /etc/nginx/nginx.template.conf
COPY helper_functions/nginx_docker.conf /etc/nginx/nginx.conf
WORKDIR /usr/src/app/
COPY requirements.txt /usr/src/app/
COPY . /usr/src/app
RUN pip install -r requirements.txt
RUN python setup.py install
RUN python manage.py collectstatic
#THIS LINE (MIGRATE) produces that "could not translate host name "db" to address" error!!!
RUN python manage.py migrate
# COPY startup script into known file location in container
COPY start_gunicorn.sh /start_gunicorn.sh
# EXPOSE port 8080 to allow communication to/from server
EXPOSE 8080
# CMD specifies the command to execute to start the server running.
CMD ["/start_gunicorn.sh"]
Again, it all works with separate docker containers locally and I belive elsewhere. I think it's only the test pipeline with its gitalb-cy.yml that needs some adjustments.
Thanks!
Aucun commentaire:
Enregistrer un commentaire