4

I am using this Dockerfile

ARG IMAGE_ONE
FROM ${IMAGE_ONE}
RUN cat /etc/debian_version

ARG IMAGE_TWO
FROM ${IMAGE_TWO}
RUN cat /etc/debian_version

But it fails because it does not use the second var IMAGE_TWO:

$ docker build --no-cache --build-arg IMAGE_ONE=debian:7 --build-arg IMAGE_TWO=debian:8 .
Sending build context to Docker daemon  2.048kB
Step 1/6 : ARG IMAGE_ONE
Step 2/6 : FROM ${IMAGE_ONE}
 ---> 90c038768099
Step 3/6 : RUN cat /etc/debian_version
 ---> Running in f842d9cf4f17
7.11
Removing intermediate container f842d9cf4f17
 ---> 0f7f7afdd8a6
Step 4/6 : ARG IMAGE_TWO
 ---> Running in ed3d36f2f9cb
Removing intermediate container ed3d36f2f9cb
 ---> ae4ae3cabc02
Step 5/6 : FROM ${IMAGE_TWO}
 --->
Step 6/6 : RUN cat /etc/debian_version
 ---> Running in 6f1c165e2765
OCI runtime create failed: container_linux.go:296:
    starting container process caused "exec: \"/bin/sh\":
    stat /bin/sh: no such file or directory": unknown

Docker version:

$ docker --version
Docker version 17.12.0-ce, build c97c6d6

Is there something wrong in my Dockerfile or is the docker build command wrong?

segfault
  • 41
  • 1
  • 4

1 Answers1

22

The reason is because IMAGE_TWO is not in the same scope check this https://docs.docker.com/engine/reference/builder/#scope

Basically the ARG IMAGE_TWO is still part of the first stage and goes out of scope when that stage ends and will not be part of the second stage.

Declaring the arguments at the beginning allow the IMAGE_TWO to be in the second stage.

ARG IMAGE_ONE
ARG IMAGE_TWO
FROM ${IMAGE_ONE}
RUN cat /etc/debian_version

FROM ${IMAGE_TWO}
RUN cat /etc/debian_version

docker build --build-arg=IMAGE_ONE=debian:7 --build-arg=IMAGE_TWO=debian:8 .

jhernandez
  • 847
  • 5
  • 9
  • 13
    Please mind that ARG values defined **above** the first FROM directive such as `IMAGE_ONE` or `IMAGE_TWO` are only available to the FROM instructions in the multi-stage build and (against all intuition) **not** available in the "bodies" of the build stages. See [this discussion](https://github.com/moby/moby/issues/34129). – Felix K. May 18 '18 at 08:39