Join our Discord Server
Avinash Bendigeri Avinash is a developer-turned Technical writer skilled in core content creation. He has an excellent track record of blogging in areas like Docker, Kubernetes, IoT and AI.

Ways to Pass Environment Variables to Docker Containers: -e Flag, .env File, and Docker Compose | Docker

2 min read


Environment variables are the essential tools of any programmer’s toolkit. They hold settings, configurations, and secrets that shape how our applications run. In the world of Docker containers, passing these variables effectively is crucial for smooth sailing on the high seas of deployment.

But fear not, mateys! This blog post is your nautical map, guiding you through the different ways to pass environment variables to your Docker containers. Buckle up and prepare to dive deep!

Why Pass Environment Variables?

Think of environment variables as hidden crewmates on your Docker voyage. They silently store vital information like database credentials, API keys, or application configs, ensuring everything runs smoothly without cluttering your code or Dockerfiles.

Here’s why passing environment variables is ahoy-some:

  • Decoupling: Keep sensitive information out of your code or Dockerfiles, enhancing security and flexibility.
  • Configuration Management: Easily manage different deployment environments by changing variables instead of rebuilding containers.
  • Scalability: Maintain consistency across multiple containers and deployments with centralized variable management.

The Three Musketeers of Passing Environment Variables:

Now, let’s explore the three main ways to pass these environment variables to your Docker containers:

1. The -e Flag:

This trusty first mate, the -e flag, allows you to directly set individual variables when running the docker run command. It’s like shouting orders to your container before setting sail:

docker run -d -t -i -e NAMESPACE='staging' busybox sh

If you want to use multiple environments from the command line then before every environment variable use the -e flag.

Example:

docker run -d -t -i -e NAMESPACE='staging' -e PASSWORD='foo' busybox sh

2. The .env File:

For larger crews of variables, the .env file acts as a trusty logbook. Each line defines a variable and its value, making it easy to manage and share configurations:

NAMESPACE=staging
PASSWORD=foo

Then, you use the --env-file flag to tell Docker to consult this logbook before launching your container:

docker run --env-file .env my_image

3. Docker Compose:

In a grand fleet of containers, Docker Compose captains the ship. Its YAML files allow you to define environment variables for all services within your application, keeping your configuration shipshape:

version: "3.9"

services:
  web:
    environment:
      NAMESPACE: staging
      PASSWORD: foo
  database:
    ...

Choosing the Right Approach:

The best way to pass environment variables depends on your specific needs. For a quick one-off deployment, the -e flag might suffice. For larger applications, the .env file offers better organization and security. And for complex multi-container setups, Docker Compose reigns supreme.

Remember, mateys:

  • Security: When dealing with sensitive information like passwords or API keys, consider storing them securely outside your Docker environment, like in a vault or secrets management tool.
  • Testing: Always test your environment variable configurations in different environments to ensure everything runs smoothly.

With these tips in your seabag, you’ll be passing environment variables to your Docker containers like a seasoned skipper. So set sail, explore the vast ocean of possibilities, and remember, a smooth deployment is a happy deployment!

Bonus Tip:

Want to get fancy with your environment variables? Check out Docker secrets or Kubernetes ConfigMaps for even more advanced management options.

Ahoy there, fellow Docker enthusiasts! Did you find this blog post helpful? Share your favorite ways to pass environment variables in the comments below, and let’s keep the seas of deployment calm and productive!

Keep Reading

  • Testcontainers and Playwright

    Testcontainers and Playwright

    Discover how Testcontainers-Playwright simplifies browser automation and testing without local Playwright installations. Learn about its features, limitations, compatibility, and usage with code examples.

    Read More

  • Getting Started with the Low-Cost RPLIDAR Using NVIDIA Jetson Nano

    Getting Started with the Low-Cost RPLIDAR Using NVIDIA Jetson Nano

    Conclusion Getting started with low-code RPlidar with Jetson Nano is an exciting journey that can open up a wide range of possibilities for building robotics projects. In this blog post, we covered the basic steps to get started with low-code RPlidar with Jetson Nano, including setting up ROS, installing the RPlidar driver and viewing RPlidar…

    Read More

  • Docker and Wasm Containers – Better Together

    Docker and Wasm Containers – Better Together

    Learn how Docker Desktop and CLI both manages Linux containers and Wasm containers side by side.

    Read More

  • The Future of AI Developer Tooling

    The Future of AI Developer Tooling

    The Fragmented World of AI Developer Tooling Since OpenAI introduced function calling in 2023, developers have grappled with a critical challenge: enabling AI agents to seamlessly interact with external tools, data, and APIs. While foundational models grow smarter, integrating agents into diverse systems remains cumbersome, requiring custom logic for every new integration. Enter Model Context…

    Read More

  • DevOps-Inspired Guide: Optimizing PoE 2 Currency Pipelines

    Path of Exile 2 (PoE 2) presents a highly modular, system-driven economy where success is dependent on efficient currency workflows, scalable strategies, and informed resource orchestration—much like DevOps pipelines in software delivery. This guide draws DevOps parallels to help you implement version-controlled, observable, and resilient currency strategies in PoE 2. 1. Currency as an Operational…

    Read More

  • Is Ollama ready for Production?

    Is Ollama ready for Production?

    Introduction: The Ollama Promise As organizations seek alternatives to cloud-based AI services, Ollama has gained significant traction for its ability to run large language models locally. While its simplicity and privacy advantages are compelling, a crucial question remains: Is Ollama truly ready for production environments? In this analysis, we’ll examine the current state of Ollama…

    Read More

Have Queries? Join https://launchpass.com/collabnix

Avinash Bendigeri Avinash is a developer-turned Technical writer skilled in core content creation. He has an excellent track record of blogging in areas like Docker, Kubernetes, IoT and AI.

How to Build and Host Your Own MCP Servers…

Introduction The Model Context Protocol (MCP) is revolutionizing how LLMs interact with external data sources and tools. Think of MCP as the “USB-C for...
Adesoji Alu
1 min read
Join our Discord Server
Index