How to Update Docker Image and Container to the Newest Version?

When you run a new Docker container, the version of the image it was based on does not automatically update to the latest version. For example, if you create a container based on the NGINX Docker image version 1.21.1, the container will continue to run with that version.

Unless you have specific reasons to run an outdated Docker image, updating to the newest is a good idea. There could be bug fixes, security improvements, etc. But how do you update the Docker container and image you’re running? That’s what we’ll explore in this post.

Requirements

  • A Linux machine with Docker already installed. This demo will be using an Ubuntu 22.04 computer.
  • An existing Docker container and image to update. This post will use an NGINX container and image.

Backup the Docker Container Information

This step is optional but recommended. Why?

In some cases, users don’t remember or did not document the conditions specified when the Docker container was first run. Such conditions include port mappings and volumes, to name a few.

ADVERTISEMENT

In this example, we set to update a Docker container and image running an NGINX web application. As you can see below, this website is accessible via HTTP://192.168.203.129:8080, and we have to ensure that this website still works after updating the Docker image and container.

update docker image

Note. The sample website above uses a free template called Forty from HTML5up.net.

Let’s run the below command to determine the container identity and which image version it uses.

docker ps -a

Based on the result, this container’s image and version are nginx:1.19.10. The container name is nginx_server, and the container ID is 40a06775ebc5.

backup docker container

The below command will save the Docker container details to a file called nginx_server_info.json.

docker inspect nginx_server > nginx_server_info.json

And now you have a backup of the container information to a JSON file. How does having this backup help you? With it, you have a reference for the container’s status, like the mounted volume and port mapping.

docker update container

Update Docker Image and Container

Let’s now update the Docker container with the latest image. This process consists of the following high-level steps:

  • Stop and delete the Docker container.
  • Pull the latest Docker image.
  • Create a new Docker container using its previous conditions (volumes, ports, etc.)

First, let’s stop the container by running this command.

docker stop [container name / ID]

docker update container image

ADVERTISEMENT

Second, delete the container.

docker rm [container name / ID]

update image docker

Third, run this command to pull the latest NGINX image version.

Note. Adding the latest tag in the command is optional. By default, Docker pulls the latest image if a specific version is not specified.

docker pull nginx:latest

docker upgrade image

As you can see, there are now two NGINX images on this host.

docker container update image

Lastly, recreate the container by running the below command:

ADVERTISEMENT

Note that this command was formulated following the backed-up information from the old container. Your command will have a different set of options.

docker run --name nginx_server -p 8080:80 -d -v /var/www:/usr/share/nginx/html nginx:latest

As you can see below, the new container is based on the latest NGINX Docker image. Compared to the old container, only the ID and image have changed.

docker refresh image

Test the New Docker Container

The new Docker container must work as expected after the image update. How you test the container depends on which application is running. But in this case, since the application is a website, we can just open the site’s URL in the browser and confirm that it works.

update a docker image

Conclusion

When you update Docker image and container, you must ensure that you’re prepared to recreate the container as it was to avoid configuration errors, application inconsistencies, and data loss. Take your time planning because that’s the most challenging part; the update comes easy once you’ve made adequate preparations.

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.