How to pass ARG value to ENTRYPOINT?
Like Blake Mitchell sais, you cannot use ARG
in ENTRYPOINT
. However you can use your ARG
as a value for ENV
, that way you can use it with ENTRYPOINT
:
Dockerfile
ARG my_arg
ENV my_env_var=$my_arg
ENTRYPOINT echo $my_env_var
and run:
docker build --build-arg "my_arg=foo" ...
Short answer: you need to use ENV
Both ARG
and ENV
are not expanded in ENTRYPOINT
or CMD
. (https://docs.docker.com/engine/reference/builder/#environment-replacement) However, because ENV
s are passed in as part of the environment, they are available at run time, so the shell can expand them. (This means you can't use the array form of ENTRYPOINT
or CMD
.)
Here is an example:
$ cat arg/Dockerfile
FROM debian:jessie
ARG FOO=bar
ENTRYPOINT echo ${FOO:-foo}
$ sudo docker build arg
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:jessie
---> f50f9524513f
Step 2 : ARG FOO=bar
---> Using cache
---> 2cfdcb514b62
Step 3 : ENTRYPOINT echo ${FOO:-foo}
---> Running in 21fb9b42c10d
---> 75e5018bad83
Removing intermediate container 21fb9b42c10d
Successfully built 75e5018bad83
$ sudo docker run 75e5018bad83
foo
$ sudo docker run -e FOO=bas 75e5018bad83
bas
$ sudo docker build --build-arg FOO=bas arg
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:jessie
---> f50f9524513f
Step 2 : ARG FOO=bar
---> Using cache
---> 2cfdcb514b62
Step 3 : ENTRYPOINT echo ${FOO:-foo}
---> Using cache
---> 75e5018bad83
Successfully built 75e5018bad83
$ sudo docker run 75e5018bad83
foo
$ cat env/Dockerfile
FROM debian:jessie
ARG FOO=bar
ENV FOO=${FOO}
ENTRYPOINT echo ${FOO:-foo}
$ sudo docker build env
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:jessie
---> f50f9524513f
Step 2 : ARG FOO=bar
---> Using cache
---> 2cfdcb514b62
Step 3 : ENV FOO ${FOO}
---> Running in f157a07c1b3e
---> a5e8c5b65a17
Removing intermediate container f157a07c1b3e
Step 4 : ENTRYPOINT echo ${FOO:-foo}
---> Running in 66e9800ef403
---> 249fe326e9ce
Removing intermediate container 66e9800ef403
Successfully built 249fe326e9ce
$ sudo docker run 249fe326e9ce
bar
$ sudo docker run -e FOO=bas 249fe326e9ce
bas
$ sudo docker build --build-arg FOO=bas env
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:jessie
---> f50f9524513f
Step 2 : ARG FOO=bar
---> Using cache
---> 2cfdcb514b62
Step 3 : ENV FOO ${FOO}
---> Running in 6baf31684b9f
---> 8f77ad154798
Removing intermediate container 6baf31684b9f
Step 4 : ENTRYPOINT echo ${FOO:-foo}
---> Running in 892ac47cabed
---> fa97da85bf8a
Removing intermediate container 892ac47cabed
Successfully built fa97da85bf8a
$ sudo docker run fa97da85bf8a
bas
$
I'm struggling with this for a day, thanks for @Rotareti for mentioning. It needs to be in ENV
before it can be used to the ENTRYPOINT
.
ENV variable=$from_ARG_variable
ENTRYPOINT exec your_exec_sh_file $variable
Hope this helps.