How to Roll Back a Broken Deployment with Docker Tags

A bad deploy can take a working site down in seconds, and the fix is rarely a clever patch written under pressure. The calm way out is to roll back broken deployment work by pointing your server at the last image that worked, then sorting out the bug after traffic is safe again. That sounds simple, and it is simple, but only when the old image still exists, the tag still points at it, and the steps are written down before anything goes wrong. Many teams learn this the hard way: they push latest, the app crashes, and there is no older image to return to. This guide shows a plain method that works on one VPS and in a small team: tag images by version, keep the last good tag, and practise the switch so it takes minutes, not hours.

What a rollback really means

A rollback is not a new deploy. It is a return to a known good state. You stop the broken container, start the previous image, and check that the app answers again. Database changes make this harder, because code can move back while data stays changed, so keep that risk in mind before you touch anything. If a release added a new column, the old code may still work with it, but if a release dropped a column, the old code may fail. That is why a good docker tags rollback strategy starts before release day, with tags that name each build and a short note about what changed in the database. When the note says a migration is safe to reverse, you can move fast. When it says it cannot be reversed, you need a forward fix instead.

How to roll back broken deployment with a tagged image

docker images myapp --format "{{.Repository}}:{{.Tag}} {{.CreatedAt}}"
docker pull myapp:1.4.2
docker stop myapp
docker rm myapp
docker run -d --name myapp -p 3000:3000 --env-file .env myapp:1.4.2
docker logs -f myapp

If you use Compose, change the image tag in the Compose file or pass it as a variable, then run the up command again. Compose will replace the container while keeping the named volumes and networks. If you want to containerize a Node.js app in a clean way first, that groundwork makes this switch far easier, because the image already carries the app and its dependencies. Keep a copy of the last good Compose file too, so a config change that came with the bad release does not survive the rollback by accident.

Tag images at build time

Tags are the whole trick. An image tagged only as latest tells you nothing about which code it holds, and latest moves every time you push. A good habit is to tag every docker build with three labels: the version number, the short commit hash, and a stable alias for the last good release, such as stable or previous. The version and commit give you an exact record. The stable alias gives you a fast target when you are in a hurry. Push all three tags to your registry so the server can pull them even if the local copy was cleaned up, and set a retention rule that keeps at least the last ten version tags.

docker build -t myapp:1.4.3 -t myapp:abc1234 -t myapp:latest .
docker push myapp:1.4.3
docker push myapp:abc1234

Put the tagging step in CI so no one has to remember it. A build that skips tagging is a build you cannot roll back to, and that gap only shows up when you need it most. In your pipeline, fail the job if the tag step fails, and write the tag into the deploy log. If you run blue-green deploys on a single VPS, tags matter even more, because the two environments swap by tag, and a wrong tag sends traffic to the wrong code.

A checklist you can run under pressure

When a deploy breaks, people skip steps. A short deployment rollback checklist stops that. Pin it in your team chat, and follow it in order, even when the fix looks obvious. The first item is to confirm the problem is the new release, not a full server outage or a disk that ran out of space. Check the logs for the new container, check disk space, and check whether the old version worked minutes before the release. If the answer is yes, roll back first and investigate second. Your users care about a working site, not about a perfect diagnosis in the first five minutes.

  • Confirm the last good tag and that the image exists on the server or in the registry.
  • Note the time, the bad tag, and the error you saw, so the fix later has a clear record.
  • Stop the broken container and start the last good image with the same env file and volumes.
  • Wait for the health check, then test a login, a search, and a form submit.
  • Tell the team the site is back on the old version and freeze new deploys until the cause is found.
  • After traffic is safe, fix the bug and ship a new version tag, not a reuse of the old one.

Never rebuild over a tag that already shipped, because two different images with one tag will confuse every server that cached the old one. A fresh tag keeps the record clean. If your team compares blue-green and canary approaches, the same checklist still fits, because both methods need a named target to return to and a test that proves the return worked.

Test the rollback before you need it

Keep old images on the server for a while, even when disk space feels tight. A cleanup job that deletes every image except latest can delete your only way back. Set the cleanup to keep the last three version tags and the stable tag, and run it after a successful deploy, not during one. If disk space is a real problem, move old images to the registry and pull them on demand, but check the pull time first. A five minute pull is fine in a drill and painful in a real outage, so measure it and decide with real numbers from your own server.

Common mistakes that make rollbacks fail

The most common mistake is changing the database and the code in one step with no way to step back. Split risky changes into two releases when you can: first add the new column or field, ship it, then start using it in a later release. That way the old code still works with the new shape. Another common mistake is storing config only inside the image, so the old image carries old config that no longer matches the current service. Keep config in an env file or a secret store outside the image, and version that file with the release. If you need to debug a failing Docker build, do it on a copy of the project, not on the live server.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.