如何将ARG值传递给ENTRYPOINT?

时间:2015-12-16 23:43:52

标签: docker dockerfile docker-compose docker-registry

Docker 1.9允许将参数传递给dockerfile。请参见链接:https://docs.docker.com/engine/reference/builder/#arg

如何在ENTRYPOINT指令中传递相同的内容?

我的dockerfile有

  

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

使用上面的dockerfile创建容器时出错。 请建议在ENTRYPOINT指令中指定参数的正确方法是什么?

5 个答案:

答案 0 :(得分:23)

Blake Mitchell sais一样,您无法在ARG中使用ENTRYPOINT。不过,您可以将ARG用作ENV的值,这样就可以将其与ENTRYPOINT一起使用:

<强> Dockerfile

ARG my_arg
ENV my_env=$my_arg

ENTRYPOINT echo $my_env

答案 1 :(得分:16)

简短回答:您需要使用ENV

ARGENV中的ENTRYPOINTCMD都未展开。 (https://docs.docker.com/engine/reference/builder/#environment-replacement)但是,因为ENV作为环境的一部分传入,所以它们在运行时可用,因此shell可以扩展它们。 (这意味着您无法使用ENTRYPOINTCMD的数组形式。)

以下是一个例子:

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

答案 2 :(得分:2)

ARG的语法应省略$

而不是ARG $Version=3.1,请尝试ARG Version=3.1

答案 3 :(得分:2)

我为此苦了一天,感谢@Rotareti的提及。必须先进入ENV,然后才能用于ENTRYPOINT

ENV variable=$from_ARG_variable
ENTRYPOINT exec your_exec_sh_file $variable

希望这会有所帮助。

答案 4 :(得分:0)

以防万一您有多个参数要传递给 ENTRYPOINT 或 CMD,您可以这样做:

FROM openjdk:8-jdk-alpine
ARG PROFILE
ENV PROFILE=${PROFILE}
...
CMD java -jar -Dspring.profiles.active=$(echo ${PROFILE}) /app/server.jar