[Answered ]-How to split development env file and production env file with docker compose?

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 your docker-compose-deploy.yml file correctly points to your prod.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 by context: ./ in your docker-compose-deploy.yml), then placing prod.env in the root should work.
  • In your settings.py, you’re using dotenv.read_dotenv(). This function looks for an .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 the prod.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 with docker compose up --build.
👤Moziii

Leave a comment