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