Q: How do you rebuild only a specific service in Docker Compose?
To rebuild only a specific service in Docker Compose, name the service: docker compose up -d --build --no-deps web rebuilds the image for web and recreates just that container.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>rebuild only a specific service in Docker Compose</strong>, name the service: <code>docker compose up -d --build --no-deps web</code> rebuilds the image for <code>web</code> and recreates just that container. Without <code>--no-deps</code>, Compose also starts dependencies, and without <code>--build</code>, <code>up</code> reuses the existing image. Interviewers ask this to check you understand how Compose decides to rebuild, how it detects configuration changes and how to avoid needless downtime in the rest of the stack.
Mental model: build versus recreate
Two separate actions are often confused: building an image and recreating a container from it. Knowing which of these commands touches running containers is what keeps a quick fix from becoming an outage.
- docker compose build: only builds images for services with a
build:section. It does not touch running containers. - docker compose up: creates or recreates containers whose configuration or image changed. It builds missing images, but does not rebuild an existing image unless asked with
--build. - Dependencies: naming a service on
upalso starts services itdepends_on.--no-depsprevents that. - Image-only services: services with just
image:are pulled, not built, so rebuilding does not apply. Usedocker compose pull. - Several services: list more than one name, such as
docker compose up -d --build --no-deps web worker, to rebuild a subset while leaving databases and caches untouched.
Hands-on: rebuild only a specific service in Docker Compose
The typical commands, from slowest to most targeted.
docker compose build web # build image only
docker compose up -d --build --no-deps web # rebuild + recreate web only
docker compose build --no-cache --pull web # clean rebuild, fresh base
docker compose up -d --no-deps --force-recreate web
# Image-only service (no build:), pull instead
docker compose pull redis && docker compose up -d --no-deps redis
- Build only:
docker compose build webprepares the new image and leaves containers running. - Build and replace:
docker compose up -d --build --no-deps webrebuilds and recreates onlyweb. Other containers keep running. - Force a fresh build: add
--no-cachetobuild, and--pullto refresh the base image first. - Force recreate:
--force-recreatereplaces the container even if nothing changed, for example after changing a mounted file the app reads only at start. - Build arguments: pass
--build-argthroughdocker compose build, or defineargsin the service's build section, so version stamps change the layer and trigger a real rebuild.
Verify that only the right service changed
Check image IDs and container timestamps rather than assuming.
docker compose ps --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}'
docker compose images web
docker inspect --format '{{.Created}}' $(docker compose ps -q web)
- Container ages:
docker compose psshould showwebas recently created while others show their original uptime. - Image identity:
docker compose imageslists the image ID and age for each service. - Build output:
docker compose build --progress=plain webshowsCACHEDfor unchanged steps, so you can see which layers actually rebuilt. - Behaviour: hit the service and compare the version endpoint or file content to the change you made.
- Downtime expectation: recreating a container stops the old one before starting the new one, so a single replica has a brief gap. Use multiple replicas behind a proxy if you need zero downtime.
Failure modes and cache behaviour
If the change did not appear, one of these is usually why. Check each symptom against the evidence before changing anything.
- Layer cache hides the change:
COPY . .placed before dependency install invalidates the cache too often, while a lateCOPYmight not include what you changed. Check.dockerignoreand layer order. - No rebuild happened: plain
docker compose up -d webreused the old image. Add--build. - Bind-mounted code: if source is mounted into the container, you may not need a rebuild at all, only a restart or a watch-mode sync.
- Environment changes: editing
environmentorportsneeds a recreate, whichup -ddoes automatically, not a rebuild. - Project scoping: commands act on the current project name. If a rebuild seems to do nothing, check
docker compose lsand the-pflag to be sure you are targeting the right stack.
Production practice: faster loops and safer deploys
Use Compose features that avoid manual rebuild commands.
services:
web:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.json
- Compose Watch:
docker compose watch(Compose 2.22 and later) syncs files or rebuilds a service on change usingdevelop.watchrules. - Dependency order: use
depends_onwithcondition: service_healthyso a recreated service waits for its database. - CI builds: build and push tagged images in CI, and on servers run
pullthenup -d --no-depsfor one service to avoid building on production hosts. - Rollbacks: keep the previous image tag so you can set it and recreate quickly.
- Cleaning up: repeated rebuilds leave dangling images behind, so run
docker image pruneoccasionally on development machines.
- I run <code>docker compose up -d --build --no-deps web</code>, which rebuilds the image and recreates only that service.
- I verify through <code>docker compose ps</code> and <code>images</code> and by reading build output for CACHED layers.
- I automate with Compose Watch in development and CI-built, pulled images for servers.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Running up without --build after changing the Dockerfile Compose reuses the existing image, so the container keeps running old code.
- Omitting --no-deps and restarting databases Naming a service starts its depends_on services too, which can create unplanned churn.
- Expecting build to restart containers docker compose build only makes an image. You still need up to replace the running container.
- Forgetting that image-only services cannot be built Services without a build section need pull, so rebuild commands do nothing for them.