Recently, we moved a client to Docker and we needed to give them a way to automagically update all “latest” Docker images.
Since Docker does not have a single command to update all pulled images we used this one-liner to update all images at once:
docker images --format "{{.Repository}}:{{.Tag}}" | grep ':latest' | xargs -L1 docker pull;
The above command will:
- Print all images in the format
RepositoryName:Tag
- Then it will filter all lines that end with the suffix
:latest
(which is the tag we are interested in) - Finally, for each result (which is one per line) it will be fed via the command
xargs -L1
to the commanddocker pull
Please note that you cannot really update an existing container using docker commands, what you need to do is actually:
- Stop the container whose image was updated
- Delete it
- Recreate it using the parameters of the previous container
As you understand, it is a good practice to save all of your data in volumes outside the container to make the update process easy.
For example, below you will find the commands using which we updated the jwilder/nginx-proxy
and the jrcs/letsencrypt-nginx-proxy-companion
images along with the two containers that were using them:
docker container stop nginx-proxy nginx-letsencrypt; docker container rm nginx-proxy nginx-letsencrypt; docker run -d -p 443:443 \ --name nginx-proxy \ --net reverse-proxy \ -v $HOME/certs:/etc/nginx/certs:ro \ -v /etc/nginx/vhost.d \ -v /usr/share/nginx/html \ -v /var/run/docker.sock:/tmp/docker.sock:ro \ -v $HOME/my_proxy.conf:/etc/nginx/conf.d/my_proxy.conf:ro \ --label com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true \ jwilder/nginx-proxy:latest; docker run -d \ --name nginx-letsencrypt \ --net reverse-proxy \ --volumes-from nginx-proxy \ -v $HOME/certs:/etc/nginx/certs:rw \ -v /var/run/docker.sock:/var/run/docker.sock:ro \ jrcs/letsencrypt-nginx-proxy-companion:latest;
This post is also available in: Greek