How to Update Docker Image and Container

July 11, 2024

Introduction

Docker does not automatically update underlying images in running containers. Once a Docker image is used to create a container, the container continues running the same image version even after new releases come out.

In this tutorial, you will learn how to update Docker images and running containers to the latest version.

How to update Docker images and containers.

Prerequisites

  • Command-line access.
  • Administrative privileges on the system.
  • Docker installed.

Why Update Docker Image and Container?

Keeping Docker images and containers up to date is a recommended practice for containerized apps. The following list introduces some of the essential reasons to update:

  • Security. Image updates frequently include security patches that fix previously unidentified vulnerabilities. Running containers based on outdated images can pose a significant security risk.
  • New features. Updated images often bring new capabilities, performance improvements, and optimizations.
  • Compatibility. Updating ensures compatibility with the latest libraries and software dependencies.
  • Compliance. Some applications need to be updated to remain compliant with industry standards and regulations.

How to Update Docker Image and Container

Updating a container with a new image starts with pulling the image from a repository. Once the image is downloaded to the host system, the user must stop and remove the old container and launch a new one with the same configuration parameters.

Follow the steps in the sections below to update your container with the newest Docker image.

Note: This tutorial uses an example of running a MySQL Docker container to illustrate how to update the Docker image and container to the latest version.

Step 1: Check Current Version

Identify the image that needs to be updated by inspecting the images on your system. To view a list of locally available images, use the following command:

docker images

The output displays downloaded images and their tags, which often contain version numbers. In the example below, the mysql image is based on the outdated version 5.7.31.

A docker images output shows an outdated MySQL image.

Note: To inspect a specific image, use the docker images | grep [image].

Step 2: Pull Latest Docker Image

Download the newer version of the image using the docker pull command:

docker pull [image]

By default, Docker pulls the latest image version. If unsure about the host system defaults, add the latest tag.

docker pull [image]:latest

To update to a specific version number, use the following syntax:

docker pull [image]:[version-number]

For example, the following command pulls the latest mysql image from Docker Hub:

docker pull mysql:latest

Docker downloads the image and outputs the status report.

Docker pulls the latest MySQL image.

Step 3: Stop and Remove Running Container

After obtaining an updated image, stop and remove the container based on the old image. This lets you launch the new one under the same name. Follow the steps below to remove a Docker container:

1. Find the name of the outdated container by listing the containers running on the system:

docker ps

In the example below, the output shows a container using the mysql:5.7.31 image.

The output of docker ps shows a running container based on an outdated image.

2. Stop the container with the following command:

docker stop [container-id-or-name]

3. Wait for the container to stop, then remove it by typing:

docker rm [container-id-or-name]

Note: Both container ID and name are visible in the output of docker ps.

Step 4: Launch New Updated Container

Recreate the container with the docker run command using the configuration parameters of the previous container. Below is the basic docker run syntax:

docker run [options] [image] [commands]

If applicable, mount a Docker volume assigned to the previous container to ensure the updated container can access the necessary files. To do this, use the -v option followed by the path to the volume directory.

The following example runs an updated MySQL container in the detached mode:

docker run --name=mysql --restart=always -e MYSQL_ROOT_PASSWORD=mypassword -v /home/marko/mysql:/var/lib/mysql -d mysql
Running a new MySQL container with the newest image version.

Check whether the container runs the latest Docker image by listing the containers:

docker ps

The output shows that the container now uses the latest mysql image.

Checking that the new container uses the latest MySQL image.

Conclusion

By following this tutorial, you should be able to successfully update your Docker container with the latest release of the underlying Docker image. Running containers from the latest Docker image is considered best practice unless you have a specific reason for using an older release.

Want to learn more about managing containers? Read all about best practices for managing Docker containers.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Docker Volumes: How to Create & Get Started
July 27, 2020

Persist data in Docker containers by mounting Docker volumes to containers. You can use an existing host...
Read more
How to Set Docker Memory and CPU Usage Limit
December 6, 2023

Docker containers have unlimited access to RAM and CPU memory of the host. This is not the recommended...
Read more
How to Override Entrypoint Using Docker Run
September 18, 2024

Entrypoint is a Docker instruction used to set up the default executable when the container is run. You can...
Read more
Docker CMD vs Entrypoint Commands: What's the Difference?
February 18, 2020

CMD is Docker instruction used if you need a default command which users can easily override. ENTRYPOINT is...
Read more