---
title: "Setting Environment Variables at Runtime in Docker"
date: "2026-11-17T10:30:00+07:00"
description: "Learn how to configure Docker containers dynamically at runtime using -e and --env flags, pass host variables, override Dockerfile defaults, and inspect runtime environment settings."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/setting-environment-variables-runtime.jpg"
alt: "Passing environment variables to Docker containers using command line flags"
tags: ['docker', 'containers', 'cli', 'configuration', 'devops']
---

Containerized applications require configuration management to run seamlessly across local development, staging, and production environments. Decoupling configuration settings from container images adheres to the [Twelve-Factor App methodology](https://12factor.net/config).

Docker provides flexible mechanisms to pass environment variables into container runtimes dynamically using the `-e` and `--env` flags without rebuilding the underlying container image.

---

## 1. Setting Environment Variables with `-e` and `--env`

When starting a container with `docker run` or `docker create`, you can specify key-value environment variables directly on the command line:

```bash
docker run -d --name app-service \
  -e NODE_ENV=production \
  -e PORT=8080 \
  node:20-alpine node server.js
```

Both `-e` and `--env` syntax are functionally identical. Multiple variables can be defined by chaining the flag multiple times.

---

## 2. Passing Host Environment Variables Implicitly

Hardcoding secrets or variable values into command-line strings can expose sensitive data in shell history files (`~/.bash_history` or `~/.zsh_history`).

To prevent shell leakage, Docker allows passing environment variables from the host terminal session by declaring only the key name without an equals sign (`=`):

```bash
# Export variable on host shell
export DATABASE_PASSWORD="SuperSecretHostPassword123!"

# Pass the host variable implicitly to the container
docker run -d --name secure-api \
  -e DATABASE_PASSWORD \
  my-api-image:latest
```

Docker reads `DATABASE_PASSWORD` from the active host shell environment and injects its value into the container runtime.

---

## 3. Overriding Dockerfile `ENV` Instructions

If a container image defines default environment variables inside its `Dockerfile` using the `ENV` instruction:

```dockerfile
# Dockerfile snippet
FROM python:3.11-slim
ENV LOG_LEVEL="INFO"
ENV APP_TIMEOUT="30"
```

Passing `-e` or `--env` flags at runtime cleanly overrides the build-time `ENV` defaults:

```bash
# Override LOG_LEVEL to DEBUG at runtime
docker run -d --name debug-app \
  -e LOG_LEVEL="DEBUG" \
  python-app:v1
```

Runtime environment variables take immediate precedence over any `ENV` values compiled into the image layer manifest.

---

## 4. Inspecting Container Environment Variables

To confirm that environment variables have been properly injected into a running or stopped container, you can use `docker inspect` or `docker exec`.

### Method A: Using `docker inspect`

The `docker inspect` command outputs detailed JSON metadata about the container configuration:

```bash
# Inspect container Environment array
docker inspect --format='{{json .Config.Env}}' debug-app
```

**Output:**
```json
["LOG_LEVEL=DEBUG","APP_TIMEOUT=30","PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]
```

### Method B: Executing `env` Inside the Container

To check the live environment inside an active container, run the POSIX `env` or `printenv` utility via `docker exec`:

```bash
docker exec debug-app printenv LOG_LEVEL
```

---

## 5. Summary and Best Practices

**Takeaway:** Decouple application configuration from Docker images by using `-e` / `--env` flags at container startup. Pass sensitive credentials implicitly from host shell variables to avoid shell history logging, and verify runtime variable states using `docker inspect` or `docker exec printenv`.

---

## Sources

- [Docker Documentation: docker run environment flags](https://docs.docker.com/reference/cli/docker/container/run/#env)
- [Docker Documentation: ENV Dockerfile Reference](https://docs.docker.com/reference/dockerfile/#env)
- [Twelve-Factor App: Configuration Management](https://12factor.net/config)
