VOOZH about

URL: https://thenewstack.io/what-is-the-docker-env-file-and-how-do-you-use-it/

⇱ What Is the Docker .env File and How Do You Use It? - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-01-24 08:00:31
What Is the Docker .env File and How Do You Use It?
tutorial,
Cloud Native Ecosystem / Containers / Kubernetes

What Is the Docker .env File and How Do You Use It?

Docker .env file holds all the sensitive passwords and configuration data so they don't have to be planted in the container itself.
Jan 24th, 2023 8:00am by Jack Wallen
👁 Featued image for: What Is the Docker .env File and How Do You Use It?

This article has been updated from when it was originally published on January 24, 2023

Overview

The article discusses the use of the .env file in Docker container deployments. A .env file is a text file that contains key-value pairs representing environment variables that can be used by containers during deployment.

Key points:

  • The .env file allows developers to set environment variables without hardcoding them into the container manifest, making it a more secure approach.
  • Environment variables in the .env file are referenced using $ followed by the variable name (e.g., $DB_DATABASE) in docker-compose.yml files.
  • The .env file can be used for simple and complex deployments, including full-stack deployments.

Benefits:

  • Simplifies managing configuration across different environments
  • Promotes better security by not hardcoding sensitive information into source code repositories or docker-compose files
  • Allows developers to create reusable .env files with placeholders for passwords

Best practices:

  • Keep the .env file in a separate directory from your project’s working files.
  • Use third-party services like HashiCorp Vault for highly sensitive information.
  • Avoid saving secrets in local storage and use secure methods to manage them.

When you deploy Docker containers, you will oftentimes have to add customized variables. Those variables could include all sorts of information, including usernames, passwords, database names, etc. Of course, you can always hard code those variables into the container manifest but that is widely considered a security issue. On top of that, if you’re deploying similar containers over and over again, it’s not exactly efficient having to re-type all of that information.

To solve these types of problems, most developers opt to go with the .env file. Essentially, an .env file is a list of key pair values that set specific variables for a container deployment. So, instead of having to code those variables into the manifest itself, you add them to an .env file and when you run docker-compose up -d, the variables will be applied from within the .env file (“env” being short for environment variables).

It really is that straightforward.

The .env file can be used for simple container deployments and even complex full-stack deployments. Either way, it’s a much more secure way of using secrets for a deployment. Although nothing is perfect, using a hidden file (one that starts with a . in Linux is considered hidden) that keeps you from hard coding secrets into your manifests should not just be considered the smart way to go, it should be your default. And, yes, there are even more secure ways to handle secrets.

A few things to keep in mind about .env files:

  • The .env file is used during the pre-processing step with docker-compose.yml files.
  • To pass variables from the .env file to the command, you use $ as in $DB_DATABASE.
  • ENV values are available via docker-build or run-style commands (using the $ as explained above).
  • ENV files allow you to pass multiple environment variables at once.
  • You can pass the .env file variables to the docker run command using the –env-file options, like so: docker run –env-file .env mysql:latest

For example, you can create a new Docker secret like this:

First, create a file that contains a secret with:

echo "this-is-my-secret" > $HOME/secret.txt

Next, create the Docker secret with:

docker secret create password-secret $HOME/secret.txt

The output will be something like this:

fx0ziezaw0xjpb0i7bt092xmi

You can list your secrets with the command:

docker secret list

The output will contain the secret you just created like so:

fx0ziezaw0xjpb0i7bt092xmi password-secret 48 seconds ago 48 seconds ago

You could then use that secret when deploying a service like this:

docker service create --name test-service --secret password-secret mysql:latest

The output of the above command will look something like this:

zcoz2b2xvzakoq1x7jny6l5x5
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged

There are also third-party services, such as HashiCorp Vault, that offer encrypted vaults that can be used to house secrets.

But if you’re looking to simplify things, the .env file is a great way to go. On top of which, the .env file isn’t just about keeping secrets. With this file, you can add any environment variable that you need.

Let me show you how to create one.

Creating Your First .env File

Let’s say you’ve created a directory to house all of your working files for the project, called ~/my-project. Change into that directory with the command cd ~/my-project. In that directory, create the .env file with the command:

nano .env

Remember, the .env file must be contained within the base directory of your project, otherwise, it won’t be picked up by the deploy command.

A very basic .env file might contain the following key pairs:

USERNAME=olivia
PASSWORD=b3stp@$$w0rd

Let’s say, however, you’re deploying a stack that includes a MySQL database container and you need to set the database server, database port, database name, database username, and database password. That .env file might look something like this:

DB_HOST="127.0.0.1"
DB_PORT="3306"
MYSQL_ROOT_PASSWORD="r00tp@$$w0rd"
DB_USER="dbuser"
DB_PASSWORD="p@$$w0rd"
DB_DATABSE="database"

Save that file with the [Ctrl]+[X] keyboard shortcut. Now, your YAML file will need to be set up properly. Let’s simplify this a bit. Say our .env file looks like this:

MYSQL_ROOT_PASSWORD="r00tp@$$w0rd"
MYSQL_ALLOW_EMPTY_PASSWORD="p@$$w0rd"
MYSQL_RANDOM_ROOT_PASSWORD="p@$$w0rd"

To pass those variables to our docker-compose.yml file, that file would have to contain something like this:

MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_ALLOW_EMPTY_PASSWORD: ${MYSQL_ALLOW_EMPTY_PASSWORD}
MYSQL_RANDOM_ROOT_PASSWORD:  ${MYSQL_RANDOM_ROOT_PASSWORD}

When you run the docker-compose up -d command, the environment key values will be passed to the deployment and things should spin up nicely, without having to save those values in the docker-compose.yml file.

This is also very handy in that you can create a single .env file to repurpose for other deployments. This is especially so when creating complex deployments. I would, however, advise not saving secrets in these files on your local storage. You could always create .env files to be reused, with placeholders for passwords, like so:

MYSQL_ROOT_PASSWORD="ROOT_PWORD"
MYSQL_ALLOW_EMPTY_PASSWORD="PASSWORD"
MYSQL_RANDOM_ROOT_PASSWORD="PASSWORD"

When you go to use that .env file, copy it into the base directory of the project, substitute the actual passwords, save the file, and then run your docker-compose command.

Of course, nothing is perfectly secure. Should something gain access to the machine housing the .env files, all they would have to do is issue the command ls -la within the directory housing the .env file to verify there is an environment variable file and then view the contents with something like cat .env. When you’re dealing with highly sensitive information, your best bet is to use a third-party service, such as the aforementioned HashiCorp Vault. But for your standard Docker deployments, the .env file is a great way to go.

FAQ

Q: What is the recommended way to store sensitive information in a Docker container? 

A: Store sensitive information such as passwords, API keys, and credentials in an environment variable file (.env file) outside of your code repository. This ensures that sensitive data is not committed to version control.

Q: What is the best way to manage environment variables for my Docker container? 

A: Use an environment variable file (.env file) or a secrets manager like HashiCorp Vault. This ensures that sensitive information is properly secured and isolated from other parts of your application.

Q: How do I secure my Docker container from unauthorized access? 

A: Use a combination of methods, such as:

  • Configure SELinux (or AppArmor) policies.
  • Limit network traffic and ports exposed.
  • Regularly update your containers to prevent exploitation of known vulnerabilities.

Q: How can I ensure that my Docker containers are running with the correct permissions? 

A: Set the security-options, no-new-privileges, apparmor-enabled to limit privileges and enforce AppArmor or SELinux policies. Additionally, use User Namespaces (UN) to isolate processes from each other.

Q: What is the recommended way to update my Docker containers with new dependencies? 

A: Use a tool like Docker Compose or Kubernetes’ rolling updates feature to roll out changes gradually, ensuring minimal disruption to your application.

Q: How can I optimize performance in my Docker container? 

A: Optimize resource allocation by:

  • Limiting CPU and memory usage
  • Using caching mechanisms (e.g., Redis)
  • Minimizing network traffic
  • Leveraging content delivery networks (CDNs) for static assets

Q: What is the best way to test and validate my Docker containers? 

A: Use automated testing frameworks like CI/CD tools, such as Jenkins or Travis CI. Additionally, perform manual testing by running your application in a non-production environment.

Q: How can I ensure that my Docker container complies with security standards?

A: Follow established guidelines and best practices for securing containers, such as the following:

  • Regularly update dependencies to prevent exploitation of known vulnerabilities
  • Implement secure protocols (HTTPS) for communication
  • Limit access to sensitive data and resources

Q: What is the recommended way to store Docker container logs? 

A: Use a centralized logging solution like Fluentd, ELK Stack, or Splunk. This ensures that log analysis can be performed efficiently and provides visibility into container activity.

Q: How can I ensure that my Docker containers are properly backed up and restored? 

A: Implement regular backups of your containers using tools like Docker’s built-in backup feature (such as the docker commit command) or third-party solutions like Backblaze B2.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
HashiCorp is a sponsor of The New Stack.
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.