How to Update Docker Image and Container to the Latest Version

August 13, 2020

Introduction

Docker images within a running container do not update automatically. Once you have used an image to create a container, it continues running that version, even after new releases come out.

It is recommended to run containers from the latest Docker image unless you have a specific reason to use an older release.

In this tutorial, you will learn how to update the Docker image and container to the latest version.

How to update docker image and container to the latest version.

Prerequisites

  • Access to a command line/terminal window
  • A user account with sudo privileges
  • An existing Docker installation
  • An existing Docker container and Docker image

Update Docker Image and Container to the Latest Version

The best way to update an existing container with the newest image is to download the latest image and launch a new container with the same configuration. Follow the steps listed 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

Verify you have an outdated image, by listing the images on your system with the command:

sudo docker images

The output displays downloaded images and their tags (version numbers). In the example below, the system shows it is using the mysql image version 5.7.31. Docker’s official MySQL images listed on DockerHub show that the latest version (at the time of writing) is 8.0.21.

List Docker images.

Therefore, if you have a container running on that image, it is best to update it.

Note: To list only a specific image that may be outdated, use the docker images | grep [docker_image] command to narrow the search.

Step 2: Pull the Latest Image

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

sudo docker pull [docker_image]

By default, Docker pulls the latest version. To ensure it does so, you can add the :latest tag.

For instance, to pull the latest mysql image, you would run:

sudo docker pull mysql/mysql-server:latest

Step 3: Launch a New Updated Container

Once you downloaded the latest Docker image, you need to stop and remove the old container. Then, create a new one with the latest image.

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

sudo docker ps

In this example, the output shows a container using the mysql/mysql-server:5.7.31 image.

List Docker containers.

2. Stop and remove the existing container so you can launch a new one under the same name:

sudo docker stop [container_id]
sudo docker rm [container_id]

Replace [container_id] with the ID number of the container.

3. Recreate the container with the docker run command and the wanted configuration, using the updated Docker image:

sudo docker run --name=[container_name] [options] [docker_image]

If you have one, make sure to mount a Docker volume assigned to the previously used container to ensure the updated container has the same content. To do this, use the -v option followed by the path to the volume directory.

For instance, to run an update MySQL container, you would run:

sudo docker run --name=mysql --restart=always -e MYSQL_ROOT_PASSWORD=mypassword -v /path/to/directory:/var/lib/mysql -d mysql

4. You can check whether your container has been updated with the latest Docker image by listing the containers with:

sudo docker ps
Updated Docker container.

Conclusion

With this, you should have successfully updated your Docker container with the latest release of a Docker image.

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

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
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
June 29, 2020

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
April 10, 2020

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