How to Rebuild a Docker Container in docker-compose.yml

When working with Docker Compose, sometimes you need to rebuild a container because you’ve changed your Dockerfile, source code, or configuration.
Simply running docker-compose up may not rebuild the image unless explicitly told to.


1️⃣ Why Rebuilding Is Necessary

  • You updated the Dockerfile
  • Dependencies changed (e.g., requirements.txt, package.json)
  • Base image was updated
  • Configuration or environment changes

If you don’t rebuild, Docker may keep using the cached image.


2️⃣ The Rebuild Commands

Option 1 — Rebuild and Restart

docker-compose up --build

This:

  • Forces a rebuild of images
  • Starts (or restarts) containers using the new image

Option 2 — Rebuild Without Starting

docker-compose build

Only builds images without starting containers.


Option 3 — Force No Cache

If you want a completely fresh build:

docker-compose build --no-cache

This ignores cached layers.


Option 4 — Rebuild a Specific Service

If your docker-compose.yml has multiple services:

docker-compose build <service_name>
docker-compose up <service_name>

3️⃣ Full Rebuild and Cleanup

If you want to start completely fresh:

docker-compose down --rmi all --volumes --remove-orphans
docker-compose up --build

This:

  • Stops containers
  • Removes all images used by the services
  • Deletes volumes and orphan containers
  • Rebuilds everything

4️⃣ Common Mistakes

MistakeWhy It HappensSolution
Changes not showingOld image is still cachedUse --build or --no-cache
Wrong container still runningDidn’t restart after buildRun docker-compose up --build
Persistent data issuesOld volume still attachedUse docker-compose down -v

📌 Summary

To rebuild containers in Docker Compose:

ActionCommand
Rebuild & startdocker-compose up --build
Rebuild onlydocker-compose build
Rebuild without cachedocker-compose build --no-cache
Specific service rebuilddocker-compose build <service>
Sharing Is Caring:

Leave a Comment