Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

docker - CMD doesn't run after ENTRYPOINT in Dockerfile

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

Exerpt from docs.docker.com

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...