<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged env</title>
<link>https://coderlegion.com/tag/env</link>
<description>Powered by Question2Answer</description>
<item>
<title>Beginner’s Guide to Environment Variables with Docker</title>
<link>https://coderlegion.com/4837/beginners-guide-to-environment-variables-with-docker</link>
<description>&lt;p&gt;If you’ve ever worked with Docker, you’ve probably heard the term “environment variables”. They play an important role in configuring your containers and securing your sensitive data. This guide will walk you through everything you need to know about using environment variables with Docker Compose, even if you’re a complete beginner.&lt;/p&gt;
&lt;p&gt;???????? &lt;strong&gt;What Are Environment Variables?&lt;/strong&gt;&lt;br&gt;
Environment variables are key-value pairs used to configure applications. Instead of hard-coding values (such as passwords or database URLs), you can store them as variables and access them from your code.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;DATABASE_URL=mongodb://localhost:27017/mydb
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;API_KEY=9YmCCoJfsI9sIactxPBu&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;???? Why Use Environment Variables in Docker Compose?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you’re using Docker Compose to spin up multi-container applications, environment variables help you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoid hardcoding sensitive data in your docker-compose.yml&lt;/li&gt;
&lt;li&gt;Easily switch between development, staging, and production setups&lt;/li&gt;
&lt;li&gt;Keep your configuration clean and reusable&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;???? Ways to Set Environment Variables in Docker&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Using the environment attribute&lt;/strong&gt;&lt;br&gt;
You can set variables inline in your docker-compose.yml file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;services:
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;  web:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;image: node:23.11.0
environment:
  - NODE_ENV=development
  - PORT=5000
  - API_KEY=PLdaTwF9YmCCoJfsI9sIactxPBu
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;⚠️ This method is simple, but can expose sensitive data.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Using an .env file as the best practice&lt;/p&gt;
&lt;p&gt;Create a .env file in the same directory as your docker-compose.yml:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;PORT=5000
NODE_ENV=development
API_KEY=PLdaTwF9YmCCoJfsI9sIactxPBu
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then in your docker-compose.yml:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;services:
web:
 image: node:23.11.0
 ports:
  - &quot;${PORT}:5000&quot;
 environment:
  - NODE_ENV=${NODE_ENV}
  - API_KEY=${API_KEY}
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;✅ Docker Compose automatically loads .env from the same directory.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;2. Using env_file attribute to load variables&lt;/strong&gt;&lt;br&gt;
You can also explicitly tell Docker Compose to use a specific .env file:&lt;/p&gt;
&lt;p&gt;  services:&lt;br&gt;
  web:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;image: node:23.11.0
env_file:
  - ./dev.env
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;This allows configurations to be separated according to the&lt;br&gt;
environment. (e.g., dev.env, prod.env).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;3. Using shell environment variables&lt;/strong&gt;&lt;br&gt;
You can export environment variables in your terminal before running Docker Compose:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;export NODE_ENV=development
export PORT=3000
docker-compose up
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;4. Passing variables inline when running a container (less common in Compose)&lt;/strong&gt;&lt;br&gt;
This approach is more common with docker run, but worth noting:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;docker run -e NODE_ENV=production node:23.11.0
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;In Docker Compose, this is equivalent to setting them under&lt;br&gt;
environment:.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;???????????? Bonus: Environment Variables vs Build Arguments&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Build arguments&lt;/em&gt; are similar to environment variables, but they’re used at build time instead of runtime.&lt;/p&gt;
&lt;p&gt;Example: Using ARG in Dockerfile&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;FROM node:23.11.0
ARG NODE_ENV
RUN echo &quot;Building in $NODE_ENV mode&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Set ARG in docker-compose.yml&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;services:
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;  web:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;build:
  context: .
  args:
    NODE_ENV: development
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Key differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ARG is only available during the image build process.&lt;/li&gt;
&lt;li&gt;ENV (environment variable) is available at container runtime.&lt;br&gt;
If you need a variable during the build, use ARG. If your app needs it during runtime, use ENV.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;???? Best Practices&lt;/strong&gt;&lt;br&gt;
⚡ Never commit .env files with secrets to version control.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;????️ Use different .env files for different environments.&lt;/li&gt;
&lt;li&gt;✅ Keep your .env file in the same folder as docker-compose.yml for auto-detection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;???? Debug Tip&lt;/strong&gt;&lt;br&gt;
Want to make sure your variables are loaded? Run this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;docker-compose exec web env
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This shows all environment variables inside the running container.&lt;/p&gt;
&lt;p&gt;If you would like to improve your skills, please check out my other articles on docker and docker-compose on different subjects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/3478/deploy-a-reactjs-app-to-aws-ec2-with-docker-nginx-and-automate-with-github-actions-part-one&quot;&gt;How to Deploy ReactApp to AWS EC2 with docker, nginx and automate with gitHubAction.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;✨ Final Thoughts&lt;/strong&gt;&lt;br&gt;
Environment variables make Docker Compose setups more secure, cleaner, and easier to manage. Whether you’re running a single service or a complex app with multiple containers, mastering environment variables is a foundational skill that pays off.&lt;/p&gt;
&lt;p&gt;Thanks for reading through and I hope you liked what you read here. Feel free to connect with me on LinkedIn, twitter and GitHub.&lt;/p&gt;
&lt;p&gt;Happy Dockering! ????&lt;/p&gt;
</description>
<category>Java</category>
<guid isPermaLink="true">https://coderlegion.com/4837/beginners-guide-to-environment-variables-with-docker</guid>
<pubDate>Wed, 20 Aug 2025 05:52:51 +0000</pubDate>
</item>
</channel>
</rss>