简体   繁体   English

Dockerfile中的ENTRYPOINT之后CMD无法运行

[英]CMD doesn't run after ENTRYPOINT in Dockerfile

So I have a docker file which does this: 所以我有一个可以执行此操作的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 /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. 因此,ENTRYPOINT运行并从AWS Parameter存储中提取了一些秘密,并将其作为环境变量填充到entrypoint.sh shell中。 The entrypoint.sh then performs some actions with them, creates some files etc and in its last line does "exec $@". 然后,entrypoint.sh对其执行一些操作,创建一些文件等,并在其最后一行执行“ 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". 然后,我期望CMD能够运行,但它只能看到运行“ ExecStop = / usr / bin / docker stop app”的systemd服务文件。

The systemd service file does this to start the container: 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

What happened to CMD? 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: https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact中所述 ,如果您将CMDENTRYPOINT的“外壳形式”组合在一起,则会省略CMD规范:

摘自docs.docker.com

So you should rather use the "exec form" and write something like this: 因此,您应该使用“ exec表单”并编写如下内容:

…
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). 但是, 这将不能按原样工作 ,因为${ENV}${SERVICE_NAME}不会被扩展(因为需要一个shell)。

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: 因此,在这里应用的最简单,正确的解决方案是重构entrypoint.sh ,或者如果您不想更改它,而仍然依赖于带有“ exec形式” ENTRYPOINT环境变量,则可以编写:

…
RUN chmod a+x entrypoint1.sh
ENTRYPOINT ["./entrypoint1.sh"]
CMD ["java -jar", "-Dspring.profiles.active=docker", "target/my.jar"]

with a file 与文件

entrypoint1.sh entrypoint1.sh

#!/bin/bash
exec /usr/bin/chamber exec ${ENV}_${SERVICE_NAME} -r 1 -- ./entrypoint.sh "$@"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM