Skip to content

Instantly share code, notes, and snippets.

@pplmx
Last active April 27, 2023 03:28
Show Gist options
  • Save pplmx/db74bb991b6fc55a603d0386f6366141 to your computer and use it in GitHub Desktop.
Save pplmx/db74bb991b6fc55a603d0386f6366141 to your computer and use it in GitHub Desktop.
Some docker useful commands

Docker CLi

docker image

  1. Update all images with local download tag
# 1. list images
# 2. split with the space, and filter out the first line, and the 2nd element is not "none", and finally combine the 1st and 2nd element
# 3. update the docker images and prune finally
docker image ls | \
    awk '(NR>1) && ($2!~/none/) {print $1":"$2}' | \
    xargs -L1 docker image pull; docker image prune -f
  1. make all images not starting with harbor.test.com create a new tag with the starts harbor.test.com/3rd
docker image ls | \
    awk '(NR>1) && ($2!~/none/) && ($1!~/^harbor.test.com/) {print $1":"$2}' | \
    xargs -I @ docker image tag @ harbor.test.com/3rd/@
  1. push all images
docker image ls | \
    awk '(NR>1) && ($2!~/none/) && ($1~/^harbor.test.com/) {print $1":"$2}' | \
    xargs -L1 docker image push

Notes

awk:

  • NR>1: NR means Number of Record; NR>1 means only rows above 2 are processed (skipping the first row)
  • $2!~/none/: the 2nd element should not be "none"
  • $1!~/^harbor.test.com/: the 1st element should not start with "harbor.test.com"

xargs:

  • -I @: let the args in the middle
@pplmx
Copy link
Author

pplmx commented Apr 27, 2023

Update images without the prefix harbor.test.com

docker image ls | \
    awk '(NR>1) && ($1!~/^harbor.test.com/) && ($2!~/none/) {print $1":"$2}' | \
    xargs -L1 docker image pull; docker image prune -f

@pplmx
Copy link
Author

pplmx commented Apr 27, 2023

Push all the images with the prefix harbor.test.com/3rd, then remove these images

docker image ls | \
    awk '(NR>1) && ($2!~/none/) && ($1~/^harbor.test.com\/3rd/) {print $1":"$2}' | \
    xargs -L1 docker image push; \
docker image ls | \
    awk '(NR>1) && ($2!~/none/) && ($1~/^harbor.test.com\/3rd/) {print $1":"$2}' | \
    xargs -L1 docker image rm; \
docker image prune -f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment