How are environment variables typically provided to a Node.js application running inside a Docker container?

Node.js interview question for Advanced practice.

Answer

They are set using the ENV instruction in the Dockerfile or the -e flag with the docker run command.

Explanation

A Docker container runs in an isolated environment. Environment variables are not automatically inherited from the host. They must be explicitly defined for the container. This is done either by setting default values in the Dockerfile using the ENV instruction or by passing them at runtime with the -e or --env flag (e.g., docker run -e "APIKEY=123" my-app).

Related Questions