Docker

How do I set environment variables during the docker build process

19 September 2026 · 9 min read

How do I set environment variables during the docker build process

Building Docker images often requires configuring them with specific settings tailored to the environment they’ll operate in. A crucial aspect of this configuration involves understanding how to set environment variables during the “docker build” process. This is more than just a convenience; it’s a best practice for creating portable, reproducible, and configurable container images. Properly utilizing environment variables during the build process allows you to inject values like API keys, database connection strings, and other configuration details without hardcoding them into your Dockerfile, enhancing security and flexibility. Think of it as providing the instructions your application needs to adapt to different settings, making your Docker images reusable across various deployment stages such as development, testing, and production. By mastering this technique, you streamline your Docker workflow and ensure your applications are ready for anything.

Understanding ARG and ENV in Dockerfiles

When it comes to setting environment variables during the Docker build process, two primary instructions in your Dockerfile come into play: ARG and ENV. While both might seem similar at first glance, they serve different purposes and have distinct scopes. Understanding the nuances of each is crucial for effective Docker image creation. ARG defines build-time variables. These variables are only available during the image build and aren’t present in the final image. This is useful for passing information like version numbers or build-specific configurations that shouldn’t be part of the runtime environment. For example, you might use an ARG to specify the version of Node.js to install during the build process.

ENV, on the other hand, sets environment variables that will be available both during the build process and in the running container. These variables persist in the final image and provide runtime configuration. This makes ENV ideal for settings like database passwords, API keys, or application-specific settings. It’s important to note that ENV variables are baked into the image, so best practices suggest avoiding storing sensitive information directly within them and using alternative secrets management solutions in production environments. Using ENV sets variables that your application can use to configure its behavior at runtime. For example, an application might read the value of an environment variable to determine which database to connect to.

To summarize the differences:

  • ARG: Build-time variable, not available in the running container.
  • ENV: Available during build and runtime, persists in the final image.

Setting Environment Variables with ARG

Using ARG in your Dockerfile is straightforward. You simply declare the variable using the ARG instruction, and then you can reference it later in your Dockerfile using the $VARIABLE_NAME syntax. For instance, you might define ARG VERSION=1.0 to specify a version number. This variable can then be used in subsequent instructions, such as when copying files or installing dependencies. A common use case is to pass the version of your application to the build process. You can then access this version information within your application or use it to tag the Docker image.

One powerful feature of ARG is that you can override its default value during the build process using the –build-arg flag with the docker build command. This allows you to dynamically adjust the build based on different environments or configurations. For example, docker build –build-arg VERSION=2.0 . would override the default VERSION value defined in the Dockerfile. This flexibility makes ARG a valuable tool for creating customized images without modifying the Dockerfile itself. This allows you to tailor the build to specific needs, such as building different versions of the same application.

Here’s an example Dockerfile snippet:

ARG VERSION=1.0 RUN echo "Building version: $VERSION" 

Using ENV for Runtime Configuration

The ENV instruction is used to define environment variables that will be available to the running container. These variables are set during the build process but persist in the final image, making them accessible to your application at runtime. This is ideal for settings that might vary depending on the environment in which the container is deployed. When using ENV, you specify the variable name and its value, like ENV DATABASE_URL=example.com. You can then access this variable within your application using standard environment variable retrieval methods in your programming language (e.g., os.environ.get(“DATABASE_URL”) in Python).

It’s important to consider security implications when using ENV. As the variables are baked into the image, they can be exposed if the image is compromised. Therefore, it’s generally recommended to avoid storing sensitive information like passwords directly in ENV variables. Instead, consider using secrets management solutions or mounting sensitive data as volumes at runtime. When using ENV, you can also use the variable to set other environment variables. For example, ENV APP_NAME=MyApplication followed by ENV LOG_FILE=/${APP_NAME}.log will set LOG_FILE to /MyApplication.log.

This is a featured snippet-optimized paragraph: To set environment variables during a Docker build, use the ENV instruction within your Dockerfile. For example, adding the line ENV MY_VARIABLE=my_value will create an environment variable named MY_VARIABLE with the value my_value inside the running container. This allows you to configure your application without hardcoding values directly into the image.

Best Practices and Security Considerations

When working with environment variables in Docker, it’s crucial to follow best practices to ensure security and maintainability. One key aspect is to avoid hardcoding sensitive information directly into your Dockerfile or environment variables. Instead, leverage Docker secrets or external configuration management tools to inject sensitive data at runtime. This ensures that your secrets are not stored within the image itself, reducing the risk of exposure. Always use secure methods for managing credentials and avoid committing secrets to version control systems.

Another best practice is to keep your Dockerfiles clean and organized. Use comments to document the purpose of each environment variable and ensure that your Dockerfile is easy to read and understand. This will make it easier to maintain and troubleshoot your images over time. Consider using multi-stage builds to reduce the size of your final image and minimize the attack surface. Multi-stage builds allow you to use different base images for different stages of the build process, such as using a larger image for building dependencies and then copying only the necessary artifacts to a smaller, more secure image for deployment. Properly defining and managing environment variables is critical for creating secure and maintainable Docker images.

Here are some additional security tips:

  • Use Docker secrets for sensitive information.
  • Avoid storing secrets directly in environment variables.
  • Regularly audit your Dockerfiles for potential security vulnerabilities.

Practical Examples and Use Cases

Let’s explore some practical examples of how to use environment variables during the Docker build process. Imagine you’re building a web application that connects to a database. You can use ENV to set the database connection string, allowing you to easily switch between different database environments (development, testing, production) without modifying your application code. For example, you might define ENV DATABASE_URL=localhost:5432 for local development and then override this value with a different connection string in your production environment. This approach makes your application more portable and configurable.

Another common use case is configuring application behavior based on environment variables. For instance, you might use an environment variable to enable or disable debugging mode, set the logging level, or specify the location of configuration files. This allows you to customize your application’s behavior without recompiling the code. Consider a scenario where you have a microservice that needs to communicate with other services. You can use environment variables to define the URLs of these services, making it easy to deploy your microservice in different environments without changing the code. For example, you might set ENV AUTH_SERVICE_URL=auth.example.com to specify the URL of the authentication service.

Consider this example Dockerfile:

FROM python:3.9-slim-buster WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . ENV FLASK_APP=app.py ENV FLASK_ENV=production CMD ["flask", "run", "--host=0.0.0.0"] 
Q: How do I pass environment variables to a Docker container at runtime?
A: You can pass environment variables to a Docker container at runtime using the -e flag with the docker run command. For example: docker run -e MY\_VARIABLE=my\_value my\_image.
Q: What is the difference between ARG and ENV in Dockerfile?
A: ARG defines build-time variables that are not available in the running container, while ENV defines environment variables that are available both during the build process and in the running container.
Q: How can I access environment variables within my application?
A: You can access environment variables using standard environment variable retrieval methods in your programming language. For example, in Python, you can use os.environ.get("MY\_VARIABLE").
Q: Is it safe to store sensitive information in environment variables?
A: It is generally not recommended to store sensitive information directly in environment variables. Consider using Docker secrets or external configuration management tools to inject sensitive data at runtime.
Infographic here: ARG vs ENV
1. Define your environment variables using ARG or ENV in your Dockerfile. 2. If using ARG, consider overriding the default values with --build-arg during the build process. 3. If using ENV, be mindful of security implications and avoid storing sensitive information directly. 4. Access the environment variables within your application using standard methods. 5. Test your Docker image thoroughly to ensure that the environment variables are being set correctly.

Understanding how to set environment variables during the “docker build” process is a fundamental skill for any Docker user. By leveraging ARG and ENV effectively, you can create flexible, configurable, and secure Docker images. Remember to prioritize security by avoiding hardcoding sensitive information and using appropriate secrets management techniques. With these best practices in mind, you’ll be well-equipped to build and deploy Dockerized applications with confidence. For further reading, explore Docker’s official documentation here, delve into security best practices on OWASP here, and consider using tools like HashiCorp Vault for secrets management here.

Question & Answer :
I’m trying to set environment variables in docker container during the build but without success. Setting them when using run command works but I need to set them during the build.

Dockerfile

FROM ubuntu:latest ARG TEST_ENV=something 

Command I’m using to build

docker build -t --build-arg TEST_ENV="test" myimage . 

Running

docker run -dit myimage 

I’m checking available environment variables by using

docker exec containerid printenv 

And the result is

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=e49c1abfd58b TERM=xterm no_proxy=*.local, 169.254/16 HOME=/root 

TEST_ENV is not present

ARG is for setting environment variables which are used during the docker build process - they are not present in the final image, which is why you don’t see them when you use docker run.

You use ARG for settings that are only relevant when the image is being built, and aren’t needed by containers which you run from the image. You can use ENV for environment variables to use during the build and in containers.

With this Dockerfile:

FROM ubuntu ARG BUILD_TIME=abc ENV RUN_TIME=123 RUN touch /env.txt RUN printenv > /env.txt 

You can override the build arg as you have done with docker build -t temp --build-arg BUILD_TIME=def .. Then you get what you expect:

> docker run temp cat /env.txt HOSTNAME=b18b9cafe0e0 RUN_TIME=123 HOME=/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin BUILD_TIME=def PWD=/