docker-compose
container orchestration
build caching
development tools
docker commands

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.

dockerfile
1# Layer 1 - base image
2FROM node:20-alpine
3# Layer 2 - working directory
4WORKDIR /app
5# Layer 3 - dependency manifest
6COPY package*.json ./
7# Layer 4 - install dependencies
8RUN npm ci
9# Layer 5 - application source
10COPY . .
11# Layer 6 - build step
12RUN npm run build

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.

yaml
1services:
2  api:
3    build: ./api
4    ports:
5      - "3000:3000"
6    depends_on:
7      - db
8  db:
9    image: postgres:16-alpine
10    environment:
11      POSTGRES_PASSWORD: devpass
12    volumes:
13      - pgdata:/var/lib/postgresql/data
14
15volumes:
16  pgdata:
bash
docker-compose up -d

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.

bash
docker-compose up --build -d

This is the command most developers want during active development. Suppose you edit a route handler:

python
1# api/routes/health.py - changed file
2from flask import Blueprint
3
4health_bp = Blueprint("health", __name__)
5
6@health_bp.get("/health")
7def health_check():
8    return {"status": "ok", "version": "2.1.0"}  # bumped version

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.

bash
docker-compose build --no-cache
docker-compose up -d

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-alpine received 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-cache alone 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 ci layer hides the difference.

To rebuild one service from scratch and leave the others cached:

bash
docker-compose build --no-cache api

Side-by-side comparison

CommandBuilds images?Uses layer cache?Starts containers?Typical use case
docker-compose upOnly if no image existsYesYesRestarting unchanged services
docker-compose up --buildAlwaysYesYesActive development with code changes
docker-compose build --no-cacheAlwaysNoNo (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:

dockerfile
1FROM python:3.12-slim
2WORKDIR /app
3# Any source change invalidates everything below
4COPY . .
5RUN pip install -r requirements.txt
6CMD ["python", "app.py"]

Well ordered:

dockerfile
1FROM python:3.12-slim
2WORKDIR /app
3COPY requirements.txt .
4RUN pip install -r requirements.txt
5COPY . .
6CMD ["python", "app.py"]

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:

dockerfile
1# Stage 1: build
2FROM node:20-alpine AS builder
3WORKDIR /app
4COPY package*.json ./
5RUN npm ci
6COPY . .
7RUN npm run build
8
9# Stage 2: production image
10FROM node:20-alpine
11WORKDIR /app
12COPY --from=builder /app/dist ./dist
13COPY --from=builder /app/node_modules ./node_modules
14CMD ["node", "dist/index.js"]

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design