Skip to content
WorktreeWise
#docker-compose#docker#microservices#git-worktree

Docker Compose with worktrees

W

WorktreeWise Engineering Team

Updated Sep 20267 min read

TL;DR: The 30-Second Summary

By default, Docker Compose uses the project directory name as its project name, causing sibling worktrees to overwrite each other's networks and volumes. Set `COMPOSE_PROJECT_NAME` per worktree to achieve 100% isolated Compose stacks.

The Real-Life Scenario

A microservices project uses `docker compose up -d` to spin up PostgreSQL, Redis, and API containers. When running Compose in a second worktree, Docker Compose recreates and destroys the containers from the first worktree!

Docker Compose is the standard tool for multi-container local environments. When combined with Git worktrees, you can run multiple complete application stacks in parallel. However, understanding how Compose names projects, networks, and volumes is essential to prevent destructive collisions.

How Docker Compose Determines Project Names

If two worktrees are named similarly or if `COMPOSE_PROJECT_NAME` is not set, Compose treats them as the same application and recreates existing containers.

What you see in the terminal:

terminal output
$ cd ../worktree-b && docker compose up -d
Recreating worktree-db_1 ... done
Recreating worktree-api_1 ... done
[Worktree A] Connection to database lost!
🔍

Under the Hood: Git Plumbing & Architecture

Compose uses the project name to prefix all containers (`<project>_<service>_1`), volumes (`<project>_<data>`), and networks (`<project>_default`). If two worktrees share the same project name, their volumes and networks collide.

Quick Command Recipes

Copy and adapt these commands directly in your terminal:

Isolate Compose project via env variable

Prefixes all containers, networks, and volumes with `wt-feat-billing`.

bash
COMPOSE_PROJECT_NAME=wt-feat-billing docker compose up -d

Specify dedicated .env file with Compose

Loads worktree-specific port and project name variables cleanly.

bash
docker compose --env-file .env.worktree up -d
TUTORIAL

Step-by-Step Practical Walkthrough

Follow these verified steps to safely resolve the issue and guarantee that your filesystem and Git references are in sync.

1

Define dynamic project name in `.env`

Add `COMPOSE_PROJECT_NAME=app-feat-search` to the worktree's `.env` file.

2

Parameterize external ports in `docker-compose.yml`

Use `${APP_PORT:-3000}:3000` and `${DB_PORT:-5432}:5432` in your compose file.

3

Launch the stack

Start the multi-container stack in detached mode.

terminal
docker compose up -d
Expected Output:
Creating network app-feat-search_default
Creating app-feat-search_db_1 ... done
4

Verify stack isolation

Check running Compose projects across worktrees.

terminal
docker compose ls
Expected Output:
NAME                 STATUS    CONFIG FILES
app-main             running   docker-compose.yml
app-feat-search      running   docker-compose.yml

Edge Cases & Advanced Scenarios

Disk space from multiple database volumes

Multiple Compose stacks create multiple Docker volumes. Run `docker volume prune` periodically during maintenance.

⚠️

Common Mistakes to Avoid

❌ Mistake: Hardcoding host ports like `5432:5432` in `docker-compose.yml`

Why it causes trouble: The second worktree cannot bind to 5432 and fails to start.

What to do instead: Always use environment variables for host ports (`${DB_PORT:-5432}:5432`).

Verification Checklist

  • `docker compose ls` shows separate project entries for each worktree
  • Each worktree interacts strictly with its own database and services
💡

Senior Engineering Tips

  • WorktreeWise can automatically inject `COMPOSE_PROJECT_NAME=${worktree.name}` into terminal sessions.

Key Takeaways

  • 01.Always set `COMPOSE_PROJECT_NAME` for each worktree.
  • 02.Parameterize host port mappings in `docker-compose.yml`.
  • 03.Enjoy fully isolated multi-container stacks running side by side.
TOOL

How WorktreeWise Solves This Visually

WorktreeWise eliminates the manual friction and mental overhead of CLI flags. It displays real-time branch states, uncommitted modifications, active terminals, and lock statuses across all worktrees on a single visual dashboard.

WorktreeWise Environment Isolation screen for Docker Compose stacks
WorktreeWise Environment Isolation screen for Docker Compose stacks
W

WorktreeWise Engineering Publication

Written and curated by the core WorktreeWise team. We build developer tools that turn Git worktrees, workflows, and parallel AI coding agents into second nature.

Related Isolation Guides

View all isolation