My goal is to run collectstatic
command inside Dockerfile
, but when trying to rebuild and run the container by running
docker-compose up -d --build
I run into KeyError
messages coming from settings.py
file on lines where environmental variables are used, for example:
os.environ['CELERY_BROKER']
This is apparently because the container is not yet built, so Docker does not know anything about the environment variables defined in my .env
file. Is there any command to import all the variables to Docker?
Or maybe it's a bad idea to run the collectstatic
command inside Dockerfile and it should be run inside docker-compose file ? Or maybe as part of a CI/CD task ?
My Dockerfile looks like this:
COPY . /app/
WORKDIR /app
RUN python manage.py collectstatic --noinput
RUN ls -la
You probably do not want to set the environment variables from .env
at build time as these get baked into the container image, so shared with anyone that has access to that image. IMO, the way to handle this would be to change the instances of os.environ['CELERY_BROKER']
in your settings.py
with os.environ.get("CELERY_BROKER", "...")
where the second argument is a sensible default for your development environment.