How do you set environment variables in a Docker container?

Prepare for the Docker Foundations Test. Review essential Docker concepts with multiple choice questions, each with hints and explanations. Ace your exam!

Setting environment variables in a Docker container can be done effectively using the -e flag with the docker run command. This method allows you to define environment variables at the time the container is started, making it flexible and dynamic.

When using the -e flag, you can specify one or multiple environment variables in the following format: -e VAR_NAME=value. This approach is particularly useful for passing configuration settings or sensitive information, such as API keys and database passwords, without hardcoding them into the application or Dockerfile.

Considering other options, editing the Dockerfile directly would set defaults for the container but wouldn't allow customization at runtime. Creating a .env file is often used in combination with Docker Compose, but it's not a feature inherent to the docker run command itself. Command-line argument passing could be used to provide parameters to the application, but it does not inherently set environment variables in the same manner as the -e flag does.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy