Why can't I use the build arg again after FROM in a Dockerfile?

gertvdijk

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.

Const

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dockerfile build ARG in COPY --from=

Use --build-arg value in Dockerfile FROM parameter

Why can't i subscribe again on that same EventEmitter after unsubscribe

Can't use ARG in docker multistage build

Why can't I use the same variable again in Django?

Use an argument from docker-compose build --build-arg inside a Dockerfile

Why can't i refresh the build after init state

Why after that I close a PrimeNG modal defined into a child component I can't open it again?

Why can't I set the content again after I have fresh the web page in Vaadin?

Why can't I find the Dockerfile?

Why can't I run command in Dockerfile but I can from within the my Docker container?

After pasting a yanked line in Vim, why can't I paste it again?

Why can't I build for ios from windows?

Why I can't use the name of the derived table(subquery result) again?

Why can't I use context.read in build(), but I can use Provider.of with listen: false?

Why I can't use vim after exit?

Why can't I use ToolButtons after assigning them an Action?

Why can't I use hPutStr after printing the result of hGetContents?

I can't build my app node with Dockerfile

Why I can't find out build folder after runing npm run build on a nextjs project?

Can't build dockerfile with Pulumi

Can't build Golang in dockerfile

Struct in interface again; Why can I use it like class?

Why I can't open font dialog again?

Why can't I use from the static method of the implemented interface?

Why i can't use Open() from XlsIO on xamarin?

Why can't I use FindElementByXPath on this table from a website?

Why can't I use this text from zenity to find files?

Select ENV/ARG in Dockerfile depending on build ARG