Pull docker images from a private repository during docker build?

43.9k views Asked by At

Is there any way of pulling images from a private registry during a docker build instead of docker hub?

I deployed a private registry and I would like to be able to avoid naming its specific ip:port in the Dockerfile's FROM instruction. I was expecting a docker build option or a docker environment variable to change the default registry.

3

There are 3 answers

0
Matt L On BEST ANSWER

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='' .
3
cpuguy83 On

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.

3
larsks On

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.