Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of "docker build --pull"?

Tags:

docker

When building a docker image you normally use docker build ..

But I've found that you can specify --pull, so the whole command would look like docker build --pull .

I'm not sure about the purpose of --pull. Docker's official documentation says "Always attempt to pull a newer version of the image", and I'm not sure what this means in this context.

You use docker build to build a new image, and eventually publish it somewhere to a container registry. Why would you want to pull something that doesn't exist yet?

like image 409
Jim Aho Avatar asked Oct 21 '19 14:10

Jim Aho


People also ask

What is the use of docker pull?

Docker enables you to pull an image by its digest. When pulling an image by digest, you specify exactly which version of an image to pull. Doing so, allows you to “pin” an image to that version, and guarantee that the image you're using is always the same.

Does docker pull build the image?

If you have a Dockerfile for an image, and that image is also already in some repository, docker pull will pull the binary copy of the image from the repository, whereas docker build will rebuild it from the Dockerfile.

What is difference between docker and docker pull?

The docker pull command downloads Docker images from the internet. The docker image command lists Docker images that are (now) on your computer. The docker run command creates running containers from images and can run commands inside them.

What is difference between docker build and docker build?

Difference between docker-compose and Dockerfile. The key difference between the Dockerfile and docker-compose is that the Dockerfile describes how to build Docker images, while docker-compose is used to run Docker containers.


Video Answer


3 Answers

it will pull the latest version of any base image(s) instead of reusing whatever you already have tagged locally

take for instance an image based on a moving tag (such as ubuntu:bionic). upstream makes changes and rebuilds this periodically but you might have a months old image locally. docker will happily build against the old base. --pull will pull as a side effect so you build against the latest base image

it's ~usually a best practice to use it to get upstream security fixes as soon as possible (instead of using stale, potentially vulnerable images). though you have to trade off breaking changes (and if you use immutable tags then it doesn't make a difference)

like image 75
Anthony Sottile Avatar answered Oct 22 '22 06:10

Anthony Sottile


Docker allows passing the --pull flag to docker build, e.g. docker build . --pull -t myimage. This is the recommended way to ensure that the build always uses the latest container image despite the version available locally. However one additional point worth mentioning:

To ensure that your build is completely rebuilt, including checking the base image for updates, use the following options when building:

--no-cache - This will force rebuilding of layers already available.

The full command will therefore look like this:

docker build . --pull --no-cache --tag myimage:version

The same options are available for docker-compose:

docker-compose build --no-cache --pull

like image 30
Soumen Mukherjee Avatar answered Oct 22 '22 05:10

Soumen Mukherjee


Simple answer. docker build is used to build from a local dockerfile. docker pull is used to pull from docker hub. If you use docker build without a docker file it throws an error.

When you specify --pull or :latest docker will try to download the newest version (if any)

Basically, if you add --pull, it will try to pull the newest version each time it is run.

like image 3
DUDANF Avatar answered Oct 22 '22 04:10

DUDANF