Dockerfile
if else
conditional statements
external arguments
containerization

Dockerfile if else condition with external arguments

Codemia Knowledge Hub ·

Dockerfiles have no if-else instruction, but you can get the same result with a shell if inside a RUN instruction that tests a build argument. Declare the argument with ARG, pass it with docker build --build-arg, and the branch runs at build time.

Why a Dockerfile needs the shell for conditionals

A Dockerfile is a list of instructions (FROM, COPY, RUN, CMD and others) that tell Docker how to assemble an image. None of them are control flow statements, so conditional logic comes from Unix shell constructs inside RUN, evaluating variables passed as build arguments or environment variables.

Dockerfile with an if/else on BUILD_ENV

Here the behavior depends on an external argument BUILD_ENV, which is either development or production.

dockerfile
1# Use an official base image
2FROM ubuntu:20.04
3
4# Define ARG and ENV variables
5ARG BUILD_ENV
6ENV APP_HOME=/app
7
8# Set work directory
9WORKDIR $APP_HOME
10
11# Copy source code to the container
12COPY . .
13
14# Conditional Logic via Shell
15RUN if [ "$BUILD_ENV" = "production" ]; then \
16      echo "Building for production environment"; \
17      apt-get update && apt-get install -y nginx; \
18    else \
19      echo "Building for development environment"; \
20      apt-get update && apt-get install -y nginx curl; \
21    fi
22
23# Expose necessary ports
24EXPOSE 80
25
26# Command to run on container start
27CMD ["nginx", "-g", "daemon off;"]
  • Arguments and environment variables: ARG defines BUILD_ENV, and ENV sets APP_HOME.
  • Shell conditional: the RUN instruction checks BUILD_ENV. If it is 'production', it installs nginx. Otherwise it installs nginx plus curl, so the CMD still has nginx to start.
  • Execution: pass BUILD_ENV with --build-arg when you build:
bash
docker build --build-arg BUILD_ENV=production -t myapp:latest .

Ways to pass the external argument

External arguments let you change build behavior without editing the Dockerfile:

  1. --build-arg: as shown above, sets build-time variables.
  2. Default values: an ARG can be given a default value in the Dockerfile and overridden at build time.
  3. Docker Compose: define build arguments in docker-compose.yml:
yaml
1version: '3.8'
2services:
3  web:
4    build:
5      context: .
6      args:
7        BUILD_ENV: "development"
FeatureUsage
Argument (ARG)Declare build-time variables inside Dockerfile. Used with --build-arg during docker build.
Environment (ENV)Set environment variables accessible during build and run time.
ConditionalsAchieved using shell scripting inside RUN commands.
Use CaseModify INSTALL step based on environment for lighter production images.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design