How to define a variable in a Dockerfile?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Docker, a Dockerfile is a script capable of creating a Docker image by executing a sequence of commands written in a plain text file. Each line in a Dockerfile represents a distinct instruction responsible for packaging our application into an image. One essential concept when writing a Dockerfile is defining and managing variables. Variables can enhance readability, reusability, and flexibility by abstracting parameters that might change based on the environment or requirements.
Using Variables in Dockerfile
In a Dockerfile, variables can be defined using the ENV instruction. The ENV directive establishes an environment variable and assigns a value to it. This variable remains available for use during all subsequent commands executed within the Dockerfile.
Syntax of ENV Command
The ENV command syntax comprises one or several key-value pairs, separated by spaces. You can define a single variable per line or multiple variables in a single ENV command.
Example Usage
Here's a simple example demonstrating how variables can be defined and used in a Dockerfile:
Benefits of Using Variables
- Reusability: By using variables, you avoid repetition and make it easier to reuse the same Dockerfile across different deployments or environments. Changes to key paths or configuration parameters can be modified in a single place, impacting the entire build process.
- Clarity: Using meaningful variable names improves the readability of the Dockerfile, making it easier to understand the intentions behind various commands and their expected outputs.
- Flexibility: Variables offer flexibility for conditional adjustments depending on the environment or context. This is particularly helpful in continuous integration and deployment pipelines where you may want different settings for development, testing, and production environments.
Best Practices for Defining Variables
- Use Upper Case for Variable Names: By convention, we use uppercase for environment variable names to differentiate them from regular shell variables.
- Ensure Unique Naming: Choose descriptive and unique variable names to prevent unintentional overwriting or conflicts with other environment variables.
- Avoid Hardcoding Sensitive Values: Do not hardcode sensitive information such as passwords or API keys in the Dockerfile. Instead, consider using Docker secrets or external environment variable files.
Summary Table
The table below outlines key practices and concepts when using variables in a Dockerfile:
| Concept/Practice | Description |
| Syntax | ENV <key>=<value> pairs can be defined inline or separately. |
| Scope | Variables persist across subsequent instructions in the build. |
| Flexibility | Facilitates environment-specific builds by adjusting variables. |
| Readability | Variables make Dockerfiles easier to manage and read. |
| Best Practice | Use uppercase names; avoid storing sensitive data within files. |
| Usage | Allows reuse and reduces repetition across Dockerfile setup. |
By understanding and using variables effectively in Dockerfiles, developers can enhance maintainability and control over containerized application deployments. These variables serve to decouple configuration specifics from the Dockerfile logic, which is invaluable as applications scale and evolve.
Related reading
- How to define build-args in docker-compose?
- How to delete a docker image?
- How to delete images from a private docker registry?
- How to Delete Kubernetes Service
- How to delete a deployment / image in kubernetes
- How to delete a label for a kubernetes pod
- How to delete untagged images from AWS ECR Container Registry
- How to deploy in kubernetes without any changes, just to get pods to cycle

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.