CMD 在 Dockerfile 中的 ENTRYPOINT 后不运行 [英] CMD doesn't run after ENTRYPOINT in Dockerfile

查看:143
本文介绍了CMD 在 Dockerfile 中的 ENTRYPOINT 后不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 docker 文件来执行此操作:

ENV ENV ${ENV}ENV SERVICE_NAME ${SERVICE_NAME}用户应用ENV HOME=/home/app复制目标/home/app/target复制入口点.sh/home/app工作目录/home/app入口点/usr/bin/chamber exec ${ENV}_${SERVICE_NAME} -r 1 -- ./entrypoint.shCMD java -jar -Dspring.profiles.active=docker target/my.jar

因此 ENTRYPOINT 运行并从 AWS Parameter Store 中提取一些秘密,并将它们作为环境变量填充到 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 form"并写成这样:

…入口点 ["/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 的变量,你可以改写:

…运行 chmod a+x entrypoint1.sh入口点 ["./entrypoint1.sh"]CMD ["java -jar", "-Dspring.profiles.active=docker", "target/my.jar"]

一个文件

<块引用>

entrypoint1.sh

#!/bin/bashexec/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 "$@"

这篇关于CMD 在 Dockerfile 中的 ENTRYPOINT 后不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆