⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 44 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #44 (L2) · Docker

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.

#Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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 up also starts services it depends_on. --no-deps prevents that.
  • Image-only services: services with just image: are pulled, not built, so rebuilding does not apply. Use docker 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.
2️⃣

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 web prepares the new image and leaves containers running.
  • Build and replace: docker compose up -d --build --no-deps web rebuilds and recreates only web. Other containers keep running.
  • Force a fresh build: add --no-cache to build, and --pull to refresh the base image first.
  • Force recreate: --force-recreate replaces the container even if nothing changed, for example after changing a mounted file the app reads only at start.
  • Build arguments: pass --build-arg through docker compose build, or define args in the service's build section, so version stamps change the layer and trigger a real rebuild.
Advertisement
3️⃣

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 ps should show web as recently created while others show their original uptime.
  • Image identity: docker compose images lists the image ID and age for each service.
  • Build output: docker compose build --progress=plain web shows CACHED for 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.
4️⃣

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 late COPY might not include what you changed. Check .dockerignore and layer order.
  • No rebuild happened: plain docker compose up -d web reused 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 environment or ports needs a recreate, which up -d does automatically, not a rebuild.
  • Project scoping: commands act on the current project name. If a rebuild seems to do nothing, check docker compose ls and the -p flag to be sure you are targeting the right stack.
5️⃣

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 using develop.watch rules.
  • Dependency order: use depends_on with condition: service_healthy so a recreated service waits for its database.
  • CI builds: build and push tagged images in CI, and on servers run pull then up -d --no-deps for 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 prune occasionally on development machines.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Remember the trio: <code>--build</code> to force an image rebuild, <code>--no-deps</code> to leave neighbours alone, and a service name to limit scope. Then confirm with <code>docker compose ps</code> timestamps."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Running up without --build after changing the Dockerfile Compose reuses the existing image, so the container keeps running old code.
  2. Omitting --no-deps and restarting databases Naming a service starts its depends_on services too, which can create unplanned churn.
  3. Expecting build to restart containers docker compose build only makes an image. You still need up to replace the running container.
  4. Forgetting that image-only services cannot be built Services without a build section need pull, so rebuild commands do nothing for them.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →