How to update Containers?

What are we going to update exactly?

Containers are usually made of two things.

  • Container image
  • Container configuration

Container image is what we are going to actually update while preserving container configuration. Containers usually store theirs configuration in a persistent volumes, each container can have one or more volumes attached. Usually container declaration require you to define volumes i.e. for /config or /data - they are actually a pointers to a folder on Docker host file system where you keep data that did not came with the container i.e. your media files or documents etc.

The simplest way to be sure that container you want to update image for will retain its configuration is to restart that container. You have to understand that when you start any container you actually running an image that cannot be changed and each time you stop and start that container this image is loaded in its original state, then on top of that your configuration is read and applied form persistent volume. If configuration is not stored persistently in a volume each time you restart container its config is wiped out with default state, read form the image itself. Therefore your configuration stored in volumes is critical but container and its image can be replaced with no consequences of loosing anything.

If you can stop and start container with no issues and changes you made to the configuration are still there after, you can be sure that updating container image should not impact configuration work you already did on this container earlier.

You can stop and start container using Portainer or by running the commands in CLI:

List all the containers with docker ps and take a note of CONTAINER ID for the container you want to stop, start or restart.

pi@docker121:~$ docker ps
CONTAINER ID   IMAGE                                 COMMAND        CREATED       STATUS 
56edd6cde5c1   portainer/portainer-ce                "/portainer"   4 weeks ago   Up 10 days  
  • pi@docker121:~$ docker stop 56edd6cde5c1 - stop container
  • pi@docker121:~$ docker start 56edd6cde5c1 - start container
  • pi@docker121:~$ docker restart 56edd6cde5c1 - restart container

List container again with docker ps and see if STATUS Up time changed?

pi@docker121:~$ docker ps
CONTAINER ID   IMAGE                                 COMMAND        CREATED       STATUS 
56edd6cde5c1   portainer/portainer-ce                "/portainer"   4 weeks ago   Up 40 seconds  

If after above Portainer is still as it was before, configured and accepting login password etc. then you can replace its image with the new one.

How to update container image?

In order to update container image with the new one we would have to recreate this container with image that we pull fresh from the repository. For this to work we need to know what image to pull and from where? All the answers are available by running docker ps command under section IMAGE

Let's continue with Portainer as an example, the process will be exactly the same for any other container with the difference of CONTAINER ID you stop, remove and recreate and IMAGE you pull.

List all the images that docker knows about so we can compare them later with the one the we pull as a replacement.

pi@docker121:~$ docker image ls
REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
portainer/portainer-ce         latest    0df02179156a   6 weeks ago     273MB

Now an actual update itself:

  • docker stop 56edd6cde5c1 - stop the container
  • docker rm 56edd6cde5c1 - remove the container
  • docker image rm 0df02179156a - remove container image
  • docker pull portainer/portainer-ce - pull new image

As you can see below in my case image I have is the latest so nothing new was pulled down.

pi@docker121:~$ docker pull portainer/portainer-ce
Using default tag: latest
latest: Pulling from portainer/portainer-ce
Digest: sha256:4f126c5114b63e9d1bceb4b368944d14323329a9a0d4e7bb7eb53c9b7435d498
Status: Image is up to date for portainer/portainer-ce:latest

In your case new image might be downloaded and ready, if that the case recreate container that we removed earlier, just go inside LMDS folder and run docker-compose up -d portainer

How to remove old or unused images?

While you pulling newer images and recreating containers using these new images old ones are still there in case you would like to use them for some reason, i.e. if new image does not work properly - you can always go back to older version. In case you would like to save some space and delete all unused images - including images for all the containers that are not started at the time, use: docker image prune -a

More details on pruning docker images find here

Help me make LMDS better

With your support anything is possible