Dockerfile中的ENTRYPOINT之后CMD无法运行 [英] CMD doesn't run after ENTRYPOINT in Dockerfile
问题描述
因此,我有一个执行此操作的docker文件:
ENV ENV $ {ENV}
ENV SERVICE_NAME $ {SERVICE_NAME}
USER app
ENV HOME = / home / app
COPY target / home / app / target
COPY entrypoint.sh / home / app
WORKDIR /主页/应用程序
ENTRYPOINT / usr / bin / chamber exec $ {ENV} _ $ {SERVICE_NAME} -r 1-./entrypoint.sh
CMD java -jar -Dspring.profiles.active = docker target / my.jar
因此ENTRYPOINT运行并从AWS Parameter存储中提取了一些秘密并填充了它们在entrypoint.sh shell中作为环境变量。然后entrypoint.sh会对它们执行一些操作,创建一些文件,并在最后一行执行 exec $ @。
我当时期望CMD运行但是只能看到运行 ExecStop = / usr / bin / docker stop app的systemd服务文件。
systemd服务文件执行以下操作来启动容器:
ExecStart = / usr / bin / docker run --name app --memory-reservation = 128m --memory = 512m- e ENV = dev -e SERVICE_NAME = app 1234567890.dkr.ecr.eu-west-2.amazonaws.com/app:latest
CMD发生了什么事?
所以您应该宁可使用 exec形式并编写如下内容:
…
ENTRYPOINT [ / usr / bin / chamber, exec, $ {ENV} _ $ {SERVICE_NAME},-r, 1,-, ./ entrypoint.sh]
CMD [ java- jar,-Dspring.profiles.active = docker, target / my.jar]
但是这不能按原样工作,因为 $ {ENV}
和 $ {SERVICE_NAME}
不会扩展(因为需要shell)。
因此,此处最简单,正确的解决方案是重构 entrypoint.sh
,或者如果您不想更改它,但仍然依赖于带有 exec形式的环境变量,则 ENTRYPOINT
,您可以改为:
…
RUN chmod a + x entrypoint1.sh
ENTRYPOINT [ ./entrypoint1.sh]
CMD [ java -jar, -Dspring.profiles.active = docker, target / my.jar ]
有文件
entrypoint1.sh
#!/ bin / bash
exec / usr / bin / chamber exec $ {ENV} _ $ {SERVICE_NAME} -r 1-./entrypoint.sh $ @
So I have a docker file which does this:
ENV ENV ${ENV}
ENV SERVICE_NAME ${SERVICE_NAME}
USER app
ENV HOME=/home/app
COPY target /home/app/target
COPY entrypoint.sh /home/app
WORKDIR /home/app
ENTRYPOINT /usr/bin/chamber exec ${ENV}_${SERVICE_NAME} -r 1 -- ./entrypoint.sh
CMD java -jar -Dspring.profiles.active=docker target/my.jar
So the ENTRYPOINT runs and pulls down some secrets from AWS Parameter store and populates them in the entrypoint.sh shell as environment variables. The entrypoint.sh then performs some actions with them, creates some files etc and in its last line does "exec $@".
I was then expecting the CMD to run but all it can see is the systemd service file running "ExecStop=/usr/bin/docker stop app".
The systemd service file does this to start the container:
ExecStart=/usr/bin/docker run --name app --memory-reservation=128m --memory=512m -e ENV=dev -e SERVICE_NAME=app 1234567890.dkr.ecr.eu-west-2.amazonaws.com/app:latest
What happened to CMD?
As documented in https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact, if you combine the "shell form" of CMD
and ENTRYPOINT
, the CMD
specification is omitted:
So you should rather use the "exec form" and write something like this:
…
ENTRYPOINT ["/usr/bin/chamber", "exec", "${ENV}_${SERVICE_NAME}", "-r", "1", "--", "./entrypoint.sh"]
CMD ["java -jar", "-Dspring.profiles.active=docker", "target/my.jar"]
However this won't work as is, because the ${ENV}
and ${SERVICE_NAME}
won't be expanded (as a shell would be required).
So the simplest, proper solution to apply here is to refactor your entrypoint.sh
, or if ever you don't want to change it and still rely on environment variables with an "exec form" ENTRYPOINT
, you could write instead:
…
RUN chmod a+x entrypoint1.sh
ENTRYPOINT ["./entrypoint1.sh"]
CMD ["java -jar", "-Dspring.profiles.active=docker", "target/my.jar"]
with a file
entrypoint1.sh
#!/bin/bash
exec /usr/bin/chamber exec ${ENV}_${SERVICE_NAME} -r 1 -- ./entrypoint.sh "$@"
这篇关于Dockerfile中的ENTRYPOINT之后CMD无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!