[Django]-Django's docker container doesn't catch the environment ALLOWED_HOSTS variable in GitLab CI pipeline

4πŸ‘

βœ…

I got my answer thanks to @Zeitounator

I will simply quote his/her comment in order to make it simple:

os.environ.get retrieves environment variables from the running system which is your docker container, not from the underlying gitlab-ci system. Gitlab CI vars (as your usual shell vars) are not automagically pushed to your container. SECRET_KEY does not issue a warning because it’s simply null. DJANGO_ALLOWED_HOSTS does because you try to split it. You have to pass those env vars to your container, either with the -e docker option or through an env file you create on spot.

So this definitely works:

...

Test stage:
  stage: test
  script:

...

    - docker run --rm -e SECRET_KEY=mydummysecretkey_gitlab-ci -e DJANGO_ALLOWED_HOSTS='localhost 127.0.0.1 [::1]' $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA pytest

...

I ended up with something like this:

docker run --rm -e SECRET_KEY='$SECRET_KEY' -e DJANGO_ALLOWED_HOSTS='$DJANGO_ALLOWED_HOSTS' $CI_COMMIT_SHA pytest
πŸ‘€Bravo2bad

Leave a comment