How to Update a Docker Image | Step-by-Step Guide

With their ability to package applications and their dependencies into portable containers, Docker images have empowered developers to go through the complexities of different environments easily.
However, like most technologies, Docker images need updates to keep your applications safe, secure, and at peak performance. In this article, you'll learn the importance of updating Docker images and a step-by-step process of how to update a Docker image.
#Prerequisites
Before you start this tutorial make sure that:
- You have the latest version of Ubuntu installed on your system.
- Docker is installed on your host machine.
- Your account has admin rights.
#What is a Docker image?
Before we talk about Docker image updates, let's set the stage with a brief overview of Docker and Docker images. Docker is a platform that enables developers to package everything an application needs to run, including the code, runtime, and libraries into an executable software package called Docker image which in turn can be used to run or launch containers - portable, isolated environments ensuring that the applications run consistently across different environments, from local development machines to production servers, eliminating the notorious "it works on my machine" situation.
Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.
#Why update Docker images?
Just like your phone's operating system needs updates to stay secure and functional, Docker images also need regular updates. Ignoring them can result in security risks, compatibility, and performance issues. Regular updates ensure up-to-date security, bug fixes, and new features, leading to better performance and safety. Below are three reasons why you have to regularly update your Docker images.
-
Updating to new dependency packages: A Docker image depends on many external libraries and components. These components get updates from time to time. Some of the updates include critical security updates and performance optimization. By updating to the latest dependency package you reduce vulnerabilities detected in the previous version and upgrade functionality.
-
Performance improvements: Image updates often include optimisations such as reduced image size, faster container startup times, and better resource management. These performance improvements can lead to more efficient use of hardware resources, quicker deployment times, and an overall smoother operation in production environments.
-
Application bug fixes: Whenever you release a new application version that contains bug fixes and UI optimisations, you have to build and update the Docker image used to launch the container. This only applies when you are using images that you built locally, not the ones you pulled from DockerHub or another image repository.
#How to update a Docker image: Step-by-step process
Unfortunately, there's no way to automatically update Docker images in a running container using Docker commands. The container keeps using whatever version it was created with, even after new versions of the image are released. Instead, you have to stop the container whose image was updated, delete it, and recreate it using the latest Docker image version. Follow the below steps to update Docker images.
#Step 1: Confirm the version of the current Docker image
First, you need to confirm the version of the particular Docker image you want to update. We'd be updating an Nginx image on a local Docker environment for this demonstration. To list the Docker images on the system, run:
docker images
You will get the following output that shows available images.
The TAG
columnon the above image shows the image version. Another way to confirm the image version, as the tag can be something other than the version number, is to use the docker inspect
command to see more details. This can help, especially when you have an image on your environment with “latest” as its tag:
docker inspect <IMAGE ID>
In the Env
section of the output, you should see the version of the image.
#Step 2: Pull the latest Docker image
Next, you need to find out what the latest version of the Docker image is and pull the image to your Docker environment. You can check DockerHub for the latest images. For this, we're using the Nginx repository on Docker Hub.
Start by navigating to the Nginx repository. From the "Tags" tab, you can see the different image tags. You'd usually just pull the image with the "latest" tag but it's best to pull using the version number. You can check the version of Nginx for the latest image by clicking on the tag to view more details. As of the time of this writing, the version of Nginx used for the latest image is 1.25.2
as can be seen in the screenshot below:
Now to pull this version of Nginx from the registry, run:
docker pull nginx:1.25.2
You will get the following output that shows that the image has been pulled successfully.
#Step 3 : Stop and remove running containers
Next, check containers running with the older image using:
docker ps
This command will list containers running on your Docker system.
From the image, you can see that one container is using an nginx:1.23.4
image. Copy the container ID as you would need this to stop and remove the container. To stop and remove the container, start by stopping the container:
docker stop <CONTAINER ID>
Next, remove the container using its ID.
docker rm <CONTAINER ID>
If the container has been removed successfully then its ID will be shown on the output as shown below.
You can also delete the old image using the docker rmi
command like so:
docker rmi <IMAGE ID>
#Step 4 : Create and run containers with the updated image
Since you've pulled the latest version of nginx, you can create containers with the new image using the docker run
command:
docker run -d nginx:1.25.2
This will run the container in the background, printing out the container ID in the terminal.
To confirm that the container you just ran is using the new version, check the running containers using:
docker ps
Also read: How to push an image to DockerHub
#Conclusion
In this guide, we showed how to update a Docker image. When updating Docker images, you're not really updating, but removing the old Docker image and starting up new containers with the new image version. By staying proactive and regularly updating your Docker images, you ensure your applications continue to thrive in a rapidly changing environment. With this newfound knowledge, why not see if you have containers with Docker images that need updating?
Thanks for learning with Cherry Servers! Our open cloud infrastructure gives developers full control, stable workloads, and free technical support 24/7. We offer simple, secure, and cost-effective cloud services, including dedicated and virtual servers, custom servers, and more.
Cloud VPS Hosting
Starting at just $3.24 / month, get virtual servers with top-tier performance.