Docker FAQ

RUN, CMD, ENTRYPOINT

InstructionRUNCMDENTRYPOINT
SummaryExecute “build commands”.Specify default commands.Specify default executable.
DescriptionRUN executes any commands to create a new layer on top of current imageCMD 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.
ExampleRUN 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 containerThe container’s main executable.

Example:

  • A simple Dockerfile use RUN to install neofetch and add a layer

    FROM ubuntu:latest
    
    RUN apt-get update && apt-get install -y neofetch
    
  • MySQL’s Official Dockerfile

    FROM debian-slim:latest
    # ...
    ENTRYPOINT ["docker-entrypoint.sh"]
    # ...
    CMD ["mysqld"]
    
  • Nginx’s Official Dockerfile

    FROM debian:bookworm-slim
    # ...
    ENTRYPOINT ["/docker-entrypoint.sh"]
    # ...
    CMD ["nginx", "-g", "daemon off;"]
    

Exec form vs shell form

Exec formShell form
SyntaxCMD [ "executable" , "param1" , "param2" ]RUN executable param1 param2
ExampleCMD [ "nginx" , "-g", "daemon off;" ]RUN apt-get update && apt-get install -y neofetch
Advantages- Avoid shell string mungingMore 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
DisadvantagesThere 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 array10As a regular string4

For more information, see exec and shell form | Docker

1

CMD [ "executable" , "param1" , "param2" ] (exec form)

2

CMD [ "param1" , "param2" ] (exec form, as default parameters to ENTRYPOINT)

10

You must use double-quotes (") around words, not single-quotes ('). e.g. ENTRYPOINT ["/bin/bash", "-c", "echo hello"]

8

RUN [ "echo", "$HOME" ] won’t handle variable substitution for $HOME, but

3

You can invoke the command with any shell you want, but you need to do it for every command in the exec form.

5

You can execute a shell directly with the exec form, for example: RUN [ "sh", "-c", "echo $HOME" ]

9

On a Windows machine: RUN ["c:\\windows\\system32\\tasklist.exe"]

4

For example, RUN source $HOME/.bashrc && echo $HOME can be split into multiple lines

6

You can override the default shell used for the shell form of command by using SHELL instruction

For example:

# Change the default shell to bash
SHELL ["/bin/bash", "-c"]
7

The default shell used for the shell form of command:

  • On Linux: ["/bin/sh", "-c"]
  • On Windows: ["cmd", "/S", "/C"]