Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 27/ 160
02. Dockerfiles & Image Building

Runtime Configuration (ENV, EXPOSE, USER)

Configure runtime env, documented ports, and non-root USER defaults in images.

BeginnerENV · EXPOSE3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Declare low-privilege defaults and useful ports, but verify them with runtime behavior rather than treating metadata as enforcement.

Think of it like…

ENV is the prefilled settings form, EXPOSE is the diagram label for the room, and USER is the badge the process wears when it enters.

Where you’ve seen it

In practice, ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"] is the concrete move for this job: configure runtime env, documented ports, and non-root USER defaults in images. The release is not accepted until docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node confirms the result.

Words you’ll hear

ENV
The implementation control in ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"] for configure runtime env, documented ports, and non-root USER defaults in images.
EXPOSE
The result to observe with docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node, rather than infer from a successful command alone.
USER
The production boundary exposed when exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path.

Visual diagram

Loading interactive diagram

Theory & deep dive

ENV control path

The concrete mechanism is ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"]. The observable result is docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node; if those disagree, inspect the runtime evidence before changing unrelated settings.

Configure runtime env, documented ports, and non-root USER defaults in images. Keep ENV, EXPOSE and USER in the same deployment record so the intent and the observed state cannot drift apart.

Concrete ENV setup

ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"]

Key takeaway: Prove EXPOSE with docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node, not with command success alone.

USER failure boundary

Running from ENV as root, assuming EXPOSE opens a firewall port, or using a named USER that does not exist causes familiar production surprises. Numeric UIDs also make policy and Kubernetes securityContext checks more consistent. The narrow failure to design for is exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path.

Accept this compromise explicitly: Image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests. The controlled check-docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path.

Key takeaway: Image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: Running from ENV as root, assuming EXPOSE opens a firewall port, or using a named USER that does not exist causes familiar production surprises. Numeric UIDs also make policy and Kubernetes securityContext checks more consistent.

Declare low-privilege defaults and useful ports, but verify them with runtime behavior rather than treating metadata as enforcement.
Key numbers at this step
Intent
ENV

When to use it & trade-offs

What it is

ENV writes default runtime environment into image configuration, EXPOSE records intended container ports, and USER sets the default numeric/group identity for later instructions and runtime. None of these replaces an actual listener, bind permission or network publishing rule.

Why it matters: Running from ENV as root, assuming EXPOSE opens a firewall port, or using a named USER that does not exist causes familiar production surprises. Numeric UIDs also make policy and Kubernetes securityContext checks more consistent.

When to reach for it

  • The fit is strongest for this job: configure runtime env, documented ports, and non-root USER defaults in images. Pair it with docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node in the runbook.
  • Production ENV changes should make EXPOSE observable through ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"], rather than trusting the runtime default.
  • Keep a rollback example for the case where exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path.

When to avoid or be careful

  • Do not accept command success while the system can still fail by exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path.
  • Avoid erasing USER protection merely to dodge this cost: “Image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests”.
  • Do not replace the evidence path-docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node-with a screenshot or a mutable tag.

ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"]: practical choices

Explicit ENV
Best for: Configure runtime env, documented ports, and non-root USER defaults in images, with a reviewed runbook.

Strengths: ENV writes default runtime environment into image configuration, EXPOSE records intended container ports, and USER sets the default numeric/group identity for later instructions and runtime. None of these replaces an actual listener, bind permission or network publishing rule.

Watch out: Image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests.

Ambient default
Best for: Short-lived EXPOSE experiments with low blast radius.

Strengths: It can begin with ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"] and fewer surrounding controls.

Watch out: It silently accepts exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path, which makes the result hard to reproduce after promotion.

Managed USER
Best for: Teams that centralize USER policy across many workloads.

Strengths: Running from ENV as root, assuming EXPOSE opens a firewall port, or using a named USER that does not exist causes familiar production surprises. Numeric UIDs also make policy and Kubernetes securityContext checks more consistent.

Watch out: The platform adds an ownership and debugging layer; retain docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node as the direct check.

Interview signals & numbers to know

When this topic comes up

  • ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"]
  • docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node
  • Failure signature: exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path
  • Trade-off: Image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests.

Benchmarks & rules of thumb

10001 UID -
non-root identity in the example
3000 -
documented/listened port, not a published host port
2 scopes -
ENV/USER persist in config while EXPOSE is advisory metadata

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"] as proof and skipping docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node.
  • Exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path
  • Optimizing ENV without accepting that image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests.

Likely interviewer follow-ups

  • Why does ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"] address ENV here rather than through application code?
  • How would you distinguish healthy EXPOSE from the failure “exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path” using docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node?
  • When would this trade-off-“Image defaults support portability, but orchestrators may override command, environment and UID and must preserve secure values in deployment manifests”-justify a different USER design?
Interview tip: Give one concrete operation-ENV NODE_ENV=production PORT=3000; EXPOSE 3000; USER 10001:10001; CMD ["node", "server.js"]-and one proof-docker run -d --name node app:prod and docker inspect -f {{.Config.User}}:{{json .Config.ExposedPorts}} node. Then name the boundary: exposing 3000 and never publishing or listening on it, or setting USER before fixing file ownership of the application path.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Runtime Configuration (ENV, EXPOSE, USER) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Setting Working Directories & Metadata (WORKDIR, LABEL, ARG)
Next topic
Container Startup Commands (CMD vs ENTRYPOINT & Combining Both)