When working with Docker Compose during development, you’ll often need to rebuild a container—whether you’ve made changes to your application code, Dockerfile, or dependencies.
But how do you do that correctly and efficiently?
In this post, you’ll learn how to rebuild Docker containers defined in a docker-compose.yml
file using simple and effective commands.
📦 What Does Rebuilding a Container Mean?
Rebuilding a container means:
- Rebuilding the Docker image using the instructions in the
Dockerfile
- Re-creating and restarting the container with the new changes
This is required when:
- You update the application code or packages
- You modify the
Dockerfile
- You change environment variables or volumes in
docker-compose.yml
✅ The Right Command: docker-compose up --build
The most common way to rebuild a container is:
docker-compose up --build
What it does:
- Rebuilds images before starting containers
- Only rebuilds services where changes are detected (like Dockerfile or app source)
- Keeps containers running after rebuild
🔁 Force a Full Rebuild (Clean Slate)
If you want to force a rebuild and remove old containers, run:
docker-compose up --build --force-recreate
This will:
- Rebuild all images
- Recreate all containers, even if the image hasn’t changed
🛠️ Rebuild Without Starting the Containers
If you only want to rebuild the image without running it:
docker-compose build
This is useful for caching layers or preparing an image in CI/CD pipelines.
To rebuild just one service:
docker-compose build service-name
🔄 Full Clean and Rebuild Workflow
Here’s a common development workflow for a full reset:
docker-compose down --volumes --remove-orphans
docker-compose build
docker-compose up
Explanation:
down
stops and removes containers, networks, volumesbuild
ensures all services are freshly builtup
starts everything with a clean state
📝 Example
Let’s say you have a docker-compose.yml
like this:
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
To rebuild and restart the web
container:
docker-compose up --build
To rebuild only the web
service:
docker-compose build web
🚨 When Changes Don’t Reflect
Sometimes your rebuild doesn’t apply changes. Possible reasons:
- The
Dockerfile
ordocker-compose.yml
didn’t actually change - You’re using volume mounts (
volumes: - .:/app
) which overwrite container content - Docker cache is persisting old layers
✅ Fix: Add --no-cache
if needed:
docker-compose build --no-cache
✅ Summary
Task | Command |
---|---|
Rebuild all containers | docker-compose up --build |
Force rebuild and recreate | docker-compose up --build --force-recreate |
Rebuild without running | docker-compose build |
Rebuild one service | docker-compose build <service> |
Rebuild from scratch | docker-compose down && docker-compose build && docker-compose up |
🎯 Conclusion
Rebuilding Docker containers with docker-compose.yml
is simple but essential for maintaining a clean and consistent development environment. Use up --build
for common updates, and build
or down
for deeper resets.