This code works prior to v2. This is the smallest example I could think of. I'm looking for a way to fix it through the docker-compose build command.
I know there is a lot of text in this question, so I tried to highlight the important parts.
Any ideas how to fix this or work around it? If no one has any idea why this is happening, I'll submit a bug report.
Edit to explain why this is not a duplicate of How to pass arguments to entrypoint in docker-compose.yml, which was asked over 5 years ago. This is not a syntax error or a question about runtime environment variables. This question is about the incorrect substitution of build time environment variables docker-compose v2.
Dockerfile
FROM nginx:1.15.2
ARG A
ARG B
RUN env
docker-compose.yml
version: "3.6"
services:
nginxtest:
image: upgrade_test:0.1
container_name: upgrade_test
build:
context: .
dockerfile: ./Dockerfile
args:
A: arg_a
B: arg_b
If I run the Dockerfile directly with docker the ARGS work as expected:
$ docker build --no-cache --build-arg A=arg_a --build-arg B=arg_b -t upgradetest .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM nginx:1.15.2
---> c82521676580
Step 2/4 : ARG A
---> Running in a0f28fe1354f
Removing intermediate container a0f28fe1354f
---> accca99c82d7
Step 3/4 : ARG B
---> Running in 797051b92fda
Removing intermediate container 797051b92fda
---> 172094fed9d5
Step 4/4 : RUN env
---> Running in 4e6dfae151f6
HOSTNAME=4e6dfae151f6
HOME=/root
A=arg_a <--------------------------------------------------------------------------------------- expected result
NGINX_VERSION=1.15.2-1~stretch
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NJS_VERSION=1.15.2.0.2.2-1~stretch
B=arg_b <--------------------------------------------------------------------------------------- expected result
PWD=/
Removing intermediate container 4e6dfae151f6
---> cb1f2f43bd32
Successfully built cb1f2f43bd32
Successfully tagged upgradetest:latest
Using the docker-compose command, things don't quite work:
$ docker-compose build --no-cache
Sending build context to Docker daemon 297B
Step 1/4 : FROM nginx:1.15.2
---> c82521676580
Step 2/4 : ARG A
---> Running in d0b419c42db3
---> 9e53cb90f9a4
Step 3/4 : ARG B
---> Running in f645fb8df1b4
---> 5d4bc5267c63
Step 4/4 : RUN env
---> Running in 7c0813b7a822
HOSTNAME=7c0813b7a822
HOME=/root
A=arg_b <-------------------------------------------------------------------------------------- expected arg_a
NGINX_VERSION=1.15.2-1~stretch
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NJS_VERSION=1.15.2.0.2.2-1~stretch
B=arg_b <-------------------------------------------------------------------------------------- expected result
PWD=/
---> a3b37c031916
Successfully built a3b37c031916
Successfully tagged upgrade_test:0.1
CodePudding user response:
There's a fix for this, and it only affects the classic builder on 2.1.0 which is why I was having a hard time reproducing it. Until that is merged, you can try going a version back, or switching over to buildkit (not sure if these steps work for v2 like this did with v1), or building outside of compose.