Chapter 2 · Lesson 15 of 20
Docker

Update a Docker container to a new image

Updating a Compose service means pulling the new image and running docker compose up -d, which recreates only the containers whose image changed.

By the end you will

Update a Compose service to a new image tag and know how to roll back.

Time
14 min
Level
Beginner
Check
4 questions
You need
  • A Compose project that is already running
  • A backup of any data the service keeps

To update a Compose service, change the image tag in compose.yaml, run docker compose pull, then docker compose up -d. Compose recreates only the containers whose image changed, and data in named volumes stays. Keep the old tag so you can go back.

How it works#

A container is created from an image, and it keeps pointing at the exact image it was created from, even if the tag on the registry later moves. Updating therefore means two separate things: getting the new image onto the server (pull), then creating a new container from it (up -d). Compose compares the service’s configuration, including the image, with the running container. If they differ, it stops and removes the old container and starts a new one. Services whose configuration did not change are left running.

The old container’s writable layer goes away with it. Data kept in named volumes or bind mounts is attached to the new container, so it survives. Anything the app wrote only inside the container does not. This is the same reason you keep data in volumes in the first place.

A tag is just a label that can be moved. latest is only a default name, not a promise of “newest” or “stable”. If you pin 1.4.2, you know what you run and you know what to return to. Pinning a digest (image: name@sha256:...) is stricter still, because digests do not move.

Step 1: Pin a tag#

In compose.yaml, name a version instead of leaving it open:

yaml
services:
  app:
    image: myorg/myapp:1.4.2

Before an update, write down the current tag. That is your way back. What you should see from docker compose images: the tag you pinned.

Step 2: Back up first#

If the service has a database or uploads, take a backup of its volume or run its dump command before you continue. A new version may migrate data in a way that cannot be undone. Read the release notes for migration or breaking-change warnings.

Step 3: Change the tag and pull#

Edit the tag, for example to 1.5.0, then:

bash
docker compose pull

What you should see: a progress display for each layer, ending with the service marked as pulled. Pulling first means the download happens while the old version is still serving, so the swap in the next step is quick.

Step 4: Recreate the service#

bash
docker compose up -d

-d runs in the background. What you should see: a line saying the old container is being recreated, then started. Compose leaves unchanged services alone.

Check it worked#

bash
docker compose ps
docker compose logs --tail 50 app

ps should show the service as running (or healthy if it has a healthcheck) with a short uptime. The logs should show a normal start-up with no repeated errors. --tail 50 limits output to the last 50 lines. Confirm the version in use:

bash
docker compose images

The tag column should show 1.5.0. Then open the app and test one real action, such as saving a record.

Roll back#

Put the old tag back in compose.yaml and run:

bash
docker compose up -d

The old image is usually still on disk, so this is quick. Do not run docker image prune -a until you are sure you will not roll back, because it removes unused old images. A rollback does not undo database migrations, which is why the backup matters.

Edge cases and gotchas#

  • latest is not pulled automatically. If the image exists locally, docker compose up -d reuses it. Run docker compose pull first, or docker compose up -d --pull always.
  • Architecture. An image tag may not exist for your CPU (arm64 versus amd64). If the pull says no matching manifest, the publisher has not built that platform.
  • Disk fills with old images. Every update leaves the previous image behind. Check with docker image ls and see the prune lesson, after the rollback window has passed.
  • Disk full during pull. A pull that runs out of space can fail with a no space left on device error. Free space first, then retry.
  • Private registries. If the image is private, pull fails until you run docker login on the server.
  • A short outage is normal. The old container stops before the new one is ready. For internal tools it is fine. For anything else, use a rolling or blue-green approach.
  • Environment changes ride along. If you also edit environment variables or volumes in compose.yaml, those are applied in the same recreate, so change one thing at a time.

Troubleshooting#

SymptomLikely causeFix
manifest unknownThe tag does not exist on the registryCheck spelling and available tags on the registry
pull access deniedImage is private or the name is wrongdocker login, or fix the image name
Nothing changed after up -dTag not edited, or latest not pulledEdit the tag, or run docker compose pull
no matching manifest for linux/amd64The image is not built for your CPUPick a tag with your platform or build it yourself
New container restarts in a loopThe new version fails at start-upRead docker compose logs app, then roll back
App works but data looks missingData was not in a volumeRestore from backup, then mount a volume
no space left on deviceDisk is fullFree space, then pull again
Container is unhealthy after the updateThe new version needs a new setting or migrationRead the release notes and logs, fix the setting or roll back
App shows the old version after up -dBrowser or proxy cached the old assetsHard refresh, and check docker compose images for the real tag

Real-world scenario#

Sam runs a notes app at myorg/notes:1.4.2. A release announces a fix for a login bypass. He reads the notes, sees a database migration, and takes a backup first (see back up a Postgres database in Docker).

bash
docker compose images
sed -i 's/notes:1.4.2/notes:1.5.0/' compose.yaml
docker compose pull
docker compose up -d
docker compose logs --tail 50 app

The logs show the migration running and then a normal start. He opens the app and edits a note to prove writes work. An hour later a plugin fails with the new version. He puts 1.4.2 back and runs docker compose up -d. The old image is still on disk, so the rollback takes seconds, but the database is already migrated, so he restores the backup too. The full plan is in rollbacks, and the next step toward no gap is zero-downtime deploys.

Quick reference#

StepCommand
See current tagsdocker compose images
Fetch new imagedocker compose pull
Recreate changed servicesdocker compose up -d
Pull and recreatedocker compose up -d --pull always
Check state and logsdocker compose ps and docker compose logs --tail 50 SERVICE
Roll backput the old tag back, run docker compose up -d
Avoiddocker image prune -a before the rollback window ends

Security and safety notes#

  • Take a backup before every update of a service that stores data.
  • docker compose down -v deletes named volumes. Do not use it as part of an update.

Next#

Your containers now need a name people can type, so point a domain at your server.

Check what you learned4 questions
1Why does the lesson pin a tag such as 1.4.2 instead of latest?
2After editing the tag in compose.yaml, which two commands update the service?
3Why should you wait before running docker image prune -a after an update?
4You use image: myapp:latest and run docker compose up -d. The registry has a newer image. What happens?

Frequently asked questions

Will I lose my data when the container is recreated?

Not if the data is in a named volume or a bind mount. Anything stored only inside the container is lost.

Should I use the latest tag?

Pinning a specific version is safer. latest can change under you, and you cannot tell later which version you were running.

Why did docker compose up -d not pick up the newest latest image?

Compose does not pull on its own when an image with that tag already exists locally. Run docker compose pull first, or docker compose up -d --pull always.

Is there downtime during an update?

Yes, a short one. The old container stops and the new one starts. For no gap you need a second copy behind a proxy, covered in the zero-downtime lesson.

Sources

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