Docker Compose Build: When to Use --no-cache or --build

By Ines Duarte ·

Docker Compose Build: How to Rebuild Without the Guesswork

You edited a file, ran docker compose up, and the container is still running the old version. It's probably the most common Compose complaint out there, and the fix is almost never the nuclear command someone posted in a five-year-old forum thread. Understanding how docker compose build actually works, and why up on its own does nothing to your image, saves you from typing docker compose down && docker compose build --no-cache && docker compose up --force-recreate every time you touch a line of code. That chain works, technically, the way carpet-bombing a room works to get rid of a fly. This guide walks you to the smallest command that fixes your specific problem instead.

Before you start, you'll want three things ready: Docker installed with Compose v2 (the command is docker compose, with a space, not the older docker-compose), a terminal open in the folder that holds your compose.yaml, and at least one service in that file with a build: section rather than only an image:. If your terminal doesn't recognize docker compose at all, that's a separate installation problem, not a rebuild problem; our docker compose command not found guide covers that case in detail.

Why docker compose up Doesn't Rebuild Your Image

Here's the part that trips up nearly everyone starting out with Compose: docker compose up is not a build command. Per the official reference, up "builds, (re)creates, starts, and attaches to containers for a service," but it only recreates a container when the service's configuration or image changed after that container was created (Docker, Compose up reference). If nothing about the image looks different to Compose, it just starts the container you already have. Your edit sits there, unread.

Picture the image as a cake that's already baked, and the container as a slice on a plate. Running docker compose up with no flags just serves another slice of the same cake. It never checks whether you changed the recipe. If you want a new cake, someone has to bake one, and that's what the build step is for. The analogy gets shakier once layer caching enters the picture (a cake doesn't have reusable layers you skip baking), but for the basic image-versus-container mix-up, it holds.

That's why docker compose up --build exists. Adding --build tells Compose to build images before starting containers, which is the flag most people actually want when they say "rebuild" (Docker, Compose up reference). Run up plain, and Compose will happily serve last week's image forever. If you're still assembling your compose.yaml from scratch, the Compose generator on this site writes the build: block and service structure for you, so there's less to get wrong before you even reach this stage.

The Four Commands That Actually Rebuild Something

Four commands cover nearly every rebuild situation, and each does a different mix of building and recreating. Confusing them is exactly why forum answers recommend commands that don't exist, like docker compose up --no-cache, which throws a usage error, because --no-cache has only ever belonged to build, never to up, in either Compose v1 or v2 (Docker, Compose build reference).

CommandWhat It RebuildsWhat It RecreatesWhen To Use It
docker compose buildImages for services with a build: blockNothing. Running containers keep using the old image.You want a fresh image ready before deploying or testing, without touching what's currently running.
docker compose up --buildImages, same as aboveAny container whose image or config actually changedThe everyday "I edited code, show me the result" command.
docker compose build --no-cacheEvery layer, from the first instruction, ignoring the cache entirelyNothing by itself; pair with up --build to also restart containersYou suspect a cached layer is stale, not just old code.
docker compose up --force-recreateNothing on its ownEvery container, even if Compose thinks nothing changedThe image is new, but the running container hasn't swapped to it yet.

Two more flags come up alongside these four. --renew-anon-volumes (short form -V) recreates anonymous volumes instead of keeping their old data, which matters when a volume is quietly hiding your new files. --pull always forces Compose to check the registry for a newer base image before building, useful when your Dockerfile pulls a tag like node:20 that moves over time (Docker, Compose up reference).

Why Your Code Changes Aren't Showing Up

Not every "my changes aren't showing" report has the same cause, and the cargo-cult chain treats them as one problem. Work through these five rungs in order, starting with the cheapest fix, and stop as soon as one of them matches what you're seeing.

  1. You never actually rebuilt. Plain docker compose up after an edit starts your existing container without touching your code at all. Run docker compose up --build instead. If the terminal shows build steps scrolling by before the "Attaching to" line, this was it.
  2. The image rebuilt, but the container didn't swap. A real Compose bug report from March 2022 describes exactly this: a user ran up --build, Compose reported a new image, and "the old code in main.py executes" anyway (docker/compose, issue #9308). Run docker compose ps or docker compose images to check which image ID your container is actually using. If it doesn't match the one you just built, force it with docker compose up --build --force-recreate.
  3. A volume is hiding your new files. A bind mount over your app folder, an anonymous volume declared in the Dockerfile, or a separate node_modules volume can sit on top of your freshly built image and keep serving old content from disk. One developer put it well: the same up --build command "works for you with some projects but not with all projects," because whether a project has one of these volumes is what decides the outcome. Run docker compose up --build --renew-anon-volumes, or check your volumes: block for anything mounted over the folder you just changed.
  4. The file never reached the build in the first place. If a COPY instruction excludes the file, your .dockerignore is too broad, or the Compose file's build.context points somewhere other than where you think, the builder never sees your edit. A Docker Forums thread from March 2024 shows a user whose CSS changes wouldn't appear after up --build; the thread was never root-caused, so treat this as a typical suspect for that kind of symptom rather than a confirmed diagnosis. Check your .dockerignore and the context: path in compose.yaml.
  5. A RUN step is genuinely cached and shouldn't be. This is the rare rung, and the only one where --no-cache is the right tool. It shows up when a base image tag moved upstream, a package install pulls from a moving URL, or a stale apt-get update layer is serving outdated package lists. Only here should you reach for docker compose build --no-cache.

Rung five is where most rebuild advice starts, and it's where it should end. Skipping straight to --no-cache throws away every cached layer, including the ones that were fine, and on a project with a slow dependency install it can turn a two-second rebuild into a five-minute one for nothing.

What --no-cache Actually Does (and What It Doesn't)

The build cache checks different things depending on the instruction. For COPY and ADD, the builder calculates a checksum from the file's contents, and modification time plays no part in that calculation (Docker, cache invalidation docs). "Touching" a file to update its timestamp does nothing to bust the cache. Editing even one character always does, because the checksum changes. If your instinct when troubleshooting a stale build is to touch a file and re-save it, that habit won't help you here.

RUN instructions work differently again. Cache checking for RUN doesn't look at files inside the container; it matches the instruction string itself against the previous build (Docker, cache invalidation docs). So RUN npm install matches a previous RUN npm install even after your package.json changed, as long as the layer above it (the one that copied package.json) didn't already break the cache. Once any layer's cache is invalidated, every layer after it rebuilds too, even ones that individually haven't changed at all. That cascade is why a single small edit can sometimes trigger a much bigger rebuild than expected.

docker compose build --no-cache ignores this whole mechanism and rebuilds every layer from the first instruction down. It's the build equivalent of throwing out an entire day's cooking because you're not sure which pot got contaminated. Sometimes that's genuinely the right call. Most of the time it's overkill for a problem that rung one through four already explain.

One more source of confusion worth clearing up: setting build.no_cache: true in your compose.yaml only affects a build that's already happening. A GitHub issue from March 2023 shows a user who expected that setting to trigger a rebuild every time they ran docker compose up. It didn't, because up still won't build an image that already exists unless you also pass --build, and the issue was closed as "not planned" for exactly that reason. no_cache changes how a build runs. It never decides whether a build runs at all.

Rebuilding Just One Service

Once your Compose file has more than one or two services, rebuilding all of them on every change gets slow fast. You don't have to. Compose accepts service names on most build and up commands, so you can target exactly the one you're working on.

docker compose build web
docker compose up -d --build --no-deps web

The first line builds only the web service's image. The second brings it up in detached mode, rebuilds it if needed, and --no-deps stops Compose from also restarting services that web depends on, such as a database that's already running fine. If you want dependencies rebuilt too, for example after a shared base image changed, drop --no-deps and add --with-dependencies to the build command instead, which builds the target service and everything it transitively depends on (Docker, Compose build reference).

How to Stop Needing the Ladder

The best fix for a slow, confusing rebuild cycle is needing it less often. A few habits handle most of the friction.

Order your Dockerfile so things that change rarely come first. Copying your package manifest (package.json, requirements.txt, composer.json) and installing dependencies before you copy the rest of your source means that install layer's cache survives ordinary code edits. Docker's own guidance is blunt about this: installing dependencies in earlier layers means "there is no need to rebuild those layers when a project file has changed." If your Dockerfile copies everything at once with a single COPY . . before installing anything (a multi-stage build fixes the ordering and the image size in one pass), every edit invalidates the install step, and that's usually the actual reason a small change triggers a multi-minute rebuild.

COPY package.json package-lock.json ./
RUN npm install
COPY . .

Add a .dockerignore file next to your compose.yaml. It works the same way a .gitignore does, keeping files and folders out of what gets sent to the builder, which stops node_modules, .git, and build artifacts from bloating your build context. The Dockerfile generator on this site writes this layer order and a matching .dockerignore automatically for Node, Python, PHP, and Go presets, so the ordering rule is handled for you instead of something you have to remember mid-edit.

For active development, docker compose watch (or docker compose up --watch) is the documented replacement for manually rerunning build commands in a loop. It syncs file changes straight into a running container, or rebuilds and swaps the container automatically, depending on how you set up the develop.watch block in compose.yaml (Docker, file watch how-to). It's worth the ten minutes of setup once you're tired of typing up --build every few minutes during a coding session.

When you do need to clean up cache, reach for docker builder prune rather than docker system prune -a. The builder-specific command removes build cache; the system-wide one removes every unused image on the machine, which is a much bigger cleanup than most rebuild problems call for. Along the same lines, nothing in this guide needs docker compose down -v. That flag deletes named volumes, and if one of them holds a database, that's your data gone, not just your container.

If you're new to Compose commands in general and this is your first project, start with our guide to how to use Docker Compose and come back here once you've got a working stack running. And before your next rebuild, it's worth pasting your compose.yaml into the Compose validator to catch a missing build.context, an unpinned base image tag, or a typo that would explain a "changes not showing" symptom before you touch the build cache at all. Once the new container is running, a tool like httpcheck.tools gives you a fast way to confirm the site is actually serving the new asset (and not a cached copy), rather than just trusting that the rebuild worked.

Frequently Asked Questions

When do I need docker compose up --build?

You need docker compose up --build any time you've changed something inside the build context, meaning your source code, your Dockerfile, or a file it copies, and you want the running containers to reflect that change. Plain docker compose up never rebuilds an image on its own; it only starts what already exists. If you're not sure whether an edit counts, running with --build costs you nothing extra when there's nothing new to build. Compose just reuses the cached layers and finishes quickly.

Why are my code changes not showing in the container?

Work through the five-rung ladder in order: check that you actually rebuilt with --build, then check whether the container swapped to the new image (verify with docker compose ps), then check whether a bind mount or anonymous volume is covering your new files, then check whether your .dockerignore or build context is excluding the file, and only then suspect the cache itself. Most "changes not showing" reports resolve at rung one, two, or three, well before --no-cache becomes relevant.

What does --no-cache do?

docker compose build --no-cache ignores the entire build cache and rebuilds every instruction in your Dockerfile from scratch, regardless of whether the underlying files or commands actually changed. It's a flag on build, not on up, so docker compose up --no-cache is not a valid command and will error out. Reach for it only when you suspect a cached layer is stale in a way that editing your source code won't fix, such as a moved base image tag or a package pulled from a URL that changed upstream.

How do I force Docker Compose to rebuild one service?

Run docker compose build <service-name> to rebuild just that service's image, or docker compose up -d --build --no-deps <service-name> to rebuild and restart it in detached mode without touching the services it depends on. Add --with-dependencies to the build command if you want its dependencies rebuilt as well, which is useful when a shared base image or a service the target depends on has also changed.