Post

Docker Directives - Env Directive

Docker Directives - Env Directive

{% raw %}

The ENV directive in a Dockerfile can be used to set environment variables.

Environment variables are key-value pairs that provide information to applications and processes running inside the container.

They can influence the behavior of programs and scripts by making dynamic values available during runtime.

Environment variables are defined as key-value pairs as per the following format:

ENV <key> <value>

For example, we can set a path using the ENV directive as below,

ENV PATH $PATH:/usr/local/app/bin/

We can set multiple environment variables in the same line separated by spaces. However, in this form, the key and value should be separated by the equal to (=) symbol:

ENV <key>=<value> <key=value> ...

Below, we set two environment variables configured.

The PATH environment variable is configured with the value of $PATH:/usr/local/app/bin, and

the VERSION environment variable is configured with the value of 1.0.0.

ENV PATH=$PATH:/usr/local/app/bin/ VERSION=1.0.0

Once an environment variable is set with the ENV directive in the Dockerfile, this variable is available in all subsequent Docker image layers.

This variable is even available in the Docker containers launched from this Docker image.

Below are some of the examples of using ENV file,

Example 1: Setting a single environment variable

# Use an official Node.js runtime as a parent imageFROM node:14# Set the environment variable NODE_ENV to "production"ENV NODE_ENV=production# Copy package.json and package-lock.json files to the working directoryCOPY package*.json ./# Install app dependencies using the NODE_ENV variableRUN if [ "$NODE_ENV" = "production" ]; then npm install --only=production; else npm install; fi# Copy app source code to the containerCOPY . .# Expose the port the app runs onEXPOSE 8080# Define the command to run the appCMD ["node", "app.js"]##

Example 2: Using Environment Variables in Application Configuration

# Use an official Python runtime as a parent imageFROM python:3.8-slim# Set environment variablesENV APP_HOME=/usr/src/appENV APP_CONFIG=config.ProductionConfig# Create application directory and set it as the working directoryRUN mkdir -p $APP_HOMEWORKDIR $APP_HOME# Copy the current directory contents into the container at /usr/src/appCOPY . .# Install any needed packages specified in requirements.txtRUN pip install --no-cache-dir -r requirements.txt# Use the environment variable in the command to run the applicationCMD ["python", "app.py", "--config", "$APP_CONFIG"]

Example 3: Passing Environment Variables to the Application

# Use an official nginx image as a parent imageFROM nginx:alpine# Set environment variablesENV NGINX_HOST=localhostENV NGINX_PORT=8080# Copy custom configuration file from the current directoryCOPY nginx.conf /etc/nginx/nginx.conf# Replace placeholders in the nginx.conf file with actual environment variable valuesRUN sed -i "s/NGINX_HOST/$NGINX_HOST/g" /etc/nginx/nginx.conf && \    sed -i "s/NGINX_PORT/$NGINX_PORT/g" /etc/nginx/nginx.conf# Expose portsEXPOSE 8080# Start nginxCMD ["nginx", "-g", "daemon off;"]

{% endraw %}

This post is licensed under CC BY 4.0 by the author.