KhueApps
Home/DevOps/Fix 'unknown shorthand flag: -d' in docker compose up

Fix 'unknown shorthand flag: -d' in docker compose up

Last updated: October 07, 2025

Overview

If you see: unknown shorthand flag: -d when running docker compose, it almost always means the -d flag was placed before the subcommand. In Docker Compose (v1 and v2), options belong to the subcommand (up), not to the compose command group.

  • Wrong: docker compose -d up
  • Right: docker compose up -d

This applies equally to docker-compose (v1) and docker compose (v2 plugin).

Quickstart Fix

  1. Use the correct flag position
  • For Compose v2: docker compose up -d
  • For Compose v1: docker-compose up -d
  • Long form is equivalent: docker compose up --detach
  1. Verify your Compose variant
  • docker compose version # v2 (plugin)
  • docker-compose version # v1 (standalone)
  1. Stop and clean up when done
  • docker compose down or docker-compose down

Minimal Working Example

Create a docker compose file and bring it up detached.

compose.yaml:

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"

Commands:

# Start in detached mode (correct flag position)
docker compose up -d

# Verify it’s running
docker compose ps

# Test from another terminal
echo "Requesting http://localhost:8080" && curl -sI localhost:8080 | head -n1

# Tear down when finished
docker compose down

If you only have v1:

docker-compose up -d
curl -sI localhost:8080 | head -n1
docker-compose down

Why the Error Happens (Flag Position)

Docker CLI syntax is hierarchical:

  • docker compose [COMMAND] [OPTIONS]
  • docker-compose [COMMAND] [OPTIONS]

-d (or --detach) is an option for the up command, not for the compose group itself. When you run docker compose -d up, the CLI tries to apply -d to compose rather than to up and fails with unknown shorthand flag: -d.

Common Wrong vs Correct Commands

WrongCorrect
docker compose -d updocker compose up -d
docker-compose -d updocker-compose up -d
docker compose -d up --builddocker compose up --build -d
docker compose -ddocker compose up -d (or another subcommand)

Tip: Place flags after the subcommand they configure.

Step-by-Step: Diagnose and Fix

  1. Check which binary you’re using
docker compose version || true
docker-compose version || true
  • If docker compose version prints a v2 version, you’re on Compose V2 (plugin).
  • If docker-compose version prints, that’s the v1 standalone.
  1. Show help for the up subcommand
docker compose help up
# or
docker-compose help up

Confirm -d, --detach is listed under up.

  1. Re-run with correct ordering
docker compose up -d
# or
docker-compose up -d
  1. If an alias is interfering, bypass it
  • Use the full path: command -v docker-compose and run that explicitly.
  • Or quote/escape if your shell expands an alias.
  1. Confirm success
docker compose ps
# Should show the service(s) in running state

Pitfalls and How to Avoid Them

  • Mixing v1 and v2: docker-compose (v1) and docker compose (v2) are different entry points, but the flag rule is the same: flags go after the subcommand.
  • Global vs subcommand flags: Assume flags like -d apply to up, not to the compose group. Always check docker compose help up.
  • Shell history muscle memory: If you often type docker compose -d up, make a shell snippet or alias to correct usage, or use the long form --detach to remind yourself it belongs to up.
  • CI scripts: Ensure scripts (Dockerfile or CI YAML) use the correct order; failing builds may silently retry until timeout.
  • Using old examples: Some blog posts show incorrect ordering; verify against the built-in help.

Performance Notes

  • Detached mode returns the prompt immediately: docker compose up -d starts containers in the background, which speeds up interactive workflows and CI steps that shouldn’t stream logs.
  • Building efficiently: If you need a rebuild, combine flags correctly: docker compose up --build -d. This keeps layers cached and avoids unnecessary rebuilds.
  • Fewer processes attached: Running detached avoids persistent log streaming and reduces terminal I/O, which can be a bottleneck in slow CI executors.
  • Stop what you don’t need: docker compose down frees resources (CPU/memory) and speeds future runs by keeping image layers cached.

Additional Examples

Start and tail logs when needed:

docker compose up -d
docker compose logs -f web

Recreate containers with changes:

docker compose up -d --force-recreate

Specify a project name and detach:

docker compose -p myproj up -d
# Note: -p applies to the compose group, but -d still belongs to up

Tiny FAQ

  • Q: Why does docker compose -d up fail?

    • A: -d is an option for up. Place it after up: docker compose up -d.
  • Q: Is docker-compose up -d still valid?

    • A: Yes, for Compose v1. For v2, prefer docker compose up -d.
  • Q: Can I use the long flag?

    • A: Yes. docker compose up --detach is equivalent to -d.
  • Q: How do I check available flags?

    • A: Run docker compose help up (or docker-compose help up).
  • Q: I fixed the order but still get an error.

    • A: Ensure you’re not hitting an alias or old binary. Check versions and run the command with its full path.

Series: Docker

DevOps