How to upgrade docker container with previous network and volumes?


This article is focused on the upgrading mechanism of container images. There are various methods to upgrade a container to its newest image. Here we have used some of the simple and easy-to-use methods. First, we discussed the manual method for a quick test. Then we tried some automatic methods. These methods will monitor the image and if any new update is present, they create a new container with the updated image.


  • Manually but using CLI

  • Automatic but using CLI

  • Automatic but using Docker Compose

Manually but using CLI

Here we are going to follow the below steps to achieve the goal of an image upgrade.

  • Create a container with the old image

  • Download the updated image on the local system

  • Create a new container with the updated image but with old volumes and networks

  • Delete the old container

Step 1: Create an old image Docker container

Use the below command to create a busybox container.


$ docker run -itd --name old_cont --volume myvolume:/volumes --network mynetwork busybox:unstable



Get inside this image and create a file inside the volume directory.

$ docker exec -it old_cont sh / # cd volumes/ /volumes # touch myfile.txt /volumes # ls myfile.txt /volumes # exit

Step 2: Create a container with the new image

This container has the latest image of the busybox.


$ docker run -itd --name new_cont --volume myvolume:/volumes --network mynetwork busybox:latest



Get inside the new container and check if the data is present.

$ docker exec -it new_cont sh / # cd volumes/ /volumes # ls myfile.txt /volumes # exit

Hence the volume and the network remain the same. Only the image of the container is changed to the latest.

Step 3: Remove the old container


$ docker rm -f old_cont



In the next method, we are going to automate the above process with the help of the “watchtower” image.

Automatic but using CLI

Here we will take help from an extra image called “watchtower”. This “watchtower” will check if any updated image is available on the repository. As soon as any update is pushed to the repository this watchtower will automatically download the updated image and create a newly updated container with all the old data that was linked to the old container.

Step 1: Create a container

First of all, we need to create a container with the old image.

docker run -itd --name old_cont busybox:unstable unstable: Pulling from library/busybox 2461e8255644: Pull complete Digest: sha256:f4ed5f2163110c26d42741fdc92bd1710e118aed4edb19212548e8ca4e5fca22 Status: Downloaded newer image for busybox:unstable 81470b0f373557b3cb057e86113f6e11ce0baec23844e1370f519e043ff3db53

The container is created and running.

Step 2: Now create the watchtower container

Here create the “watchtower” container and add the name of the above container for upgrading.


$ docker run -itd --name watch_container -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower old_cont


Unable to find image 'containrrr/watchtower:latest' locally
latest: Pulling from containrrr/watchtower
560f024ada32: Pull complete
03aa1c411c91: Pull complete
4e2295fcaa5d: Pull complete
Status: Downloaded newer image for containrrr/watchtower:latest

Now this will take care of everything, whenever the busybox:unstable is updated on the docker hub, watchtower will automatically upgrade this container to the updated image.

Automatic but using Docker Compose

we will create a Docker Compose file. This compose file will have multiple containers that need to be monitored. One container of “watchtower” will monitor all these containers. Whenever there is an update in the image of any of the containers, it will create a new container for that image.

Step 1: Create a Docker Compose file

Docker compose contains a total of three services or containers. Two containers “old_cont” and “test_cont” are monitored by the third container “watch_cont”.

version: "3" services: old_cont: image: busybox:latest container_name: busybox_container command: sleep infinity test_cont: image: busybox:unstable container_name: testing_container command: sleep infinity watch_cont: image: containrrr/watchtower container_name: watchtower_container volumes: - /var/run/docker.sock:/var/run/docker.sock command: --interval 60 busybox_container testing_container

The watchtower will wait for 60 seconds a check if any updated image is present on the docker hub repository. If we do not give the --interval option then it will wait for 24 hours.

Step 2: Start the containers

To start all these containers use the below command.

$docker compose up -d

This will create and run all these containers in detached mode.


We were successfully able to create an upgrade cycle for the container. Even though these methods are not used in the production environment because we have dedicated software for this task. Some advanced tools are used for the task of updates and upgrades.