Passing command line arguments in Docker Compose

You can set environment variables.

MODE=prod docker compose up -d --build

Or

Create a .env file with the content

MODE=prod

and run

docker compose up -d --build

Or create two env files if you don’t want to change the content of a single file:

docker compose --env-file prod up --build
# or
docker compose --env-file dev up --build

“prod” and “dev” then must contain MODE=prod and MODE=dev

So you can use the variable in the compose file:

services:
  service:
    build:
      args:
        MODE: $MODE

Note that while --env-file passed to docker run sets the variables in the container, the same parameter passed to docker compose sets the variables to be used in the compose file.

1 Like