How to use environment variables in Docker

If you’re new to Docker, you might be wondering what environment variables are and how they can be used in your containers. Environment variables are essentially key-value pairs that you can set in your Docker container to define certain properties or configurations.
In this blog post, we’ll explore environmental variables and how to use them in Docker.

Before going through this blog post if you are new to docker I recommend you to visit our previous blogs related to docker to get a basic understanding of Docker blogs.

Types of environment variables in docker

Build time: ARG–This is usually used to store configuration options while building the docker image such as version, library etc. So in short terms, ARG or build time variables are defined in the docker file which is used to pass information to the docker file during the build. These variables will not be available while creating the container.

Runtime variables:ENV:– as the name suggests, these are used during the runtime of a container. They allow you to pass information to your container at the time it’s launched such as api_keys, passwords etc. Unlike ARG they persist inside the image as well as in the container.

How to set build time variables in docker

We can use define our build ARG in our docker file and using the –build-arg parameter we can use the ARG to build our image

Here’s an example:

FROM ubuntu
ARG MY_VAR=VALUE1
RUN echo “MY_VAR is ${MY_VAR}”

In this example, we’re defining an ARG called MY_VAR in the Dockerfile, which is used in the RUN command to print the value of MY_VAR.

As you can see in the screenshot, the value for MY_VAR is echoed as VALUE1, defined in the Docker file.

How to override build time variables.

Suppose you want to override the variable MY_VAR with a new value other than the one defined in the docker file you can use the build-arg flag in the docker build command

The value of MY_VAR can be passed at build time using the –build-arg option as shown below.

docker build --build-arg MY_VAR=NEW_VALUE -t my_image .

As you can see in this screenshot the value has changed to NEW_VALUE.

Using this method we can define our build time variables but these variables will not be available at the container run time for that we need to use docker env variables.

How to set Runtime variables in docker

To use environment variables in your Docker containers, you can set them in your Dockerfile or specify them when you run your container using the -e option.

For example, we are defining the ENV variable MYSQLPASSWORD as an example in our docker file and building the image and running the container based on that image. This is our docker file.

FROM ubuntu
ENV MYSQLPASSWORD=123456

In the docker file, we are just defining a runtime variable called MYSQLPASSWORD. After building the image and creating the container we can confirm whether the env is available inside the container using the printenv option.

After creating the container and executing it we confirmed that the ENV are available in the container.

We can also define env using other methods like passing directly in the docker run command as shown below

docker run -e MYSQLPASSWORD=123456 image:latest

You can also call the docker ENV file inside the Dockerfile using the ENV instruction with the –env-file option. Here’s an example Dockerfile:

FROM ubuntu
COPY my_env_file /app/
ENV –env-file /app/my_env_file

In this example, the Dockerfile copies the my_env_file environment file to the /app/ the directory inside the image. The ENV instruction with the –env-file option then sets the environment variables defined in the file inside the container. Also, the file with the name my_env_file must reside in the current working directory with all env’s variables.

How to combine both ARG and ENV in the docker file.

You can use both build-time arguments and environment variables in a Dockerfile by combining the ARG and ENV instructions. Here’s an example Dockerfile

FROM ubuntu
ARG TEST1
ENV TEST2=$TEST1
RUN echo “The env variable value is $TEST2”

Here we are defining our ARG and assigning that value to the environment variable. So we only need to pass our environment variables in the Buildtime it will also be available in the runtime.

We are going to build the container using the –build-arg

docker build –build-arg TEST1=value1 -t my_image .

The output shows that the docker has processed the ARG and assigned it to the ENV variable. Let’s confirm whether the same env is available inside the container.

As you can see the ENV variable is available in the container also in this way we can add both runtime and build time variables in the docker file.

Summary: After going through this blog post you will get a basic understanding of how to use ENV variables in Docker.

Leave a Reply

Your email address will not be published. Required fields are marked *