CHEAPER EACH MONTH - LIMITED DEAL Learn more

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

September 3rd, 2023
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

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.

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 images on the system, run:

docker images

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

Confirm the image version by inspecting 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:

Check the image version in the registry

Now to pull this version of Nginx from the registry, run:

docker pull nginx:1.25.2

Pull the latest version of the Nginx image from the registry

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.

List running containers

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>

Stop container

Remove container

You can also delete the old image using the docker rmi command like so:

docker rmi <IMAGE ID>

Delete old image

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.

Recreate container with the new image version

To confirm that the container you just ran is using the new version, check the running containers using:

docker ps

List running containers

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.

Helping engineers learn 💡 about new technologies and ingenious IT automation use cases to build better systems 💻

Join Cherry Servers Community

Get monthly practical guides about building more secure, efficient and easier to scale systems on an open cloud ecosystem.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 96f862b.436