Rebuilding Docker Compose Services Completely

To rebuild Docker Compose services completely, potentially removing old images first, you have several options depending on your exact needs. Here are the common approaches:

Using --force-recreate with docker compose up --build

When you want to ensure containers are recreated based on newly built images:

docker compose up --build --force-recreate

Pruning images before building

You can explicitly remove unused images first, then build. This uses a two-step approach:

# Remove unused images (e.g., older than 24 hours, adjust filter as needed)
docker system prune -f --filter "until=24h"

# Build fresh images, potentially leveraging cache for unchanged layers
docker compose build

The first command removes dangling and unused Docker objects (including images not associated with any container). The filter helps control which images are pruned. The second command then builds your images. Use --no-cache with build if you want to avoid the build cache entirely. [Prune unused Docker objects]

Using --no-cache to ensure a clean build

To force Docker to rebuild every layer of your images from scratch, ignoring the build cache:

docker compose build --no-cache

This ensures a completely fresh build, useful if you suspect cache corruption or want to ensure all intermediate steps are re-executed (e.g., pulling base images, running package manager updates). After this, run docker compose up. [Compose Build Specification]

Using Compose watch with pruning (for Development)

If you're actively developing, docker compose watch can automatically rebuild on code changes and optionally prune old images:

docker compose watch --prune

The --prune flag specifically tells Compose watch to remove dangling images resulting from rebuilds, helping keep your system clean during development iterations. [Docker Compose watch]

Removing containers before rebuilding

If you want to ensure no old containers interfere and start completely from scratch (containers and potentially images):

# Remove containers defined in the Compose file
docker compose rm -f -s -v # -f (force), -s (stop), -v (remove volumes)

# Build images (add --no-cache if needed)
docker compose build

# Create and start new containers
docker compose up

This sequence first removes the containers (and optionally associated anonymous volumes with -v), then builds fresh images, and finally creates and starts new containers based on those images. [Docker Compose rm]

Most Thorough Approach (Clean Slate):

For a truly clean slate, combine system pruning with a no-cache build:

# Prune all unused docker objects (images, containers, networks, etc.)
docker system prune -a -f # Use -a to include unused images

# Rebuild all image layers from scratch
docker compose build --no-cache

# Start fresh containers
docker compose up -d # -d for detached mode

Choose the method that best suits whether you need to just recreate containers, force a cache-less build, remove old images, or perform a complete cleanup.