Docker覆盖ENTRYPOINT但保留CMD

vom3gejh  于 2023-08-03  发布在  Docker
关注(0)|答案(2)|浏览(133)

我需要扩展一个基础镜像(jwilder/nginx-proxy),因为我需要向该镜像添加一些额外的配置。
这些配置更改中的一些需要在容器的运行时期间完成,在基础映像的逻辑开始之前。
我实现运行时配置的方法是创建一个自定义ENTRYPOINT文件,该文件首先执行所有自定义工作,然后启动基础映像的入口点。
我的入口点看起来像这样:

#!/bin/bash

# my custom logic here

echo "Executed custom logic."

# default jwilder/nginx entrypoint
/app/docker-entrypoint.sh "$@"

字符串
目前,这不起作用,因为“$@”解析为空。我的自定义Dockerfile没有设置自定义CMD,只有ENTRYPOINT。
有没有什么原因导致这不起作用?我希望基本图像的CMD保持设置并传递到我的ENTRYPOINT。否则,我需要复制基础映像的CMD,如果CMD发生更改,则很容易出错。

d6kp6zgx

d6kp6zgx1#

从文件上看
如果CMD是从基础映像定义的,则设置ENTRYPOINT会将CMD重置为空值。在这种情况下,必须在当前映像中定义CMD才能有值。
CMD也是如此。CMD和ENTRYPOINT将重置基础图像中的任何CMD或ENTRYPOINT值。
你得自己重新设置。
如果你担心这个值会改变--而基础图像是在你的控制之下--你可以设置一个环境变量并使用它。

yc0p9oo0

yc0p9oo02#

假设从基地的CMD将被删除,如在建设者参考。CMD可以与新的ENTRYPOINT沿着包含在Dockerfile中。
https://hub.docker.com/layers/jwilder/nginx-proxy/latest/images/sha256-876a1df7bf88bb801cef813b276e4ee8c1861fca4c47cfaaeb2e7d3a087c4360?context=explore表示CMD将是CMD ["forego" "start" "-r"]
Dockerfile示例:

FROM jwilder/nginx-proxy:latest

COPY /new-entrypoint.sh /app

ENTRYPOINT ["/app/new-entrypoint.sh"]
CMD ["forego", "start", "-r"]

字符串
应产生:

docker build -t test -f Dockerfile .
docker run -v /var/run/docker.sock:/tmp/docker.sock:ro test | head
Executed custom logic.
Info: running nginx-proxy version 1.3.1-34-gc430825
Setting up DH Parameters..
Warning: TRUST_DOWNSTREAM_PROXY is not set; defaulting to "true". For security, you should explicitly set TRUST_DOWNSTREAM_PROXY to "false" if there is not a trusted reverse proxy in front of this proxy.
Warning: The default value of TRUST_DOWNSTREAM_PROXY might change to "false" in a future version of nginx-proxy. If you require TRUST_DOWNSTREAM_PROXY to be enabled, explicitly set it to "true".
forego      | starting dockergen.1 on port 5000
forego      | starting nginx.1 on port 5100
nginx.1     | 2023/07/20 23:50:39 [notice] 21#21: using the "epoll" event method
nginx.1     | 2023/07/20 23:50:39 [warn] 21#21: 10240 worker_connections exceed open file resource limit: 1024
nginx.1     | nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx.1     | 2023/07/20 23:50:39 [notice] 21#21: nginx/1.25.1
nginx.1     | 2023/07/20 23:50:39 [notice] 21#21: built by gcc 12.2.0 (Debian 12.2.0-14)

的数据
此外,根据https://docs.docker.com/engine/reference/commandline/run/,我们可以使用--entrypointdocker run命令来覆盖Dockerfile中的默认ENTRYPOINT,然后在docker run命令的末尾提供CMD
示例:Dockerfile

FROM jwilder/nginx-proxy:latest

COPY /new-entrypoint.sh /app


应产生:

docker run --entrypoint /app/new-entrypoint.sh -v /var/run/docker.sock:/tmp/docker.sock:ro test forego start -r | head
Executed custom logic.
Info: running nginx-proxy version 1.3.1-34-gc430825
Setting up DH Parameters..
Warning: TRUST_DOWNSTREAM_PROXY is not set; defaulting to "true". For security, you should explicitly set TRUST_DOWNSTREAM_PROXY to "false" if there is not a trusted reverse proxy in front of this proxy.
Warning: The default value of TRUST_DOWNSTREAM_PROXY might change to "false" in a future version of nginx-proxy. If you require TRUST_DOWNSTREAM_PROXY to be enabled, explicitly set it to "true".
forego      | starting dockergen.1 on port 5000
forego      | starting nginx.1 on port 5100
nginx.1     | 2023/07/20 23:53:55 [notice] 22#22: using the "epoll" event method
nginx.1     | 2023/07/20 23:53:55 [warn] 22#22: 10240 worker_connections exceed open file resource limit: 1024
nginx.1     | nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx.1     | 2023/07/20 23:53:55 [notice] 22#22: nginx/1.25.1
nginx.1     | 2023/07/20 23:53:55 [notice] 22#22: built by gcc 12.2.0 (Debian 12.2.0-14)

相关问题