为什么我不能在 Dockerfile 中的 FROM 之后再次使用构建参数? [英] Why can't I use the build arg again after FROM in a Dockerfile?

查看:20
本文介绍了为什么我不能在 Dockerfile 中的 FROM 之后再次使用构建参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Docker 18.05.0~ce~3-0~ubuntu,我想将构建参数传递给 FROM 以及我的 Dockerfile 中的其他行.你会期望下面的工作:

ARG FROM_IMAGE=ubuntu:bionic来自 $FROM_IMAGE复制sources_list/$FROM_IMAGE/etc/apt/sources.list

它适用于第二行 (FROM),但它的行为就像在 COPY 行中未设置:

<块引用>

第 1/3 步:ARG FROM_IMAGE=ubuntu:bionic第 2/3 步:来自 $FROM_IMAGE---> 8626492fecd3[...]步骤 3/3:复制 sources_list/${SOURCES_LIST_FILE}/etc/apt/sources.list复制文件失败:复制目录失败:mkdir/var/lib/docker/overlay2/0536b4e280ddca2fec18db9d79fa625a8be86efdbaaea5b3dbefcdaaab3f669/merged/etc/apt/sources.list:不是目录

如果添加另一个单独的构建参数,它适用于同一 COPY 行:

ARG FROM_IMAGE=ubuntu:bionic来自 $FROM_IMAGEARG SOURCES_LIST_FILE复制sources_list/${SOURCES_LIST_FILE}/etc/apt/sources.list

<块引用>

第 4/4 步:复制 sources_list/${SOURCES_LIST_FILE}/etc/apt/sources.list---> 7f974fffe929

为什么我不能在 FROM 行前后两次使用 FROM_IMAGE 构建参数?我找不到任何记录在案的此类限制.

解决方案

为什么我不能在 FROM 行前后使用 FROM_IMAGE 构建参数两次?

根据您将与 FROM 行相关的 ARG 放在哪里,存在真正的差异:

  • 第一个 FROM 之前的任何 ARG 都可以在任何 FROM 行中使用
  • 构建阶段中的任何 ARG(在 FROM 之后)都可以在该构建阶段中使用

这与构建阶段机制有关,可以在此处找到一些实际行为的参考:https://github.com/docker/cli/pull/333,关于为什么文档和构建机制对 ARG 的使用有点混乱的讨论在这里:https://github.com/moby/moby/issues/34129

I'm using Docker 18.05.0~ce~3-0~ubuntu and I'd like to pass a build argument to the FROM as well as other lines in my Dockerfile. You would expect the below to work:

ARG FROM_IMAGE=ubuntu:bionic
FROM $FROM_IMAGE

COPY sources_list/$FROM_IMAGE /etc/apt/sources.list

It works for the second line (FROM), but it behaves like it is unset in the COPY line:

Step 1/3 : ARG FROM_IMAGE=ubuntu:bionic Step 2/3 : FROM $FROM_IMAGE ---> 8626492fecd3 [...] Step 3/3 : COPY sources_list/${SOURCES_LIST_FILE} /etc/apt/sources.list failed to copy files: failed to copy directory: mkdir /var/lib/docker/overlay2/0536b4e280ddca2fec18db9d79fa625a8be86efdbaaea5b3dbbefcdaaab3f669/merged/etc/apt/sources.list: not a directory

If add another, separate build arg, it works for the same COPY line:

ARG FROM_IMAGE=ubuntu:bionic
FROM $FROM_IMAGE

ARG SOURCES_LIST_FILE
COPY sources_list/${SOURCES_LIST_FILE} /etc/apt/sources.list

Step 4/4 : COPY sources_list/${SOURCES_LIST_FILE} /etc/apt/sources.list ---> 7f974fffe929

Why can't I use the FROM_IMAGE build arg twice, on and after a FROM line? I fail to find any documented restriction of this sort.

解决方案

Why can't I use the FROM_IMAGE build arg twice, on and after a FROM line?

There is a real difference depending on where you put ARG related to FROM line:

  • any ARG before the first FROM can be used in any FROM line
  • any ARG within a build stage (after a FROM) can be used in that build stage

This is related to build stages mechanics and some reference of actual behavior can be found here: https://github.com/docker/cli/pull/333, and a discussion on why documentation and build mechanics is a bit confusing on ARG usage is here: https://github.com/moby/moby/issues/34129

这篇关于为什么我不能在 Dockerfile 中的 FROM 之后再次使用构建参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆