Docker
Dockerfile
variables
environment variables
DevOps

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.

Practice system design

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

dockerfile
ENV <key>=<value>

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:

dockerfile
1# Start with a base image
2FROM ubuntu:20.04
3
4# Define variables
5ENV APP_HOME=/usr/src/app \
6    APP_ENV=production
7
8# Create the app directory
9RUN mkdir -p $APP_HOME
10
11# Set the working directory
12WORKDIR $APP_HOME
13
14# Copy source code into the working directory
15COPY . $APP_HOME 
16
17# Install dependencies
18RUN apt-get update && apt-get install -y python3-pip
19
20# Install app dependencies
21RUN pip3 install --no-cache-dir -r requirements.txt
22
23# Expose application port
24EXPOSE 80
25
26# Command to run application
27CMD ["python3", "app.py"]

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/PracticeDescription
SyntaxENV <key>=<value> pairs can be defined inline or separately.
ScopeVariables persist across subsequent instructions in the build.
FlexibilityFacilitates environment-specific builds by adjusting variables.
ReadabilityVariables make Dockerfiles easier to manage and read.
Best PracticeUse uppercase names; avoid storing sensitive data within files.
UsageAllows 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.