One of the challenges of using Docker containers in your applications is passing environment variables to the containers. There are many ways to do this, and here we’ll cover seven of them, ranging from easiest to hardest. When you use environment variables, they help you avoid hard-coding values in your application code, which makes it easier to change those values when you need to do so later. Additionally, if your application runs in multiple environments—development, staging, and production—you can use environment variables to help make sure that it behaves the same way in each of these environments.
Using docker run
Docker run is the most common way to start a container. By default, any environment variables you set in your shell are inherited by the container. However, there are other ways to pass environment variables to containers.
Use the -e flag when you run the container:
`docker run -e VARIABLE=value` will set an environment variable for that one instance of the docker run command only. To make it permanent and propagate it to all subsequent runs of that container, use `-e VARIABLE_NAME=value`. You can also specify multiple environment variables at once like this: `-e VAR1=value1 -e VAR2=value2`, etc. If you want to override existing environment variables, use `-e VARIABLE_NAME=override`. For example, if the current value of ENVIRONMENT is production but I want the new container’s ENVIRONMENT to be development instead then I would type
`docker run -e ENVIRONMENT=development`
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Here’s an example docker-compose.yml file:
A key point about docker compose files is that they’re composed of blocks that map containers to particular images and configurations. For example, in this snippet we can see two services defined – web_db and web_app – each with their own network settings: The two relevant lines are as follows:
web_db networks : – dbnet
web_app networks : – appnet
Notice how both services have been mapped to the same image (with different versions) but have different networks attached? The service with name ‘web_db’ has the dbnet network while the service named ‘web_app’ has the appnet network.
Use ENV directives in your shell config file
If you want to set environment variables every time you start a new shell, you can use the ENV directive in your shell config file. The ENV directive will set an environment variable for the duration of the current shell session and any child processes.
Use ENV directives directly in your docker command
You can use the ENV directive directly in your docker command. This is the most straightforward way to set environment variables for your containers. To do this, simply add the -e flag followed by the name of the variable and its value when you run a docker command. For example , if I wanted to start a new container with the DATABASE_URL environment variable set to postgres://user:password@host:5432/dbname, I would execute the following command:
To store these as defaults so that you don’t have to enter them every time, edit your .env file.
Share an init container
An init container is a special type of container that runs before other containers in a pod. Init containers can contain utilities or setup scripts not present in the application image. They can also block until a specific condition is met, such as waiting for a database to become available. Passing environment variables to an init container is a common use case.
Mount directories on host into the container
- You can use the -v flag to mount a directory from your host machine into your container.
- For example, let’s say you have a directory on your host machine at /path/to/my/dir. You can mount this directory into your container like so: docker run -v /path/to/my/dir:/container/path …
- Another way to mount directories into containers is to use Docker volumes.
Override the default directory with an environmental variable in your docker run command
If you want to override the default directory that your container runs in, you can do so with an environmental variable. Just add the -e flag to your docker run command, followed by the name of the variable and the value you want to set it to. For example, if you wanted to override the HOME variable, you would use this command: docker run -e HOME=/new/path my_container.