Last active
March 27, 2020 02:50
-
-
Save odg0318/d9aa13d89a07e540b6b70ea400a9bd16 to your computer and use it in GitHub Desktop.
Right use entrypoint and cmd in Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM alpine:3.11 | |
| COPY entrypoint.sh /entrypoint.sh | |
| COPY process.sh /process.sh | |
| RUN chmod +x /entrypoint.sh /process.sh | |
| ENTRYPOINT ["/entrypoint.sh"] | |
| CMD ["/process.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -e | |
| trap handle_trap INT | |
| function handle_trap() { | |
| echo "trapped in entrypoint.sh" | |
| exit 1 | |
| } | |
| echo "begin entrypoint.sh" | |
| exec "$@" | |
| # the following line will be not executed. | |
| echo "end entrypoint.sh" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ docker build -t test . | |
| $ docker --rm run test | |
| begin entrypoint.sh | |
| begin process.sh | |
| Mem: 711664K used, 1055120K free, 16888K shrd, 2108K buff, 403816K cached | |
| CPU: 0% usr 0% sys 0% nic 100% idle 0% io 0% irq 0% sirq | |
| Load average: 0.03 0.05 0.06 1/173 6 | |
| PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND | |
| 6 1 root R 1580 0% 0 0% top | |
| 1 0 root S 1572 0% 0 0% {process.sh} /bin/sh /process.sh | |
| waiting for process(pid=$PID) | |
| trapped in process.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -e | |
| trap handle_trap INT | |
| function handle_trap() { | |
| echo "trapped in process.sh" | |
| kill -TERM $PID | |
| wait $PID | |
| exit 1 | |
| } | |
| echo "begin process.sh" | |
| # make a child process of process.sh | |
| top & | |
| PID=$! | |
| echo "waiting for process(pid=$PID)" | |
| wait $PID | |
| # the following line will be not executed. | |
| echo "end process.sh" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
List type of
ENTRYPOINTis recommended in Dockerfile.An example like above can be received
INTsignal from docker command.Flow
/entrypoint.sh /process.shis executed in a container./process.shis executed byexeccommand with the same pid as/entrypoint.sh, which makes/process.shreceive kill signals.topis executed on the background and/process.shwaits for its exiting.INTkill signal by ctrl+c, which is trapped in/process.sh.handle_trapfunction finally killstopprocess and/process.shalso exits.References