How to Update a Docker Image: A Step-by-Step Guide

update a docker image

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

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: 

Tech Delivered to Your Inbox!

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. 

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

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: 

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

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

  1. 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: 

  1. Use docker ps and docker inspect to verify the running image before you update a Docker image. 
  2. Always pull a specific version (docker pull <image_name>:<tag>) to avoid unexpected updates.
  3. Before you update production containers, test new images in a staging environment. 
  4. Use docker-compose up -d or docker service update for seamless updates.
  5. Clean up unnecessary images by running docker image prune -a.

Troubleshooting Common Update Issues

IssuePossible CauseSolution
Container doesn’t start after updateConfiguration or environment variable changesCompare old and new configurations with docker inspect
Image not updatingLocal cache storing an old versionUse docker pull –no-cache <image> to force update
Port conflictsOld container still running on the same portStop the old container with docker stop <container_id>
Data loss after updateData stored inside the container, not in volumesUse persistent storage (volumes or bind mounts)
Insufficient disk spaceToo many old images accumulatingRun docker system prune -a to remove unused images
Service downtimeUpdating without rolling restartUse docker-compose up -d or docker service update
Permissions issuesInsufficient privileges to update/remove containersUse sudo before Docker commands
Image not found errorImage name or tag incorrectVerify 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

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!