Chapter 4 · Lesson 3 of 18
Deploy and operate

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

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

By the end you will

Go back to the last good release quickly and confirm the old version is live again.

Time
18 min
Level
Beginner
Check
4 questions
You need
  • An app deployed with Docker Compose and a pinned image tag

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, 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.

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 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#

SymptomLikely causeFix
docker compose ps still shows the new tagEdit not saved or wrong fileCheck the image line and the Compose file in use
manifest unknown or pull access deniedOld tag missing or no registry loginLog in, or check the tag exists in the registry
Old image not found locally and no registryImage prunedRebuild from the old commit or restore from a registry
Old version starts then crashesSchema changed by a migrationRestore a backup or roll the migration back
Rollback finishes, errors continueCause was not the code, for example config or a dependencyCompare config, and check the upstream service
Error response from daemon: Conflict. The container name is already in useA leftover container with the same nameRemove it with docker rm or let Compose manage names
Health URL shows the old version but users see new pagesCDN or browser cachePurge the cache and version asset names
Container loops in a restartOld config incompatibleRead 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 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
RuleWhy
Pin image tagsThe old one stays addressable
Keep the last few imagesRollback needs no rebuild
Additive migrationsOld code still works on new schema
Back up before risky releasesData 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.

Next#

Next, handle the settings every release needs in Environment variables and secrets.

Try it: Roll back to the previous image tag

Put the old pinned tag back in compose.yaml, redeploy and confirm the old version runs.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Why can a rollback be fast?
2Errors rise right after a deploy. What is the recommended order?
3Does a rollback undo database changes?
4You deployed myapp:latest, then pushed a broken build under the same tag. Why is rolling back hard?

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.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios