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

URL: https://levelrail.com/learn/docker/update-a-container-to-a-new-image/
Updated: 2026-11-27

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](https://levelrail.com/learn/glossary/#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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `manifest unknown` | The tag does not exist on the registry | Check spelling and available tags on the registry |
| `pull access denied` | Image is private or the name is wrong | `docker login`, or fix the image name |
| Nothing changed after `up -d` | Tag not edited, or `latest` not pulled | Edit the tag, or run `docker compose pull` |
| `no matching manifest for linux/amd64` | The image is not built for your CPU | Pick a tag with your platform or build it yourself |
| New container restarts in a loop | The new version fails at start-up | Read `docker compose logs app`, then roll back |
| App works but data looks missing | Data was not in a volume | Restore from backup, then mount a volume |
| `no space left on device` | Disk is full | Free space, then pull again |
| Container is `unhealthy` after the update | The new version needs a new setting or migration | Read the release notes and logs, fix the setting or roll back |
| App shows the old version after `up -d` | Browser or proxy cached the old assets | Hard 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](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/deploy-operate/rollbacks/)), and the next step toward no gap is [zero-downtime deploys](https://levelrail.com/learn/deploy-operate/zero-downtime-deploys/)).

## Quick reference

| Step | Command |
| --- | --- |
| See current tags | `docker compose images` |
| Fetch new image | `docker compose pull` |
| Recreate changed services | `docker compose up -d` |
| Pull and recreate | `docker compose up -d --pull always` |
| Check state and logs | `docker compose ps` and `docker compose logs --tail 50 SERVICE` |
| Roll back | put the old tag back, run `docker compose up -d` |
| Avoid | `docker 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.

## Related lessons

- [Docker Compose basics](https://levelrail.com/learn/docker/docker-compose-basics/)): how Compose decides what to recreate
- [Rollbacks](https://levelrail.com/learn/deploy-operate/rollbacks/)): go back when an update goes wrong
- [Zero-downtime deploys](https://levelrail.com/learn/deploy-operate/zero-downtime-deploys/)): update without the short gap
- [Free disk space with docker prune safely](https://levelrail.com/learn/docker/free-disk-space-with-docker-prune-safely/)): remove old images after the window

## Next

Your containers now need a name people can type, so [point a domain at your server](https://levelrail.com/learn/domains-https/point-a-domain-at-your-server/)).

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

- [Docker docs, Compose](https://docs.docker.com/compose/)
