Docker

How to Use “–pull” to Force to Pull Base Image Before my Build?

The “docker build” is an essential command line utility of the Docker environment is used to build the Docker image by reading the Dockerfile. The Dockerfile includes the necessary instructions to create the Docker image. This command supports the different options, and “–pull” is one of them. The Docker “–pull” option is utilized to pull the base image specified in Dockerfile before the new build.

This article will illustrate how to force pull before a new build.

How to Use “–pull” to Force to Pull Base Image Before a New Build?

In Docker, the “–pull” option is supported by the “docker build” command. It is used to pull and use the latest or newest version of the base image specified in the Dockerfile before building the new image.

Go through the instructions to use the “-pull” option.

Step 1: Create Dockerfile

First, make a new Dockerfile and paste the provided instructions into the file:

FROM python
WORKDIR /src/app
COPY . .
CMD [ "python", "./pythonapp.py" ]

 

The above-coded instructions use the “python” as a base image and containerize the “pythonapp.py” program:

Step 2: Create Program File

Next, create a new program file named “pythonapp.py” and paste the provided code into the file:

print("Hello, I am building first Python application")

 

Step 3: Build Image

Build the docker image through the given command:

> docker build --pull -t python .

 

In the above command:

  • –pull” option is used to force to pull the base image before building the new image.
  • -t” is utilized to specify the tag of the image or name of the image:

However, users can also utilize the “–no-cache” option along with the “–pull” command to avoid the cache of the previous image and freshly create the new image from the latest base image:

> docker build --no-cache --pull --tag new-image .

 

Step 4: Execute Docker Image

Lastly, execute the newly generated image to execute and containerize the program:

> docker run new-image

 

We have illustrated how to use “–pull” to force to pull the base image before the new build.

Conclusion

The “–pull” option in the “docker build” command is used to pull the base image before a new build. In order to create a new docker image by pulling the based image mentioned in Dockerfile, utilize the “docker build –pull <image-name> .” command. However, users can add the “–no-cache” option to avoid the build layer of the previous image and rebuild the image from the start. This write-up has demonstrated how to use “–pull” to force to pull the base image before a new build.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.