Curious Minds

Clear answers to science and history questions

Docker Compose Detached Mode: How -d Works

· 6 min read · Science

Docker Compose's detached mode is enabled by adding the -d (or --detach) flag to docker compose up, which starts all services defined in the compose file in the background and returns your terminal prompt immediately instead of streaming their logs. In detached mode Compose creates the containers, networks, and volumes, starts them, prints a short status summary, and exits — the containers keep running under the Docker daemon until you stop them with docker compose down or docker compose stop.

What "Detached" Actually Means in Compose

Docker Compose is a client that talks to the Docker daemon over the Docker API. When you run docker compose up without flags, the Compose process stays in the foreground: it attaches to every container's stdout/stderr and multiplexes the output into your terminal, and pressing Ctrl+C sends a stop signal to the containers. That is convenient for development but ties up a terminal and means the containers die when the session ends.

With docker compose up -d, Compose performs the same creation and start operations but does not attach to the containers' output streams. It waits until the containers have been started (not until they are healthy, unless you ask for that), prints a table of container names and states, and then exits. The containers are now owned by the daemon, not by your shell, so closing the terminal, logging out, or losing an SSH connection does not stop them.

A common misconception is that detached mode is a separate run mode with different behaviour inside the containers. It is not: the same containers run the same commands. The only difference is whether the Compose client stays attached to their streams and whether it forwards signals from your terminal.

How the Startup Sequence Works

Understanding the order of operations helps explain why detached mode sometimes appears to "finish" before your application is ready.

  1. Compose reads and validates compose.yaml (or docker-compose.yml), resolving variables, profiles, and overrides.
  2. It checks whether the project's network exists and creates it if needed.
  3. It creates or recreates containers whose configuration has changed, and creates named volumes declared in the file.
  4. It starts containers, respecting depends_on ordering where defined.
  5. It prints a summary such as Container myapp-web-1 Started and returns control to the shell.

Step 4 is the important one. A plain depends_on only waits for the dependency container to be started, not for the service inside it to be ready to accept connections. Detached mode makes this gap more visible because you no longer see the dependency's logs scrolling past. The fix is a healthcheck plus depends_on with condition: service_healthy, which makes Compose wait for the healthcheck to pass before starting dependents.

Working With Logs and Status After Detaching

Once detached, you interact with the stack through follow-up commands rather than the original terminal session.

  • docker compose logs -f re-attaches to the aggregated log stream; add a service name to narrow it.
  • docker compose ps lists the project's containers and their states, including health status.
  • docker compose stop stops containers without removing them; docker compose start brings them back.
  • docker compose down stops and removes containers and networks; add -v to also delete named volumes.
  • docker compose exec <service> sh opens a shell inside a running container.

Because Compose identifies a project by the directory name (or the -p flag / COMPOSE_PROJECT_NAME variable), these commands work from the same directory later, even in a new shell. Running them from a different directory without specifying the project name will target a different, probably empty, project.

Detached Mode vs Foreground Mode: When to Use Which

Neither mode is universally better; they suit different workflows.

  • Foreground is better while actively debugging a service, because logs appear live and Ctrl+C cleanly stops the stack.
  • Detached is better for long-running local stacks, for running tests or builds against a stack, and on servers where the terminal session is not meant to persist.
  • Detached with --wait combines the two: Compose returns only after containers are running or healthy, which is useful in scripts and CI pipelines.
  • Detached with --build rebuilds images before starting, so a single command can pick up code changes.

Note that docker compose up -d is idempotent in a useful way: running it again reconciles the running stack with the file, recreating only containers whose configuration changed. That makes it a reasonable "apply" command in a development loop.

Common Problems and How to Diagnose Them

Most detached-mode confusion comes from a small set of issues.

  • Containers exit immediately. The summary shows Exited (1) or similar. Check docker compose logs; a foreground process that finishes (a one-shot script, a misconfigured command) leaves nothing to keep the container alive.
  • Port already allocated. Another process or a previous stack holds the host port. Stop the old stack or change the published port mapping.
  • Application not reachable right after up -d returns. The container is running but the app inside is still booting. Add a healthcheck and use --wait or condition: service_healthy.
  • Changes to the compose file seem ignored. Compose recreates containers only when it detects configuration drift; if you edited a mounted file rather than the compose file, restart the service instead.
  • Orphan containers. Containers from a renamed or removed service linger; docker compose up -d --remove-orphans cleans them up.

In each case the diagnostic path is the same: docker compose ps for state, then docker compose logs for the reason.

Detached Mode and Process Lifecycle

It is worth being precise about what keeps a container alive. A container runs as long as its main process (PID 1 inside the container) runs. Detaching does not change this; it only changes whether your terminal is watching. If the main process is a shell script that starts a server in the background and then exits, the container exits too, regardless of mode. This is why foreground-style commands such as nginx -g 'daemon off;' or running the application directly as PID 1 are standard practice in images.

Signal handling also matters. In foreground mode, Ctrl+C is forwarded to the containers, and Docker sends SIGTERM, waits for the configured stop timeout (10 seconds by default), then SIGKILL. In detached mode you trigger the same sequence explicitly with docker compose stop or down. Applications that ignore SIGTERM will be killed after the timeout, which can interrupt in-flight work; setting an appropriate stop_grace_period in the compose file gives them more time.

Frequently Asked Questions

Does docker compose up -d keep running after I close the terminal?

Yes. The containers are managed by the Docker daemon, not by your shell. Closing the terminal, disconnecting SSH, or logging out does not stop them. They keep running until you stop or remove them, or the daemon or host restarts (unless containers are configured to restart automatically).

How do I see the logs of a detached stack?

Run docker compose logs from the project directory, optionally with -f to follow output live and a service name to filter. Compose stores container logs through the Docker logging driver, so past output is still available after detaching.

What is the difference between -d and --wait?

-d returns as soon as containers have started. --wait also detaches but blocks until containers are running or healthy, and exits with a non-zero code if any fail. --wait implies -d.

Why did my container exit right after starting in detached mode?

Almost always because the container's main process finished. Detached mode does not hide this; it just returns your prompt before you notice. Check docker compose logs and docker compose ps to see the exit code and the last output.

Can I run docker compose up -d twice?

Yes. It reconciles the running stack with the compose file, recreating only containers whose configuration changed and leaving unchanged ones running. This makes it safe to use as a repeatable apply command.

Does detached mode work the same on Windows and macOS?

The flag behaves identically because it is implemented in the Compose client, which talks to the Docker daemon the same way on every platform. Differences you might notice come from the container runtime and filesystem, not from detached mode itself.

In this article
  1. What "Detached" Actually Means in Compose
  2. How the Startup Sequence Works
  3. Working With Logs and Status After Detaching
  4. Detached Mode vs Foreground Mode: When to Use Which
  5. Common Problems and How to Diagnose Them
  6. Detached Mode and Process Lifecycle
  7. Frequently Asked Questions