Load .env File When Running Docker Image

Docker has built-in support for environment (.env) files. This is a handy way to manage configuration settings that must be passed into containers during runtime, such as API keys, database passwords, and other sensitive or customizable data.

  • Create a file named .env in your project directory.
  • Add environment variables in the format KEY=value on separate lines. For example:

Use the --env-file option when running your Docker container. This option lets you specify the path to your .env file, which Docker will read and set the environment variables from within the container. It looks like this:

docker run --env-file ./.env your-image-name

This command tells Docker to load the environment variables defined in the .env file into the container at runtime.

Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses; Lead Developer, Software Architect, Product Manager, CTO and now happily a Founder.


Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.