srigu
srigu

Reputation: 505

How is Docker Pull command different from Docker Build command

The official documentation from Docker, says:

Docker build - "Build an image from a Dockerfile"

Docker pull - "Pull an image or a repository from a registry"

Following is a sample Dockerfile:

FROM php:7.1.8-apache

MAINTAINER XXXX

COPY . /srv/app
COPY docker/vhosts.conf /etc/apache2/sites-available/000-default.conf

WORKDIR /srv/app

RUN docker-php-ext-install mbstring pdo pdo_mysql \
    && chown -R www-data:www-data /srv/app

It appears that build command will first download the image from the Docker hub and then do the other stuff as mentioned in the docker file. Whereas Docker pull will just download the image from the repository. In a way, 'pull' is part of the 'build'. I am new to Docker , I need confirmation for my understanding or let me knpw if there is more to it.

Upvotes: 14

Views: 9963

Answers (2)

David Maze
David Maze

Reputation: 158812

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.

A couple of cases will also automatically docker pull for you. If you docker run an image you don't have, it will be pulled; a Dockerfile FROM line will also pull a base image if it's not present.

In your case the Dockerfile is describing how to build a new image, starting from the php:7.1.8-apache image. You need that base image to build your custom image, and if you don't have it already it will be pulled for you. But what you get out is a different image from that PHP base image, and unless you've docker pushed the image somewhere you won't be able to directly docker pull the result.

Upvotes: 8

Mikael Kjær
Mikael Kjær

Reputation: 700

Your understanding is correct when building from another image, with a few exceptions:

  1. If you build from scratch no image will be pulled. More information here.
  2. If the image is already on your computer (from a previous pull, build)

Upvotes: 8

Related Questions