Docker
Docker-Compose
Project Management
Containerization
Software Development

Running multiple projects using docker which each runs with docker-compose

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

You can run multiple Compose-based projects on the same machine as long as their containers, networks, volumes, and host ports do not collide. In practice, Docker Compose already gives you most of the isolation you need, because each project gets its own Compose project name and resource prefix.

How Compose Separates Projects

When you run docker compose up in different directories, Compose usually uses the directory name as the project name. That project name is then used to namespace resources such as networks and containers.

For example, two directories might produce resources like:

  • 'projecta_web_1'
  • 'projectb_web_1'

That prevents most conflicts automatically, even if both projects have services with the same names in their compose.yaml files.

A Typical Workflow

Imagine two projects:

text
~/work/project-a
~/work/project-b

Each one has its own Compose file. You can start them independently:

bash
1cd ~/work/project-a
2docker compose up -d
3
4cd ~/work/project-b
5docker compose up -d

If the services use different host ports, both projects can run at the same time without trouble.

The Main Real-World Conflict: Host Ports

Compose project names isolate container names and networks, but they do not magically solve host port conflicts. If both projects publish 8080:8080, only one of them can bind port 8080 on the host.

Example:

yaml
1services:
2  web:
3    build: .
4    ports:
5      - "8081:8080"

The second project should use a different host port such as 8081, 8082, or another free port.

Use Explicit Project Names When Needed

If you want more control than the directory name provides, set the project name explicitly with -p or COMPOSE_PROJECT_NAME.

bash
docker compose -p billing up -d
docker compose -p analytics up -d

This is useful in CI, in temporary environments, or when multiple clones of the same repository run on one machine.

Inspect and Manage Each Project Separately

Once several stacks are running, it helps to query them by project name so you can see exactly which containers belong to which app.

bash
docker compose -p billing ps
docker compose -p analytics ps

The same rule applies when stopping or removing them. Use the same project name or directory context that you used on startup so you do not accidentally operate on the wrong stack. That keeps day-to-day cleanup predictable. It reduces operator mistakes.

Volumes and Networks Still Need Thought

Compose prefixes named volumes and networks with the project name, which helps a lot. But if you deliberately configure external volumes or hard-code shared network names, you can still create collisions or accidental cross-project communication.

For most local development setups, the safest rule is simple:

  • Let each project keep its own default network
  • Use project-scoped named volumes unless shared data is intentional

Common Pitfalls

  • The most common failure is port collision, not container-name collision.
  • Running multiple copies of the same repository from directories with the same effective project name can cause resource overlap unless you set -p.
  • External volumes and shared networks bypass some of Compose's built-in isolation.
  • Remember to stop the right project with the same directory or project name you used to start it.

Summary

  • Multiple Compose projects can run side by side on one machine.
  • Compose usually isolates them automatically through project names based on directory names.
  • Host ports must still be unique across projects.
  • Use -p or COMPOSE_PROJECT_NAME when you need explicit, predictable separation.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.