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:
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:
By default, Docker pulls the latest image version. If unsure about the host system defaults, add the latest
tag.
To update to a specific version number, use the following syntax:
For example, the following command pulls the latest mysql image from Docker Hub:
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:
In the example below, the output shows a container using the mysql:5.7.31 image.
2. Stop the container with the following command:
3. Wait for the container to stop, then remove it by typing:
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:
Or in my case, I use Docker Compose
docker compose up [image] -d
No comments:
Post a Comment