Instruction | RUN |
CMD |
ENTRYPOINT |
---|---|---|---|
Summary | Execute "build commands". | Specify default commands. | Specify default executable. |
Description | RUN executes any commands to create a new layer on top of current image |
CMD sets the command to be executed when running a container from image. |
An ENTRYPOINT allows you to configure a container that will run as an executable. |
Example | RUN apt-get update && apt-get install -y neofetch |
||
Notes | - The main purpose of a CMD is to provide (default command) and its parameters for an executing container.1 |
||
- CMD can also be used to only provide default parameters for the ENTRYPOINT instruction. 2 |
- Command line arguments to docker run <image> will be appended after all elements in an exec form ENTRYPOINT , and will override all elements specified using CMD . |
||
- You can override the ENTRYPOINT instruction using the docker run --entrypoint flag. |
|||
Provide defaults (executable and/or parameters) for an executing container | The container's main executable. |
Example:
-
A simple Dockerfile use
RUN
to install neofetch and add a layerFROM ubuntu:latest RUN apt-get update && apt-get install -y neofetch
-
FROM debian-slim:latest # ... ENTRYPOINT ["docker-entrypoint.sh"] # ... CMD ["mysqld"]
-
FROM debian:bookworm-slim # ... ENTRYPOINT ["/docker-entrypoint.sh"] # ... CMD ["nginx", "-g", "daemon off;"]
Exec form | Shell form | |
---|---|---|
Syntax | CMD [ "executable" , "param1" , "param2" ] |
RUN executable param1 param2 |
Example | CMD [ "nginx" , "-g", "daemon off;" ] |
RUN apt-get update && apt-get install -y neofetch |
Advantages | - Avoid shell string munging | More relaxed, and emphasizes ease of use, flexibility, and readability |
- Invoke commands using a specific command shell, or any other executable3 | - Long commands can be split up into multiple lines4 | |
Disadvantages | There is no automatically5 using of a command shell: | There is an automatically using of a command shell6 (the default shell7). |
- There is no shell processing, e.g. variable substitution8 | ||
- The backslashes need to be escaped9 | ||
How is it parsed? | As a JSON array10 | As a regular string4 |
For more information, see exec and shell form | Docker
Footnotes
-
CMD [ "executable" , "param1" , "param2" ]
(exec form) ↩ -
CMD [ "param1" , "param2" ]
(exec form, as default parameters to ENTRYPOINT) ↩ -
You can invoke the command with any shell you want, but you need to do it for every command in the exec form. ↩
-
For example,
RUN source $HOME/.bashrc && echo $HOME
can be split into multiple lines-
Using escape character
RUN apt-get update && \ apt-get install -y neofetch
-
Using heredocs
RUN <<EOF apt-get update apt-get install -y neofetch EOF
For more information about heredocs, see
-
-
You can execute a shell directly with the exec form, for example:
RUN [ "sh", "-c", "echo $HOME" ]
↩ -
You can override the default shell used for the shell form of command by using
SHELL
instructionFor example:
↩# Change the default shell to bash SHELL ["/bin/bash", "-c"]
-
The default shell used for the shell form of command:
- On Linux:
["/bin/sh", "-c"]
- On Windows:
["cmd", "/S", "/C"]
- On Linux:
-
RUN [ "echo", "$HOME" ]
won't handle variable substitution for$HOME
, but ↩ -
On a Windows machine:
RUN ["c:\\windows\\system32\\tasklist.exe"]
↩ -
You must use double-quotes (
"
) around words, not single-quotes ('
). e.g.ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
↩