---
name: docker-compose-force-recreate
description: docker compose up -d after build keeps the old container running unless --force-recreate is specified — new image is built but not used
type: concept
---

# Docker Compose — `--force-recreate` Required After Build

## The Bug

```bash
docker compose build web
docker compose up -d        # ← old container still running
```

Even though the image was rebuilt, `up -d` without `--force-recreate` keeps the existing container if it's already running. Docker Compose detects "container is running and config hasn't changed" and skips recreation.

**Result:** new code is in the image but the running container has the old code.

## The Fix

```bash
docker compose build web
docker compose up -d --force-recreate web
```

Or as a one-liner (rebuild + recreate):

```bash
docker compose up -d --build --force-recreate web
```

## When Each Flag Is Needed

| Command | Effect |
|---------|--------|
| `docker compose up -d` | Start stopped containers; skip running ones |
| `docker compose up -d --force-recreate` | Recreate ALL containers (new image, updated config) |
| `docker compose up -d --build` | Build image before starting, but may still skip recreation |
| `docker compose up -d --build --force-recreate` | Full rebuild + recreate — safest for code changes |
| `docker compose restart web` | Restart process inside existing container — **does NOT apply new image** |

## Related

- [[wiki/concepts/docker-compose-restart-no-code-reload]] — `restart` doesn't reload code (same issue, different flag)

## Deploy Script Pattern

```bash
#!/bin/bash
set -e
git pull
docker compose build web
docker compose up -d --force-recreate web
echo "Deployed."
```

**Source:** daily/2026-05-13.md | 2026-05-13
