Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables in Django container not being properly set #70

Closed
davidwilby opened this issue Feb 2, 2021 · 7 comments · Fixed by #85
Closed

Environment variables in Django container not being properly set #70

davidwilby opened this issue Feb 2, 2021 · 7 comments · Fixed by #85
Labels
bug Something isn't working django

Comments

@davidwilby
Copy link
Collaborator

There's some problem with the environment variables file and the entrypoint script for the django service, resulting in:

CELERY_BROKER_URL taking it's value from reprohack_site/.envs/.local/.django, due to the line:

CELERY_BROKER_URL=REDIS_URL

and not being set by entrypoint for some reason.

similarly, this line is not being executed correctly, so DATABASE_URL is not set, meaning that a whole range of django db commands can't be run incl. createsuperuser and migrate

export DATABASE_URL="postgis://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
@davidwilby davidwilby added bug Something isn't working django labels Feb 2, 2021
@willfurnass
Copy link
Collaborator

I don't see how that line can be right: seems that it should really be something like:

CELERY_BROKER_URL="${REDIS_URL}"

@willfurnass
Copy link
Collaborator

Re the DATABASE_URL not being set: this looks pertinent: cookiecutter/cookiecutter-django#2848

@davidwilby
Copy link
Collaborator Author

CELERY_BROKER_URL=REDIS_URL is in the env variable file, but there's a note elsewhere (I think in the entrypoint) about how you can't do variable expansion in env variable files.

But it seemed to me that when running the local stack that the entrypoint script was not executing correctly as none of the environment variables set therein were actually set.

@griff-rees
Copy link
Collaborator

If memory serves stuff of this ilk came up when trying to sort this:

cookiecutter/cookiecutter-django#1888
cookiecutter/cookiecutter-django#2712
cookiecutter/cookiecutter-django#2413 (comment)

so it may be possible to alter the configuration, perhaps in line with this change: cookiecutter/cookiecutter-django#2674

@Joe-Heffer-Shef
Copy link

Joe-Heffer-Shef commented Mar 18, 2021

So, it looks like the environment variables set in the entrypoint script aren't being remembered because it's a different shell. I don't think it's the best idea to set environment variables in this way, see: Environment variables in Compose

I'm not sure the Django container is actually running the /entrypoint script because the compose file defines the command option (which is equivalent to the CMD statement in a Dockerfile) like this command: /start as shown here https://github.com/reprohack/reprohack_site/blob/dev/local.yml#L24

The /start script doesn't set the DATABASE_URL environment variable.

I believe that Docker Compose will only use the entrypoint/ENTRYPOINT script when using docker-compose run and will run the command/CMD script (and not the entrypoint script) when using docker-compose up.

From Docker RUN vs CMD vs ENTRYPOINT:

ENTRYPOINT instruction allows you to configure a container that will run as an executable. It looks similar to CMD, because it also allows you to specify a command with parameters.

@Joe-Heffer-Shef
Copy link

By the way, passwords should probably not be stored in an environment variable.

See: Manage sensitive data with Docker secrets

@Joe-Heffer-Shef
Copy link

Proposed fix Joe-Heffer-Shef@9747be2

@twinkarma twinkarma mentioned this issue May 21, 2021
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working django
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants