Distribution: build-arg not set during build

Created on 3 Dec 2017  Â·  3Comments  Â·  Source: distribution/distribution

Take this simple dockerfile:

ARG IMAGE
ARG FILES
FROM ${IMAGE}
RUN echo ${IMAGE} > /usr/local/src/image
RUN echo ${FILES} > /usr/local/src/files

I run a docker build as follows:

$ docker build . --tag test:latest --file dockerfile --build-arg IMAGE=ubuntu:latest --build-arg FILES=files

When I spin up a container with this image, the two files created are empty:

$ docker run -d -t -p 8081:8081 test /bin/bash
$ docker exec -it focused_mestorf bash
root@45189afac13a:/# cd /usr/local/src/
root@45189afac13a:/usr/local/src# ls
files  image
root@45189afac13a:/usr/local/src# cat *


root@45189afac13a:/usr/local/src# 

This used to work in the past (docker.io). Has anything changed about build-arg handling?

Most helpful comment

In a Dockerfile, FROM starts a new build-stage, and any property from the previous stage (if any) is reset. In your example, the Dockerfile should likely be like this:

ARG IMAGE
FROM ${IMAGE}
ARG IMAGE
ARG FILES
RUN echo ${IMAGE} > /usr/local/src/image
RUN echo ${FILES} > /usr/local/src/files

In other words: to be able to use the IMAGE and/or FILES build-arg, they have to be defined within the stage (so _after_ the FROM).

There have been some bug fixes in recent Docker releases related to properties from previous stages not being properly reset, so possibly this previously worked due to a bug that has now been fixed; you mentioned that it worked in the docker.io packages; not that those are not the official Docker packages, so it's possible there's still a bug in those.

Also, be aware that this repository (and issue tracker) is for the open source Docker _registry_, not Docker itself; if you discover a bug in Docker, it's best to report this in the Docker issue tracker, located at https://github.com/docker/for-linux/issues.

I'm closing this issue, because this doesn't look like a bug, but feel free to continue the conversation

All 3 comments

I am sorry I neglected to state my operating environment: Ubuntu 17.04, 64-bit, Intel® Core™ i7-7700HQ CPU @ 2.80GHz × 8, 15.5 GB.

In a Dockerfile, FROM starts a new build-stage, and any property from the previous stage (if any) is reset. In your example, the Dockerfile should likely be like this:

ARG IMAGE
FROM ${IMAGE}
ARG IMAGE
ARG FILES
RUN echo ${IMAGE} > /usr/local/src/image
RUN echo ${FILES} > /usr/local/src/files

In other words: to be able to use the IMAGE and/or FILES build-arg, they have to be defined within the stage (so _after_ the FROM).

There have been some bug fixes in recent Docker releases related to properties from previous stages not being properly reset, so possibly this previously worked due to a bug that has now been fixed; you mentioned that it worked in the docker.io packages; not that those are not the official Docker packages, so it's possible there's still a bug in those.

Also, be aware that this repository (and issue tracker) is for the open source Docker _registry_, not Docker itself; if you discover a bug in Docker, it's best to report this in the Docker issue tracker, located at https://github.com/docker/for-linux/issues.

I'm closing this issue, because this doesn't look like a bug, but feel free to continue the conversation

Thanks very much for your guidance. Works just like you said.

I'll check whether this is described anywhere in the manuals.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

afiskon picture afiskon  Â·  4Comments

jascodes picture jascodes  Â·  5Comments

juanjoDiaz picture juanjoDiaz  Â·  4Comments

xflash picture xflash  Â·  3Comments

luke-park picture luke-park  Â·  5Comments