How to pass ARG value to ENTRYPOINT?

2019-04-18 02:29发布

Docker 1.9 allows to pass arguments to a dockerfile. See link: https://docs.docker.com/engine/reference/builder/#arg

How can i pass the same arugments within ENTRYPOINT Instruction??

My dockerfile has

ARG $Version=3.1
ENTRYPOINT /tmp/folder-$Version/sample.sh start

I am getting an error while creating container with above dockerfile. Please suggest what is the correct way to specify the argument within ENTRYPOINT instruction??

3条回答
forever°为你锁心
2楼-- · 2019-04-18 03:06

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=$my_arg

ENTRYPOINT echo $my_env
查看更多
老娘就宠你
3楼-- · 2019-04-18 03:06

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 ENVs 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
$
查看更多
Deceive 欺骗
4楼-- · 2019-04-18 03:15

The syntax for ARG should omit the $.

Instead of ARG $Version=3.1, try ARG Version=3.1.

查看更多
登录 后发表回答