Skip to content

Latest commit

 

History

History
117 lines (86 loc) · 8.08 KB

docker-faq.md

File metadata and controls

117 lines (86 loc) · 8.08 KB

Docker FAQ

RUN, CMD, ENTRYPOINT

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 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 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

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

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

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

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

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

  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"]
  8. RUN [ "echo", "$HOME" ] won't handle variable substitution for $HOME, but

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

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