Witold Krak
Witold Krak

Reputation: 13

Dockerfile --build-arg argument passed for WORKDIR during image creation doesn't work

I would like to build an image and copy the source of the python flask app, to some custom folder defined by ARG named APP. I used WORDIR syntax but it doesn't work and I feel that I need some "review" and help...

I've tried also with syntax ${app} but it doesn't help too.

Dockerfile:

FROM python:3.11.1
ARG APP
WORKDIR $app # also treied with ${app}
COPY ./app/* $APP/
RUN pip3 install -r $APP/requirements.txt
EXPOSE 5000
CMD /bin/bash
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

To trigger docker build I'm using script:

#/bin/bash
docker build \
--no-cache \
--build-arg APP=flask_base \
--network host \
-t $1 \
.
$ ./build_image.sh project_flask_base_test

output:

The build succeded but during build creation, I can see that all sources are copied to the root folder:Sending build context to Docker daemon  90.11kB
Step 1/8 : FROM python:3.11.1
 ---> 539eccd5ee4e
Step 2/8 : ARG APP
 ---> Running in cc77b3a6ef3b
Removing intermediate container cc77b3a6ef3b
 ---> 75c8591fcdaa
Step 3/8 : WORKDIR $APP
 ---> Running in 275c4dcdf379
Removing intermediate container 275c4dcdf379
 ---> c6c5e85607ca
Step 4/8 : COPY ./app/* $APP/
 ---> 0c002d958343
Step 5/8 : RUN pip3 install -r $APP/requirements.txt
 ---> Running in aabe43391e7b
Collecting flask==2.2.3

I've read docker doc: https://docs.docker.com/engine/reference/builder/#arg https://docs.docker.com/engine/reference/builder/#workdir

but w/o success

Upvotes: 0

Views: 617

Answers (1)

Paul Hodges
Paul Hodges

Reputation: 15418

These are case sensitive. $app is NOT the same as $APP.

ARG APP
WORKDIR $app # also treied with ${app}
COPY ./app/* $APP/

This is assigning $APP, then trying to use $app which which should be still unset. (Is it a Windows env?)

Pretty sure it should be

WORKDIR $APP 

On the other hand, your output shows

Step 3/8 : WORKDIR $APP

Are you sure these are from the same run?

TESTING

Note that ARG is no different from arg, as directives are NOT case sensitive -

$: cat Dockerfile
FROM busybox
ARG tst=foo
arg FOO=bar
RUN echo "tst=[$tst/$TST] foo=[$foo/$FOO]" >/tmp/tst
CMD cat /tmp/tst

and docker reports both as ARG in uppercase, but preserves the case of tst and FOO, as those are relevant.

$: docker build -t foo .
. . .
Step 2/5 : ARG tst=foo
. . .
Step 3/5 : ARG FOO=bar
. . .
Step 4/5 : RUN echo "tst=[$tst/$TST] foo=[$foo/$FOO]" >/tmp/tst
. . .
Step 5/5 : CMD cat /tmp/tst
. . .
Successfully built cd67b7a87cba

and when using both in the output, only the correct case version creates any output.

$: docker run --rm foo
tst=[foo/] foo=[/bar]

Upvotes: 0

Related Questions