Home > Software design >  "docker build --pull" VS "docker pull $MY_IMAGE && docker build $MY_IMAGE"
"docker build --pull" VS "docker pull $MY_IMAGE && docker build $MY_IMAGE"

Time:07-03

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.

CodePudding user response:

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 previously docker 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's FROM line(s). This can be useful since images like ubuntu:20.04 or python: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).

CodePudding user response:

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.

  • Related