Search code examples
dockerdockerfiledocker-registrydocker-trusted-registry

Pull docker images from a private repository during docker build?


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.


Solution

  • 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='' .