How to Upgrade a Docker Container After Its Image Changed

TLDR
To upgrade a Docker container after its image has changed, pull the updated image, stop the running container, and recreate it using the new image. Use docker-compose or orchestration tools like Kubernetes for smooth upgrades.
When working with Docker containers, you may need to upgrade a container to use a new version of its image. This guide will show you how to upgrade a Docker container after its image has changed, ensuring minimal downtime and smooth transitions.
Step 0: Use a volume for Persistent Data
Before upgrading a container, make sure that any persistent data is stored in a Docker volume or bind mount. This ensures that your data remains intact during the upgrade process. If you haven't set up a volume yet, you can do so with the following command:
docker volume create <volume-name>
Then, when you run your container, use the -v flag to mount the volume:
docker run -d --name <container-name> -v <volume-name>:/data <image-name>:<tag>
Note that if you don't use a volume, any data stored in the container's filesystem will be lost when you remove the container.
Step 1: Pull the Updated Image
Start by pulling the updated version of the image from the registry:
docker pull <image-name>:<new-tag>
Replace <image-name> and <new-tag> with the name and tag of the updated image. For example:
docker pull nginx:1.21
This command downloads the new version of the image to your local system.
Step 2: Stop the Running Container
Stop the container that is using the old image:
docker stop <container-name>
Replace <container-name> with the name or ID of the container. For example:
docker stop my-nginx
Step 3: Remove the Old Container
Remove the stopped container to free up the name for the new container:
docker rm <container-name>
For example:
docker rm my-nginx
Step 4: Create a New Container with the Updated Image
Start a new container using the updated image:
docker run -d --name <container-name> <image-name>:<new-tag>
For example:
docker run -d --name my-nginx nginx:1.21
This creates a new container with the updated image.
Step 5: Verify the Upgrade
Check that the new container is running and using the updated image:
docker ps
You should see the new container in the list.
Using Docker Compose for Upgrades
If you are using Docker Compose, upgrading a container is even easier. Update the image tag in the docker-compose.yml file:
version: '3.8'
services:
web:
image: nginx:1.21
ports:
- '8080:80'
Then run the following command to recreate the container:
docker-compose up -d
Docker Compose will pull the updated image and recreate the container.
Additional Tips
Automate Upgrades: Use CI/CD pipelines to automate image updates and container upgrades.
Minimize Downtime: Use rolling updates or load balancers to minimize downtime during upgrades.
Monitor Logs: Check container logs to ensure the new container is running correctly:
docker logs <container-name>
By following these steps, you can efficiently upgrade Docker containers to use new images while maintaining stability and minimizing downtime.
Related Resources
- Docker Compose: Always Recreate Containers: force updates
- Rebuild Docker Container in Compose: Compose rebuild
- Docker Data Loss When Container Exits: preserve data during upgrades
- Introduction to Docker Guide: Docker fundamentals
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
Docker Terminal Simulator
Practice Docker commands in an interactive browser terminal. Learn images, containers, logs, exec, Dockerfile builds, volumes, networks, and Docker Compose with a live Docker daemon visualization.
How Docker Works Under the Hood
Watch what really happens when you run docker run -p 8080:80 nginx, one layer at a time. Step down the whole stack: the CLI, the daemon, the registry pull, containerd, the OCI runtime bundle, runc, the running container, and the shared Linux kernel. Every stage shows the real low-level command you can run yourself, so it doubles as a tour of the primitives that make a container: namespaces, cgroups, and runc. A container is not a small VM, and this shows you why.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorTags
Found an issue?
Related Posts
Also worth your time on this topic
Difference Between RUN and CMD in a Dockerfile
Understand the key differences between RUN and CMD in a Dockerfile, and learn when to use each for building and running Docker containers.
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes