Tuesday, September 17, 2024

How-To Update a Docker Container

  I run numerous docker containers and have had need to update them and maintain the original container names.  The follow steps have worked flawlessly and have allowed me to update the containers within a few minutes.


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] 


Or in my case, I use Docker Compose

docker compose up [image] -d

No comments: