How Updating a Container Works
A container is created from an image, and the image never changes underneath it. To run a newer version you replace the container:
- Pull the new image.
- Stop and remove the old container.
- Start a new container from the new image with the same options and the same volumes.
Anything written inside the container itself, outside a volume or bind mount, is lost in step 2. Check where the data lives before you start.
Before You Start: Data and a Backup
docker inspect -f '{{json .Mounts}}' web
This lists the volumes and bind mounts of the container named web. If it prints an empty list, the container keeps its data inside itself; copy it out with docker cp before you go on.
Back up the volume. Stop the container so the files do not change during the copy:
docker stop web
docker run --rm -v web_data:/data:ro -v "$PWD":/backup alpine tar czf /backup/web_data.tar.gz -C /data .
docker start web
For a database, a dump made with the database's own tool is the better backup. A server snapshot in your provider's control panel adds a second safety net; see snapshot vs backup.
Step 1: Write Down the Options
The new container needs the same ports, volumes, environment variables, network, and restart policy. Read them from the old one:
docker inspect web
docker port web
The output includes the environment variables, passwords among them, so do not paste it anywhere public. Keeping the full docker run command in a script avoids this step next time; see how to write a bash script.
Step 2: Pull the New Image
docker pull nginx:1.30.8
Pulling first keeps the downtime short: the old container keeps serving while the download runs.
Step 3: Replace the Container
docker stop web
docker rename web web-old
docker run -d --name web \
-p 8080:80 \
-v web_data:/usr/share/nginx/html \
--restart unless-stopped \
nginx:1.30.8
Renaming instead of removing keeps a way back. Check the new container:
docker ps
docker logs --tail 50 web
Step 4: Remove the Old One or Roll Back
When the new version works, remove the old container and then its image, named by the tag it had before:
docker rm web-old
docker image rm nginx:OLD_TAG
If it does not work, go back:
docker rm -f web
docker rename web-old web
docker start web
Some applications change their data format during an upgrade, and the old version may not read it any more. That is the case the backup is for.
With Docker Compose
Compose keeps the options in the file, so the update is two commands in the project folder:
docker compose pull
docker compose up -d
Compose replaces only the containers whose image changed and keeps the named volumes. To change the version, edit the tag in compose.yaml first. More options are in docker compose up -d.
Pin Tags in Production
- Avoid latest:
latestis whatever was pushed last. A restart months later can bring a new major version you did not plan for. - Use a version tag:
nginx:1.30follows the patch releases of one line;nginx:1.30.8stays on one exact version. - Read the release notes: before a jump between major versions, especially for databases.
What docker update Does
The command named docker update does not update the image. It changes the resource limits (CPU, memory, process count) and the restart policy of an existing container, without restarting it:
docker update --cpus 2 --memory 2g --memory-swap 2g web
docker update --restart unless-stopped web
Ports, volumes, and environment variables cannot be changed this way; for those you replace the container as shown above.