Update Docker Images & Containers To Latest Version

Update Docker Images and Containers To The Latest Version

When working with Docker, everything revolves around images. And when you’ve used an image to deploy a container, it continues running that image version, even if a new release is existing.

Since images within launched containers cannot self-update, it’s essential to use other ways to keep them up-to-date and optimize your applications’ performance. 

This article demonstrates how to upgrade a deployed Docker container based on an updated image version.

Importance of updating Docker images and containers

Unless you have a convincing reason to use an older version, it’s recommended to run Docker containers using the most up-to-date image releases.

Here are some reasons for keeping them up-to-date:

  • Updated images often come with patch fixes that can enhance the security of your applications. 
  • Upgraded images often include new or improved features that can boost the performance of your applications. 
  • Updated images often remove outdated features that can otherwise impair smooth compatibility with various devices or applications. 

How to update Docker images and containers

Let’s talk about the steps of upgrading a Docker image and a container to the version you desire.

Step 1: Check current image version

To find out if your Docker container requires an upgrade, you need to check the version you are using. You can get this information by looking at the set of identifiers and tags that come with every image pulled from a Docker Registry such as the Docker Hub.

You can use any of the following two simple methods to check if your image is outdated:

  • List images on your system

To see the images on your system, run the docker images or docker image ls command.

Here is an example:

Using docker images or docker image ls command to see a list of images on system

 

If you check the output of either of the commands, you’ll see that they list the available images, alongside their version tags, IDs, and other stuff. It’s the version tag that we want here.

For example, the version of the mysql image is 8.0.

  • Inspect the images further

If an image is tagged with a version name, such as latest, you may need to dig further to know the exact version of the image. 

Remember that the latest tag may not actually mean that the image is the latest version. It’s the default tag that Docker applies to any image without a specific tag. 

It’s not dynamic and may not track the most recent push. 

So, it may be confusing to use, at times.

For example, these two commands will both create a new image that is tagged as latest:

docker build -t user/image_name .

 

  docker build -t user/image_name:latest .

So, to know the exact version of such an image, you may run the docker inspect command, which outputs detailed low-level information about the image.

Here is its syntax:

  docker inspect <image_id>

Let’s run the command against the bash image on our system. Note that we got its ID after running the docker images command.

Here is the command:

  docker inspect 39a95ac32011

Here is the output (for brevity, it’s truncated):

Output of running docker inspect command to check image version

From the above output, you can see that the version of the bash image, which is tagged as latest, is 5.0.18.

Step 2: Stop the container

After knowing the outdated image version the deployed container is using, the next step is to stop the container.

First, you’ll need to use the docker ps command to see a list of all containers currently running on your system.

To see a list of all containers, running and not running, add the -a (or –all) flag.

Here is an example:

Then, stop the existing container by running the docker stop command.

Here is its syntax:

  docker stop <container_id>

For example, here is how to stop the mysql container:

Using docker stop command to stop a container

It’ll return the ID of the deployed container.

Step 3: Remove the container

After stopping the running container, you can now use the docker rm command to remove it. 

Here is its syntax:

  docker rm <container_id>

Here is how to remove the mysql container:

Using docker rm command to remove a container

It’ll return the ID of the container.

You may also remove its associated image by using the docker rmi command. 

Here is its syntax:

  docker rmi <image_id>

Here is how to remove the mysql image:

Using docker rmi command to remove an image

Step 4: Pull your desired image version

Next, you can look for the version of the image you need to update to.

For example, this is a screenshot from Docker Hub showing the mysql version we need to use for carrying out the Docker update image task:

Docker Hub screenshot showing mysql version

You can see that the version is tagged as 8.0.22.

To download the image from Docker Hub, you can use the docker pull command. 

Here is its syntax:

  docker pull <image_name:image_tag>

Here is how to pull the mysql image:

Using docker pull command to download an image from Docker Hub

Step 5: Launch the updated container

After downloading the new image, you can use it to recreate the container by executing the docker run command. 

Here is its syntax:

  docker run <image_name:image_tag>

Here is how to carry out a Docker update container task for the downloaded mysql image:

Undertaking Docker update container task based on an upgraded image

Note that the docker run command will start by looking for the image on your local system. If it’s not already present, it’ll download the image from the Docker Hub registry.

So, you may use the docker run command and avoid step 4 above.

Step 5: Verify the update

Lastly, you may need to ascertain if your Docker update image efforts were successful.

You may run the docker images or docker ps -a command to verify if everything went well.

Here is an example:

Checking the success of Docker update image tasks

As you can see on the image above, the Docker update container process for mysql went well. 

Conclusion

That’s how to upgrade a Docker container using an updated image version. 

Of course, the above manual process is demanding and time-consuming. 

WhiteSource Renovate is a free, open-source tool that allows you to save time and reduce risks by carrying out Docker updates automatically. 

It’s a free, open-source tool you can use to set up an automated workflow that scans Docker Hub for any image upgrades and gracefully makes the updates for you.

It’s the tool you need to keep your Docker images and containers up-to-date, performant, and secure. 

Guy Bar-Gil / About Author

Guy is a product manager at WhiteSource, where we enable software development teams to integrate open source fearlessly and without compromising agility. Before WhiteSource, Guy worked for the IDF’s intelligence division, where he spent time as a combat operator and project manager. Outside of work, you can find Guy reading (everything from fiction to physics), playing and watching sports, traveling the world, and spending time with friends and family.

LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *