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
- Docker or Docker Desktop installed.
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.
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
Looking at the TAG
column, you should see the version of the image. 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 docker repositories for the latest images. For this, we're using the Nginx repository on Docker Hub. 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
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 running with a 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, run:
docker stop <CONTAINER ID>
docker rm <CONTAINER ID>
You can also delete the old image using the docker rmi
command like so:
docker rmi <IMAGE ID>
Explore how web hosting service provider Debesis improved its service quality, performance, and reliability by migrating to Cherry Servers' bare-metal servers.
"Cherry Servers engineers always help when we need them, while their customer service quality is a blast!"
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, anonymous cloud hosting, custom servers, and more.