Challenge, Easy,  on  ContainersLinux

Many applications rely on environment variables for configuration - from log level and feature flags to database credentials and API keys. However, containerized applications run in isolated execution environments (that is what containers are), which, in particular, means any environment variables set in your host shell session won't be visible to the process(es) running in the container.

This isolation is by design and provides important security benefits. But it also means you need to explicitly pass environment variables to your containers.

Containers are isolated execution environments: Environment variables set in the host shell session are not visible to the container.

Your task is to launch a postgres container with the following environment variables:

  • POSTGRES_USER=admin - sets the superuser name
  • POSTGRES_PASSWORD=1234 - sets the superuser password
Hint 1: Running a container with environment variables 💡

Docker provides an easy way to set environment variables while launching a container with the docker run command. To pass environment variables to a container, use the --env (or -e) flag followed by the variable assignment like this:

docker run -e MY_VAR="Hello Container" alpine printenv MY_VAR
Hint 2: Passing multiple environment variables 💡

The -e flag can be used multiple times to set multiple environment variables.

docker run -e FOO=foo -e BAR=bar alpine printenv FOO BAR

Alternatively, you can store the environment variables in a file and pass it to the container using the --env-file flag.

Level up your Server Side game — Join 11,000 engineers who receive insightful learning materials straight to their inbox