# How to roll back a bad release (Docker and Compose)

> A rollback returns traffic to the previous working release when the new one misbehaves.

URL: https://levelrail.com/learn/deploy-operate/rollbacks/
Updated: 2026-11-27

When errors spike right after a deploy, the fastest fix is to go back to the last release that worked, then work out what broke. A rollback returns traffic to the previous working release. With Docker it means running the older image again.

## How it works

A release is an image plus its configuration. If you keep the image of the last good release and a record of its tag, going back is the same operation as a deploy with an older tag. Nothing is rebuilt, which is why it can take seconds.

This only works if you can name the old version. A pinned tag such as `ghcr.io/example/app:1.0.0` identifies one image forever. A moving tag such as `latest` does not: after a new push, `latest` means the new image and the old one is only reachable by its digest. Pin tags in Compose, and write down what is live.

A worked example. Version 1.0.0 has run for weeks. You deploy 1.1.0, and the error rate climbs. You open `compose.yaml`, change the image line back to 1.0.0, and run `docker compose up -d`. Compose sees the service definition changed, stops the 1.1.0 container and starts one from the 1.0.0 image (already on disk, so no pull). A few seconds later the health URL reports version 1.0.0. If you deployed with a [zero-downtime swap](https://levelrail.com/learn/deploy-operate/zero-downtime-deploys/)), the old copy might still be running, and rolling back is just switching traffic.

Data is the part a rollback cannot touch. If 1.1.0 ran a migration that renamed a column, 1.0.0 may fail against the new schema. That is why migrations should be additive and backed by a [backup](https://levelrail.com/learn/deploy-operate/backups-and-the-3-2-1-rule/)).

## Step 1: Find what is running

```bash
docker compose ps
```

What you should see: the service, its image with tag, how long it has been up and its status. Note the current tag before you change anything.

## Step 2: Put the old tag back

Edit the `image:` line in `compose.yaml` to the last good tag, or use sed as in the lab:

```bash
sed -i 's/app:1.1.0/app:1.0.0/' compose.yaml
```

`-i` edits the file in place. Check the result with `grep image compose.yaml`.

## Step 3: Redeploy

```bash
docker compose up -d
```

`-d` runs in the background. Compose recreates only services whose definition changed. What you should see: a line such as `Container app-app-1  Started`. If the image is missing locally Compose pulls it first, which needs registry access.

## Step 4: Confirm the old version is live

```bash
docker compose ps
curl -fsS https://example.com/health
```

What you should see: the old tag in the image column, `Up` with a recent start time, and a health response that names the old version if your app reports one. `-f` makes curl fail on HTTP errors, `-sS` hides progress but shows errors.

## Check it worked

After rolling back, confirm the health check passes, the error rate drops and the version shown in the app or logs is the old one. Read the logs for a minute with `docker compose logs --tail 50 app` to check nothing new is failing.

## Edge cases and gotchas

- **`latest` is not a version.** If you only ever deployed `latest`, the old image may have no name left. Find it with `docker image ls` or by digest, and start pinning tags.
- **The old image was pruned.** Running `docker image prune -a` or a [disk cleanup](https://levelrail.com/learn/docker/free-disk-space-with-docker-prune-safely/)) can delete it. You then need a registry that still holds it. Keep the last few tags.
- **A migration already ran.** The older code may not work with the new schema. Either roll back the migration if it is reversible, restore a backup, or roll forward with a fix.
- **Config and environment changed too.** If the release also needed new variables, the old version may ignore them, and removing them can break the new one. Keep config changes in the same record as the tag.
- **Caches and CDNs hold new assets.** Users may load old HTML with new JavaScript or the reverse. Version asset filenames so both copies can coexist.
- **The registry tag was overwritten.** Someone pushed over `1.0.0`. Prefer immutable tags or deploy by digest.
- **Rolling back hides the cause.** Capture logs and the failing request before you replace the container, or you lose the evidence.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `docker compose ps` still shows the new tag | Edit not saved or wrong file | Check the image line and the Compose file in use |
| `manifest unknown` or `pull access denied` | Old tag missing or no registry login | Log in, or check the tag exists in the registry |
| Old image not found locally and no registry | Image pruned | Rebuild from the old commit or restore from a registry |
| Old version starts then crashes | Schema changed by a migration | Restore a backup or roll the migration back |
| Rollback finishes, errors continue | Cause was not the code, for example config or a dependency | Compare config, and check the upstream service |
| `Error response from daemon: Conflict. The container name is already in use` | A leftover container with the same name | Remove it with `docker rm` or let Compose manage names |
| Health URL shows the old version but users see new pages | CDN or browser cache | Purge the cache and version asset names |
| Container loops in a restart | Old config incompatible | Read `docker logs`, see the restart-loop guide |

## Real-world scenario

You ship 1.4.0 of an internal tool at 16:00 and by 16:05 the support channel reports checkout errors. You do not read stack traces first. You run `docker compose ps`, note 1.4.0, edit the tag back to 1.3.2, and run `docker compose up -d`. At 16:07 `curl -fsS https://tool.example.com/health` reports 1.3.2 and errors stop.

Then you debug. Logs from the 1.4.0 container are gone because Compose replaced it, but your logging driver kept them, so `docker logs` on the shipped logs shows a failing query. 1.4.0 added a column and the code expected it, but the migration had run only half way on this database. You fix the migration, test it on a copy of last night's backup, and ship 1.4.1 the next morning.

The plan worked because the old tag was pinned, the old image was still on disk, and the migration only added things. See the blog guide on [rolling back a failed Docker deploy](https://levelrail.com/blog/rollback-failed-deploy-docker/)) for a scripted version.

## Quick reference

```bash
docker compose ps                         # what is running
docker image ls                           # what is still on disk
sed -i 's/app:NEW/app:OLD/' compose.yaml  # put old tag back
docker compose up -d                      # redeploy
docker compose logs --tail 50 app         # watch after
```

| Rule | Why |
| --- | --- |
| Pin image tags | The old one stays addressable |
| Keep the last few images | Rollback needs no rebuild |
| Additive migrations | Old code still works on new schema |
| Back up before risky releases | Data cannot be rolled back with code |

## Security and safety notes

- A rollback can reintroduce a known security bug. Plan a fix soon after.
- Restoring a backup overwrites newer data. Know what you will lose before you do it.

## Related lessons

- [Zero-downtime deploys](https://levelrail.com/learn/deploy-operate/zero-downtime-deploys/)): how a swap leaves the old copy ready.
- [Deploy an app with Docker Compose](https://levelrail.com/learn/deploy-operate/deploy-an-app-with-docker-compose/)): where the pinned tag lives.
- [Backups and the 3-2-1 rule](https://levelrail.com/learn/deploy-operate/backups-and-the-3-2-1-rule/)): the data side of a bad release.
- [Docker restart policies](https://levelrail.com/learn/docker/docker-restart-policies/)): what happens when the old version crashes.

## Next

Next, handle the settings every release needs in [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)).

## Frequently asked questions

### Does a rollback undo database changes?

No. Code goes back but data does not. That is why migrations should be backward compatible and why you keep backups.

### How do I roll back a Docker Compose app to the previous version?

Put the previous pinned image tag back in compose.yaml and run docker compose up -d. If you used the latest tag, there is nothing to go back to, so pin tags.

### Why did my rollback not change anything?

Common causes are a latest tag that points at the new image, a cached image on the host, or a config that still references the new tag. Check the running image with docker compose ps.
