docker-compose
detached mode
container orchestration
DevOps
Docker tutorial

docker-compose for Detached mode

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Detached mode means Docker Compose starts your services in the background and returns control to the shell immediately. It is the normal choice when you want the stack running but do not want to keep one terminal attached to the live logs.

Start a Compose Stack in Detached Mode

The core command is:

bash
docker compose up -d

The -d flag stands for detached. Compose creates the containers, networks, and any other declared resources, then exits while the services keep running in the background.

If you are using the older standalone binary, the equivalent command is:

bash
docker-compose up -d

Verify That the Services Are Running

Once the stack is detached, use ps to see container state:

bash
docker compose ps

If a service exits immediately, detached mode does not hide that forever. docker compose ps will usually show the service as exited, restarting, or unhealthy depending on what the container is doing.

Follow Logs Without Reattaching

Detached mode does not mean you lose visibility. You can still stream logs:

bash
docker compose logs -f

Or focus on one service:

bash
docker compose logs -f api

This is the common workflow: run detached, then inspect logs only when you actually need them.

Stop, Restart, and Remove

Detached services are managed with the same Compose commands as attached ones:

bash
1docker compose stop
2docker compose start
3docker compose restart
4docker compose down

Use down -v if you also want to remove named volumes created by the project:

bash
docker compose down -v

Be careful with that last command because volume removal usually means data loss for local databases and other stateful services.

Rebuild When the Image Changed

Detached mode does not automatically rebuild images when your Dockerfile or dependencies change. If you changed something that affects the image, start the stack again with --build.

bash
docker compose up -d --build

That pattern is especially common in local development when application code is copied into the image instead of mounted as a bind volume.

When Detached Mode Is a Good Fit

Detached mode is useful when:

  • You want the stack running while you keep using the terminal
  • The services are long-lived and stable
  • You prefer to inspect logs only when debugging

Attached mode is still helpful during first-time startup or troubleshooting because it keeps logs in front of you immediately.

That tradeoff is the simplest way to choose: attached mode for active debugging, detached mode for normal background operation. Most teams switch between both depending on the moment. The mode is just a workflow choice. Choose it intentionally. Always.

Common Pitfalls

  • Starting detached and then forgetting to check logs can hide a crash loop for longer than necessary.
  • Port conflicts still happen in detached mode just as they do in attached mode.
  • If you change the image build context or Dockerfile, you may need docker compose up -d --build.
  • 'docker compose down -v removes volumes, so do not use it casually on data you care about.'

Summary

  • Use docker compose up -d to start services in the background.
  • Use docker compose ps and docker compose logs -f to monitor them afterward.
  • Detached mode is convenient for normal development and long-running local stacks.
  • Stopping and removing the stack works the same way as usual, but volume removal is destructive.

Course illustration
Course illustration

All Rights Reserved.