Docker is a powerful containerization platform that allows developers to package applications and their dependencies into lightweight and portable containers. While managing Docker images, it is important to rebuild them efficiently to ensure consistency and smooth deployments.
In this guide, we will explore how to update a docker image, understand the essential terminologies, and walk through the crucial steps.
Updating a Docker Image
To update a Docker image, you need to follow these steps:
Checking the Current Docker Image Version
First up, check the current image version running in your container:
docker ps –format “table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.CreatedAt}}\t{{.Status}}”
To check the image version that is used to create a container:
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
docker inspect –format='{{.Config.Image}}’ <container_id>
To list all downloaded images and their versions:
docker images
Pulling the Latest Version of a Docker Image
After you know what the latest version a Docker image is, you need to pull the image version of the Docker Hub using the command:
docker pull <image_name>:latest
For a specific version:
docker pull <image_name>:<tag>
To verify the latest image is available:
docker images | grep <image_name>
Stopping and Removing the Existing Container
To update a Docker image, you first need to stop and remove the existing container.

Find the running container:
docker ps
Stop the container:
docker stop <container_id>
Remove the container:
docker rm <container_id>
If the Docker container is set in volumes, you need to remove all the volumes. You can do so:
docker rm -v <container_id>cc
Updating the Docker Container with the New Image
Then, you can start a new container with the updated image:
docker run -d –name <new_container_name> <image_name>:latest
If you need to use the same configuration as the previous container:
docker run -d –name <container_name> –restart unless-stopped -p <host_port>:<container_port> <image_name>:latest
Then verify:
docker ps
For containers managed by Docker Compose, update the service using:
docker-compose pull
docker-compose up -d
Check the container’s running image version:
docker inspect –format='{{.Config.Image}}’ <container_id>
Confirm the application is running correctly by checking logs:
docker logs <container_id>
If the container is using ports, test its availability:
curl http://localhost:<port>
Cleaning Up Old Images to Free Space
You should regularly clean containers by removing old images to free up some space. You can do so by first listing all the current images:
docker images
Then remove all the unused and dangling images by:
docker image prune -f
docker image prune -a -f
Or, you can remove a specific image by running:
docker rmi <image_id>
To free up additional space, remove stopped containers and unused volumes:
docker system prune -a
Automating Docker Image Updates
You can also automate the process to update a Docker image:
- Using Watchtower (for automatic updates)
You can Install and run Watchtower by:
docker run -d –name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower
To monitor and auto-update specific containers:
docker run -d –name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower <container_name>
- Using Docker Compose for Updates
If you are using Docker Compose yml to update images and restart services, you can use:
docker-compose pull
docker-compose up -d
- Using a Cron Job to Automate Updates
Schedule automated updates using Cron Job in Linux by:
crontab -e
0 0 * * * docker pull <image_name> && docker stop <container_name> && docker rm <container_name> && docker run -d –name <container_name> <image_name>:latest
Best Practices to Update a Docker Image
To avoid roadblocks when you update a Docker image, you can follow the following best practices:
- Use docker ps and docker inspect to verify the running image before you update a Docker image.
- Always pull a specific version (docker pull <image_name>:<tag>) to avoid unexpected updates.
- Before you update production containers, test new images in a staging environment.
- Use docker-compose up -d or docker service update for seamless updates.
- Clean up unnecessary images by running docker image prune -a.
Troubleshooting Common Update Issues
Issue | Possible Cause | Solution |
Container doesn’t start after update | Configuration or environment variable changes | Compare old and new configurations with docker inspect |
Image not updating | Local cache storing an old version | Use docker pull –no-cache <image> to force update |
Port conflicts | Old container still running on the same port | Stop the old container with docker stop <container_id> |
Data loss after update | Data stored inside the container, not in volumes | Use persistent storage (volumes or bind mounts) |
Insufficient disk space | Too many old images accumulating | Run docker system prune -a to remove unused images |
Service downtime | Updating without rolling restart | Use docker-compose up -d or docker service update |
Permissions issues | Insufficient privileges to update/remove containers | Use sudo before Docker commands |
Image not found error | Image name or tag incorrect | Verify name/tag with docker images or check the repository |
Wrapping Up – Update a Docker Image
Following this tutorial will help you to update a Docker image easily and without any hiccups. You can also keep a track of the best practices and take help from the troubleshooting guide if you encounter any issues!
1. How do I pull the latest version of a Docker image?
To pull the latest version of an image from Docker Hub, run:docker pull <image_name>:latest
2. How do I automate Docker image updates?
You can use watchtower to automatically update running Docker containers:docker run -d --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower
3. How do I clean up old Docker images after updating?
To remove unused images and free up space, run:docker system prune -a