2

I am trying to COPY a source file that is locally present to a destination path that is dynamically passed using the docker ARG command.

Example dockerfile is:

$ cat mydockerfile
FROM debian:latest
RUN apt update
ENV app_env='prod'
ARG src_app_dir
ARG dest_app_dir
RUN echo ${src_app_dir}
RUN echo ${dest_app_dir}
RUN mkdir /root/${dest_app_dir}
COPY ${src_app_dir}/file.txt /root/${dest_app_dir}/filenew.txt
WORKDIR /
CMD ["bash"]

I am trying to pass the build arg dest_app_dir="server_app_dir" and expecting the build process creates the container path /root/server_app_dir/

The source folder is already present on my local machine and where the docker-build context is present.

$ ls -d local_app_dir/
local_app_dir/
$ ls local_app_dir/
file.txt

But I am getting the following error for the destination path:

 $ docker image build --build-arg src_app_dir="local_app_dir" dest_app_dir="server_app_dir" --tag arg_env:1.0 --file mydockerfile
unable to prepare context: path "dest_app_dir=server_app_dir" not found

Does not it work that way or am I missing the correct concept/usage of Docker build ARG and COPY commands here?

I am using docker-desktop on Windows11.

 $ docker version
Client: Docker Engine - Community
 Cloud integration: v1.0.23
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:48:21 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop
 Engine:
  Version:          20.10.14
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       87a90dc
  Built:            Thu Mar 24 01:46:14 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.11
  GitCommit:        3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
vjwilson
  • 754
  • 2
  • 14
  • 30

1 Answers1

4

You need to specify the build-arg as many times as the arguments

docker image build --build-arg src_app_dir="local_app_dir" --build-arg dest_app_dir="server_app_dir" --tag arg_env:1.0 --file mydockerfile .

Example

enter image description here


EDIT: Forgot to add context. Thanks @BMitch

Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14
  • I am sorry, that does not help, I already tried it but it shows to check the help and manual. ""docker image build" requires exactly 1 argument." – vjwilson May 07 '22 at 15:47
  • @vjwilson This should not be the case. I definetely am able to use more than one build-arg. Also please check https://stackoverflow.com/questions/42297387/docker-build-with-build-arg-with-multiple-arguments. Maybe are you using an old docker version? – Tolis Gerodimos May 07 '22 at 15:57
  • I am using docker-desktop on windows11: Engine version 20.10.14 – vjwilson May 07 '22 at 16:02
  • Docker Desktop 4.7.1 (77678) is currently the newest version available – vjwilson May 07 '22 at 16:03
  • @vjwilson Updated the answer with an execution example. You didnt post anywhere in your answer that you were using docker in windows. – Tolis Gerodimos May 07 '22 at 16:08
  • 2
    Don't forget to include the context at the end (with a `.`) – BMitch May 07 '22 at 16:09
  • @TolisGerodimos does that matter, as docker works similar way, correct? I think the difference I see with your command is, you used "." at the end. – vjwilson May 07 '22 at 16:12
  • @TolisGerodimos --file mydockerfile still does not work. Can you check? – vjwilson May 07 '22 at 16:12
  • @vjwilson Updated the answer we forgot to add context, try again. BMitch was right – Tolis Gerodimos May 07 '22 at 16:14
  • Thanks, now it is an acceptable answer :) I marked the same. But do you guys, what makes the difference when using "." representing dockerfile default in the current location and why it was not working when I flagged it as --file mydockerfile. what made the difference there? Could you add the description in the answer by editing it?. – vjwilson May 07 '22 at 16:18
  • ok nvm, I figured out, you can still use a filename other than dockerfile, what matters is the context to use with it. Like this "--file mydockerfile ." – vjwilson May 07 '22 at 16:22