简体   繁体   English

我可以在 Dockerfile FROM 语句中引用环境变量吗?

[英]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)但是,这需要在没有互联网访问权限的隔离干净构建机器上运行,所以我需要通过本地镜像服务器(例如 Artifactory 或 Nexus 或其他类似的东西)路由它

If the docker image were hosted on docker hub (eg FROM ubuntu) then the --registry-mirrors docker configuration option would solve it for us.如果 docker 映像托管在 docker 集线器上(例如来自 ubuntu),那么--registry-mirrors docker配置选项将为我们解决它。
BUT because microsoft have decided not to use docker hub, the registry mirror thing isn't working either.但是因为微软决定不使用 docker 集线器,所以注册表镜像也不起作用。

After much effort, I've set up aa custom domain mirror for mcr.microsoft.com and so now I can do this:经过一番努力,我为 mcr.microsoft.com 设置了一个自定义域镜像,所以现在我可以这样做了:

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.但是我们有远程工作人员,他们可能不在本地办公室 LAN 上,并且看不到我的本地镜像服务器。 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我的独立构建机器将设置MICROSOFT_DOCKER_REPO环境变量,其他人的机器将使用默认的mcr.microsoft.com

Anyway.反正。 Adding the ENV line to the dockerfile results in docker throwing this error:ENV行添加到 dockerfile 会导致 docker 引发此错误:

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?似乎有很多引用表明 FROM 行必须是文件中的第一行,甚至在任何注释之前...如何在 FROM 语句中引用环境变量? Or alternatively, how can I make registry mirrors work for things that aren't docker hub?或者,我怎样才能使注册表镜像适用于不是 docker 集线器的东西?

Thanks!谢谢!

" ARG is the only instruction that may precede FROM in the Dockerfile " - Dockerfile reference . ARGDockerfile中唯一可能在FROM之前的指令” -Dockerfile 参考

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.使用--build-arg使用非默认MICROSOFT_DOCKER_REPO构建: 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.注意:您可以从主机环境中传递--build-argMICROSOFT_DOCKER_REPO=repo.example.com docker build --rm --build-arg MICROSOFT_DOCKER_REPO=${MICROSOFT_DOCKER_REPO} -t so:58196638.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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