moby / buildkit

concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit
https://github.com/moby/moby/issues/34227
Apache License 2.0
8.02k stars 1.12k forks source link

BUG: Docker ARG command fails to perform argument substitution in recent buildkit versions #5178

Open S-Fichtl opened 1 month ago

S-Fichtl commented 1 month ago

Intro to the Bug

I am using a single Dockerfile to build images for different Ubuntu versions. Those images are then used in CI to build software targeting those different Ubuntu versions. Sometimes, there are of course some differences between Ubuntu versions, e.g. I'm installing different compilers. The code I use looks something like this:

FROM $baseimage AS base_image
ARG distro
ARG ${distro}=" "
...
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${focal:+10}${jammy:+14} 10

This used to work just fine but it doesn't seem to work anymore. -> BUG


Reproducing the Bug

Here's a minimal image to reproduce with full Dockerfile and build commands Dockerfile:

ARG baseimage
ARG distro
ARG ${distro}=" "

FROM $baseimage AS base_image
ARG distro
ARG ${distro}=" "

RUN echo "arg is ${distro}" > /test.log
RUN echo "distro is ${focal:+Focal}${jammy:+Jammy} ${focal:-not Focal}${jammy:-not Jammy}" >> /test.log

CMD sleep infinity

build commands:

baseimage='ubuntu:focal'
distro='focal'
docker build . -t testimage:${distro} --build-arg distro=${distro} --build-arg baseimage=${baseimage} -f Dockerfile --target base_image

In this case, when using focal as test argument, I'd expect the log file contents to be:

arg is focal
distro is Focal not Jammy

However, instead it's:

arg is focal
distro is  not Focalnot Jammy

I don't know what exactly changed to make it stop working, but I assume a recent docker upgrade (I'm on Ubuntu 22.04 here for building those images). But this used to work only a few months ago, and now it doesn't anymore.

With some help on StackOverflow I got it to work by setting DOCKER_BUILDKIT=0. So with this flag set, the above example Dockerfile and buildscripts behave correctly again.

tonistiigi commented 1 month ago

Second report of #5156