I’ve read Docker documentation and I do not understand whether --pull
option pulls my image (if it was already built before) or this option only pulls base images that I use in my Dockerfile.
My current code:
- docker pull $MY_IMAGE
- docker build --pull --target deploy --cache-from $MY_IMAGE -t $MY_IMAGE .
Is calling docker pull $MY_IMAGE
redundant if I have --pull
in the docker build
command?
Should my code be like:
- docker build --pull --target deploy --cache-from $MY_IMAGE -t $MY_IMAGE .
?
What is my main goal
I need to make sure that my image (that maybe was already built before) is available and up-to-date for further using in --cache-from
.
2
Answers
The
docker build --pull
will always try to find the newest version of the base image. It will use your cached image and (or) your dockerfile image base image and tag.If you use your own image for example, it will try to pull newer layers from your repository,
If you are using Ubuntu for example, it will try to use the latest or layer that’s been pushed.
For your goal this purpose for what I see gives you the ability to search for new layers that might be published and pull them without specifying the current version. – keeping you up to date.
If you run
docker build -t "$MY_IMAGE" .
, it will always build a new image (or reuse a cached image) and tag it with$MY_IMAGE
. Anything that you had previouslydocker pull "$MY_IMAGE"
will lose its name (assuming it was a different image).There’s no reason to pull an old build of your image before you build a new one.
docker build --pull
option pulls the image(s) in your Dockerfile’sFROM
line(s). This can be useful since images likeubuntu:20.04
orpython:3.10
do regularly get security updates and patch releases, and (assuming you have network connectivity) it’s inexpensive if you already have the most recent version of the base image(s).