Docker compose up vs up --build vs build --no-cache
Codemia Knowledge Hub ·
docker-compose up reuses existing images, docker-compose up --build rebuilds (using the layer cache) before starting containers, and docker-compose build --no-cache rebuilds every layer from scratch without starting anything. Picking the wrong one either wastes minutes on every iteration or runs stale code.
How Docker layer caching works
Every Dockerfile instruction produces an intermediate layer. Docker hashes the instruction text plus the content of any files added by COPY or ADD. If the hash matches a cached layer, Docker skips execution and reuses the cached result.
If you only change application source, layers 1 through 4 are cache hits and Docker rebuilds only layers 5 and 6. This is what makes iterative builds fast, and it determines which compose command you need.
docker-compose up: reuse existing images
up reads docker-compose.yml, creates networks and volumes if needed, and starts every service. If an image already exists locally, Compose reuses it without rebuilding.
The first run builds the api image because none exists. Later runs reuse that image even if you have changed source files since: docker-compose up does not watch your source tree.
Use plain up when no Dockerfile or application code has changed since the last build, when restarting stopped containers, or when every service uses a pre-built registry image with no local build step.
docker-compose up --build: rebuild with cache, then start
--build makes Compose run the build step before starting containers. Layer caching still applies, so only modified layers and their dependents are rebuilt.
This is the command most developers want during active development. Suppose you edit a route handler:
docker-compose up --build sees that the COPY . . layer's context changed, rebuilds from there, and starts the updated container. Without --build, the old image keeps running your previous code.
--build does not disable the cache. It only guarantees that Compose runs docker build before docker start, which is why it typically adds just seconds with a well-structured Dockerfile.
docker-compose build --no-cache: full rebuild, no start
This rebuilds every image from the first instruction, ignoring all cached layers. It does not start containers afterward.
It is the slowest option. A Node.js project that takes 5 seconds with cache hits might take 90 seconds without them, mostly because npm ci re-downloads every dependency.
A full cache bust is justified when:
- Debugging "works on my machine" issues. A cached layer might hold an artifact from a previous build that masks a real problem.
- The base image was updated. If
node:20-alpinereceived a security patch, cached layers still reference the old base. A no-cache build with--pull(docker-compose build --no-cache --pull) fetches the fresh base;--no-cachealone reuses the local base image. - CI/CD needs reproducibility. Some teams run no-cache builds in CI and accept the time cost.
- Upstream dependency behavior changed. If the lockfile is unchanged but upstream package behavior has changed (rare but real), a cached
npm cilayer hides the difference.
To rebuild one service from scratch and leave the others cached:
Side-by-side comparison
| Command | Builds images? | Uses layer cache? | Starts containers? | Typical use case |
docker-compose up | Only if no image exists | Yes | Yes | Restarting unchanged services |
docker-compose up --build | Always | Yes | Yes | Active development with code changes |
docker-compose build --no-cache | Always | No | No (run up separately) | Debugging, CI, base image refresh |
Dockerfile order matters more than the command
The biggest performance lever is how the Dockerfile is ordered: instructions that change least go at the top, those that change most go at the bottom.
Poorly ordered:
Well ordered:
In the second version a code change only invalidates the final COPY . . layer, and the dependency install stays cached. That alone can turn a 60-second rebuild into a 3-second one with docker-compose up --build.
Multi-stage builds and the cache
Each stage of a multi-stage build has its own cache chain:
With --build, both stages are cached independently. With --no-cache, both rebuild from scratch. Multi-stage builds shrink the final image; proper cache ordering keeps iteration fast on top of that.
Mistakes that cause stale or slow builds
Running plain up after code changes. The existing image is reused, so nothing changes. Use --build.
Using --no-cache by habit. It throws away all caching and can add minutes to every cycle. Reserve it for when you have a specific reason to distrust the cache.
Expecting build --no-cache to start containers. The build command only produces images. Run up afterward.
COPY . . before RUN pip install. Every source change then triggers a full dependency reinstall, whichever compose command you use.
No .dockerignore. Docker then sends everything in the build context, including node_modules, .git and test artifacts, which inflates the context and causes unnecessary cache invalidation.
Related reading
- Docker-Desktop for MAC Kubernetes not creating secrets or token for serviceaccounts
- Docker-Swarm, Kubernetes, Mesos Core-OS Fleet
- docker buildkit not supported by daemon in AWS EKS kubernetes cluster
- Docker Compose - How to execute multiple commands?
- Docker-compose volume mount before run
- docker-compose volumes_from equivalent with version 3
- Docker gets error failed to compute cache key not found - runs fine in Visual Studio
- Docker RabbitMQ persistency
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.