Docker Course
Docker
/
Intermediate

ENV Variables

Definition

The instruction used to set environment variables inside the Docker image. These variables persist in the final image and are available to the application running inside the container.

Explain Like I'm New

Leaving sticky notes for your app to read later. You write a sticky note: `DATABASE_PORT=5432`. When the app boots up days later, it reads the note to know where to connect.

Real World Example

Setting `ENV NODE_ENV=production`. This permanently embeds the environment variable into the image. When Node.js boots up, it reads this variable and automatically switches to high-performance production mode.

Common Use Cases

  • •Configuration
  • •Path management

Interview Questions

basic

  • Can a user change an `ENV` variable when starting the live container, or is it permanently frozen in the image?

intermediate

  • Why is it a severe security risk to use the `ENV` instruction to store database passwords in a Dockerfile?

Flash Cards

Question

Can user change it?

Click to reveal answer
Answer

Yes! The `ENV` instruction just sets the *default* value. A user can easily override it when starting the container by typing `docker run -e "NODE_ENV=development" myapp`.

Question

Why a security risk?

Click to reveal answer
Answer

Because anyone who downloads your image can instantly inspect its layers using `docker history` and read the plaintext password you embedded in the image. Passwords should be passed in at runtime, never baked into the Dockerfile.