38

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.

Akshay barahate
  • 649
  • 8
  • 23
Julio Guerra
  • 5,523
  • 9
  • 51
  • 75

3 Answers3

25

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.

cpuguy83
  • 5,794
  • 4
  • 18
  • 24
  • 1
    You say `In the future this won't be a requirement`. Source? – starbeamrainbowlabs May 06 '20 at 00:38
  • 1
    @starbeamrainbowlabs this answer was originally give in 2015. I think it was "future" functionality, at that time. The documentation (at least) for the use of ARGs in FROM mentioned in my answer was added in 2017. https://github.com/docker/cli/commit/6e40868ade57395d8bec27a65adf528f983996b0 I didn't dig any farther than that. – Matt L Nov 05 '20 at 14:35
  • Yes unfortunately this "future" never happened. We always wanted to decouple where an image comes from from the actual image name, it just didn't happen. – cpuguy83 Feb 16 '23 at 02:23
25

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='' .
Matt L
  • 686
  • 9
  • 12
8

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    Is it documented somewhere? It makes impossible using a registry mirror without modifying every Dockerfiles. – Julio Guerra Jun 18 '15 at 18:43
  • 2
    I don't think it's explicitly documented, but it has come up in the issue tracker [here](https://github.com/docker/docker/issues/7203) and variously in the mailing list. – larsks Jun 18 '15 at 18:48
  • 1
    @JulioGuerra You can setup a registry in mirror mode and tell docker about the mirror. There is no need to change the image name to pull it from the mirror. – cpuguy83 Feb 16 '18 at 18:58