Pull docker images from a private repository during docker build?

The docker folks generally want to ensure that if you run docker pull foo/bar you'll get the same thing (i.e., the foo/bar image from Docker Hub) regardless of your local environment.

This means that there are no options available to have Docker use anything else without an explicit hostname/port.


I was facing the same issue in 2019. I solved this using arguments (ARG).
https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
Arguments allow you to set optional parameters (with defaults) that can be used in your FROM line.

Dockerfile-project-dev

ARG REPO_LOCATION=privaterepo.company.net/
ARG BASE_VERSION=latest
FROM ${REPO_LOCATION}project/base:${BASE_VERSION}
...

For my use-case I normally want to pull from the private repo, but if I'm working on the Dockerfiles I may want to be able to build from an image on my own machine, without having to modify the FROM line in my Dockerfile. To tell Docker to search my local machine for the image at build time I would do this:

docker build -t project/dev:latest -f ./Dockerfile-project-dev --build-arg REPO_LOCATION='' .

The image name should include the FQDN of the registry host. So if you want to FROM <some private image> you must specifiy it as FROM registry_host:5000/foo/bar

In the future this won't be a requirement, but unfortunately for now it is.