|
| 1 | +Dockerfile: |
| 2 | + |
| 3 | +FROM debian:wheezy |
| 4 | +ENTRYPOINT ["/bin/ping"] |
| 5 | +CMD ["localhost"] |
| 6 | + |
| 7 | +Execution: |
| 8 | +Docker build -t test . |
| 9 | +Docker run -d test |
| 10 | +Docker run -it test google.com |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +Dockerfile: |
| 16 | +FROM debian:wheezy |
| 17 | +CMD ["/bin/ping", "localhost"] |
| 18 | + |
| 19 | +Execution: |
| 20 | +Docker build -t test2 . |
| 21 | +Docker run test2 |
| 22 | +Docker run test2 bash |
| 23 | + |
| 24 | + |
| 25 | +The ENTRYPOINT specifies a command that will always be executed when the container starts. The CMD specifies arguments that will be fed to the ENTRYPOINT. |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are few rules that describe their co-operation. |
| 31 | + 1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands. |
| 32 | + 2. ENTRYPOINT should be defined when using the container as an executable. |
| 33 | + 3. CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container. |
| 34 | + 4. CMD will be overridden when running the container with alternative arguments |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +CMD |
| 39 | +╔════════════════════════════╦═════════════════════════════╗ |
| 40 | +║ No CMD ║ error, not allowed ║ |
| 41 | +╟────────────────────────────╫─────────────────────────────╢ |
| 42 | +║ CMD [“exec_cmd”, “p1_cmd”] ║ exec_cmd p1_cmd ║ |
| 43 | +╟────────────────────────────╫─────────────────────────────╢ |
| 44 | +║ CMD [“p1_cmd”, “p2_cmd”] ║ p1_cmd p2_cmd ║ |
| 45 | +╟────────────────────────────╫─────────────────────────────╢ |
| 46 | +║ CMD exec_cmd p1_cmd ║ /bin/sh -c exec_cmd p1_cmd ║ |
| 47 | +╚════════════════════════════╩═════════════════════════════╝ |
| 48 | + |
| 49 | +-- ENTRYPOINT exec_entry p1_entry |
| 50 | +╔════════════════════════════╦══════════════════════════════════╗ |
| 51 | +║ No CMD ║ /bin/sh -c exec_entry p1_entry ║ |
| 52 | +╟────────────────────────────╫──────────────────────────────────╢ |
| 53 | +║ CMD [“exec_cmd”, “p1_cmd”] ║ /bin/sh -c exec_entry p1_entry ║ |
| 54 | +╟────────────────────────────╫──────────────────────────────────╢ |
| 55 | +║ CMD [“p1_cmd”, “p2_cmd”] ║ /bin/sh -c exec_entry p1_entry ║ |
| 56 | +╟────────────────────────────╫──────────────────────────────────╢ |
| 57 | +║ CMD exec_cmd p1_cmd ║ /bin/sh -c exec_entry p1_entry ║ |
| 58 | +╚════════════════════════════╩══════════════════════════════════╝ |
| 59 | + |
| 60 | +-- ENTRYPOINT [“exec_entry”, “p1_entry”] |
| 61 | +╔════════════════════════════╦═════════════════════════════════════════════════╗ |
| 62 | +║ No CMD ║ exec_entry p1_entry ║ |
| 63 | +╟────────────────────────────╫─────────────────────────────────────────────────╢ |
| 64 | +║ CMD [“exec_cmd”, “p1_cmd”] ║ exec_entry p1_entry exec_cmd p1_cmd ║ |
| 65 | +╟────────────────────────────╫─────────────────────────────────────────────────╢ |
| 66 | +║ CMD [“p1_cmd”, “p2_cmd”] ║ exec_entry p1_entry p1_cmd p2_cmd ║ |
| 67 | +╟────────────────────────────╫─────────────────────────────────────────────────╢ |
| 68 | +║ CMD exec_cmd p1_cmd ║ exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd ║ |
| 69 | +╚════════════════════════════╩═════════════════════════════════════════════════╝ |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments