1👍
✅
Based on your description, it seems like the issue is with how the environment variables from your prod.env
file are being read and utilized in your Docker setup. Here are some steps and considerations to help resolve this issue:
- Ensure that the path specified in the
env_file
directive in yourdocker-compose-deploy.yml
file correctly points to yourprod.env
file. If the file is in the root directory, the path should be correct as- prod.env
. - The
prod.env
file needs to be within the build context of your Docker image. If you’re building your image from the root directory (as indicated bycontext: ./
in yourdocker-compose-deploy.yml
), then placingprod.env
in the root should work. - In your
settings.py
, you’re usingdotenv.read_dotenv()
. This function looks foran .env
file by default. If you’re using a different filename like prod.env, you need to specify the path explicitly:
dotenv.read_dotenv(os.path.join(BASE_DIR, 'prod.env'))
Ensure that this code is executed before any environment variables are accessed.
- In your Dockerfile, you’re copying the entire project directory into the image. This should include your prod.env file if it’s in the root. However, note that the ENV instructions in the Dockerfile set environment variables at build time, not runtime. Your Django app reads them at runtime.
- Ensure that your volume mounts in the
docker-compose-deploy.yml
file do not unintentionally overwrite the directory where prod.env is located. - After building the image, run a container and exec into it to check if the prod.env file is present at the expected location.
- Temporarily add a command in your Dockerfile to
cat
theprod.env
file after copying it, to make sure it’s being copied correctly. - Use
docker compose config
to verify that your compose file is correctly configured and includes the prod.env file. - Check the permissions of the prod.env file to ensure it’s readable by the user running the Django application inside the container.
- Every time you make changes to the
prod.env
file or Docker configuration, ensure to rebuild your containers withdocker compose up --build
.
Source:stackexchange.com