Update Docker Images & Containers To Latest Version

Table of Contents

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.

What is a Docker image?

A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools. It is a snapshot of a file system and the parameters needed for running a specific application or service.

Docker images are built from a set of instructions written in a Dockerfile, which specifies the base image, dependencies, configuration, and other settings required for the application. These images are designed to be portable, consistent, and scalable across different environments, allowing developers to package their applications and dependencies into a single unit that can run seamlessly on any system supporting Docker.

Role of Containers

Containers are instances of Docker images, running in isolated environments. They enable consistent operation across various development and deployment environments, making it easier to develop, test, and deploy applications.

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:

the docker images or docker image ls command

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:

how To see a list of all containers, running and not running

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. 

There is an easier way to upgrade dependencies in Docker files

Renovate is an open source tool by Mend for developers and DevOps that automatically creates pull requests (PRs) for dependency updates. Our PRs embed all the information you need to ease your update decision.

Renovate supports upgrading dependencies in various types of Docker definition files. By using Renovate, you can easily keep your Docker images, containers, and IaC files up-to-date, performant, and secure.

Get Renovate now. It’s free. >>

Recent resources

What is LDAP Injection? Types, Examples and How to Prevent It

Learn what LDAP Injection is, its types, examples, and how to prevent it. Secure your applications against LDAP attacks.

Read more

How to Use Dependency Injection in Java: Tutorial with Examples

Learn how to use Dependency Injection in Java with this comprehensive tutorial. Discover its benefits, types, and practical examples.

Read more

Idempotency: The Microservices Architect’s Shield Against Chaos

Discover the power of idempotency in microservices architecture. Learn how to maintain data consistency and predictability.

Read more