buildx icon indicating copy to clipboard operation
buildx copied to clipboard

automatically substitute system environment variables in Dockerfile during the build

Open ViliusS opened this issue 1 year ago • 3 comments

I'm not sure if this will be a bug report or feature request but at least on Windows I cannot get automatic environment variable substitution to work in a Dockerfile which is executed by docker buildx build . or docker build . commands.

I tried multiple configurations:

ARG NPM_AUTH_TOKEN
ENV NPM_AUTH_TOKEN=${NPM_AUTH_TOKEN}
ARG NPM_AUTH_TOKEN=${NPM_AUTH_TOKEN}
ENV NPM_AUTH_TOKEN=${NPM_AUTH_TOKEN}
ARG NPM_AUTH_TOKEN=${NPM_AUTH_TOKEN}

The only thing to be working is if I add docker-compose.yaml file with contents:

services:
  mydefaultbuild:
    build:
      args:
        NPM_AUTH_TOKEN: ${NPM_AUTH_TOKEN}

Then if I run docker buildx bake substitution starts to work.

Is this a bug? If not, it would be great if environment variable substitution would work out of the box without needing to remap variables in compose file.

ViliusS avatar Jul 27 '22 20:07 ViliusS

build command does not pass your client side environment variables to the dockerfile. They need to be passed with --build-arg https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg

tonistiigi avatar Jul 27 '22 22:07 tonistiigi

I know that it doesn't pass them. My question was different, is this a bug or this issue can be converted to feature request so that env. variables will be passed automatically?

ViliusS avatar Jul 28 '22 05:07 ViliusS

add .env file

cat .env
NPM_AUTH_TOKEN=<yourToken>
docker-compose -f docker-compose.yaml up

wojiushixiaobai avatar Aug 17 '22 01:08 wojiushixiaobai