Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -950,6 +950,29 @@ If you then run `docker stop test`, the container will not exit cleanly - the |
| 950 | 950 |
user 0m 0.04s |
| 951 | 951 |
sys 0m 0.03s |
| 952 | 952 |
|
| 953 |
+### Understand how CMD and ENTRYPOINT interact |
|
| 954 |
+ |
|
| 955 |
+Both `CMD` and `ENTRYPOINT` instructions define what command gets executed when running a container. |
|
| 956 |
+There are few rules that describe their co-operation. |
|
| 957 |
+ |
|
| 958 |
+1. Dockerfile should specify at least one of `CMD` or `ENTRYPOINT` commands. |
|
| 959 |
+ |
|
| 960 |
+2. `ENTRYPOINT` should be defined when using the container as an executable. |
|
| 961 |
+ |
|
| 962 |
+3. `CMD` should be used as a way of defining default arguments for an `ENTRYPOINT` command |
|
| 963 |
+or for executing an ad-hoc command in a container. |
|
| 964 |
+ |
|
| 965 |
+4. `CMD` will be overridden when running the container with alternative arguments. |
|
| 966 |
+ |
|
| 967 |
+The table below shows what command is executed for different `ENTRYPOINT` / `CMD` combinations: |
|
| 968 |
+ |
|
| 969 |
+| | No ENTRYPOINT | ENTRYPOINT exec_entry p1_entry | ENTRYPOINT ["exec_entry", "p1_entry"] | |
|
| 970 |
+|--------------------------------|----------------------------|-----------------------------------------------------------|------------------------------------------------| |
|
| 971 |
+| **No CMD** | *error, not allowed* | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry | |
|
| 972 |
+| **CMD ["exec_cmd", "p1_cmd"]** | exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry exec_cmd p1_cmd | exec_entry p1_entry exec_cmd p1_cmd | |
|
| 973 |
+| **CMD ["p1_cmd", "p2_cmd"]** | p1_cmd p2_cmd | /bin/sh -c exec_entry p1_entry p1_cmd p2_cmd | exec_entry p1_entry p1_cmd p2_cmd | |
|
| 974 |
+| **CMD exec_cmd p1_cmd** | /bin/sh -c exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd | exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd | |
|
| 975 |
+ |
|
| 953 | 976 |
## VOLUME |
| 954 | 977 |
|
| 955 | 978 |
VOLUME ["/data"] |