简体   繁体   中英

Can I reference an environment variable in a Dockerfile FROM statement?

What I'd like to do is this:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine
# more stuff

But, this needs to run on an isolated clean build machine which does not have internet access, so I need to route it through a local mirror server (eg Artifactory or Nexus or another similar thing)

If the docker image were hosted on docker hub (eg FROM ubuntu) then the --registry-mirrors docker configuration option would solve it for us.
BUT because microsoft have decided not to use docker hub, the registry mirror thing isn't working either.

After much effort, I've set up aa custom domain mirror for mcr.microsoft.com and so now I can do this:

FROM microsoft-docker-mirror.local.domain/dotnet/core/aspnet:3.0-alpine
# more stuff

That works. But we have remote workers who may not be on the local office LAN and can't see my local mirror server. What I now want to do is vary it depending on environment. Eg

ENV MICROSOFT_DOCKER_REPO mcr.microsoft.com
FROM ${MICROSOFT_DOCKER_REPO}/dotnet/core/aspnet:3.0-alpine

My isolated build machine would set the MICROSOFT_DOCKER_REPO environment variable, and everyone else's machines would use the default mcr.microsoft.com

Anyway. Adding the ENV line to the dockerfile results in docker throwing this error:

Error response from daemon: No build stage in current context

There seem to be lots of references to the fact that the FROM line must be the first line in the file, before even any comments... How can I reference an environment variable in my FROM statement? Or alternatively, how can I make registry mirrors work for things that aren't docker hub?

Thanks!

" ARG is the only instruction that may precede FROM in the Dockerfile " - Dockerfile reference .

ARG MICROSOFT_DOCKER_REPO=mcr.microsoft.com
FROM ${MICROSOFT_DOCKER_REPO}/dotnet/core/aspnet:3.0-alpine

Build with a non-default MICROSOFT_DOCKER_REPO using the --build-arg : docker build --rm --build-arg MICROSOFT_DOCKER_REPO=repo.example.com -t so:58196638.

Note : you can pass the --build-arg from the hosts environment ie MICROSOFT_DOCKER_REPO=repo.example.com docker build --rm --build-arg MICROSOFT_DOCKER_REPO=${MICROSOFT_DOCKER_REPO} -t so:58196638.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM