❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Docker Directives – Env Directive

9 July 2024 at 01:45

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 image
FROM 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 directory
COPY package*.json ./

# Install app dependencies using the NODE_ENV variable
RUN if [ "$NODE_ENV" = "production" ]; then npm install --only=production; else npm install; fi

# Copy app source code to the container
COPY . .

# Expose the port the app runs on
EXPOSE 8080

# Define the command to run the app
CMD ["node", "app.js"]
##

Example 2: Using Environment Variables in Application Configuration


# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set environment variables
ENV APP_HOME=/usr/src/app
ENV APP_CONFIG=config.ProductionConfig

# Create application directory and set it as the working directory
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Use the environment variable in the command to run the application
CMD ["python", "app.py", "--config", "$APP_CONFIG"]

Example 3: Passing Environment Variables to the Application


# Use an official nginx image as a parent image
FROM nginx:alpine

# Set environment variables
ENV NGINX_HOST=localhost
ENV NGINX_PORT=8080

# Copy custom configuration file from the current directory
COPY nginx.conf /etc/nginx/nginx.conf

# Replace placeholders in the nginx.conf file with actual environment variable values
RUN 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 ports
EXPOSE 8080

# Start nginx
CMD ["nginx", "-g", "daemon off;"]

❌
❌