Description:

  • https://docs.docker.com/engine/reference/builder/
  • Works layer by layer, data are used in the next layer
  • Start from the least likely to be changed first
  • Next update uses a last update and add the differences as a layer
    • much smaller file size
  • Layer-Creating Instructions: FROM, RUN, COPY, ADD → These modify the filesystem and create new image layers.
  • Non-Layer-Creating Instructions: CMD, ENTRYPOINT, ENV, EXPOSE, LABEL, VOLUME, USER, WORKDIR, HEALTHCHECK → These modify metadata but don’t create new image layers.

Overview:

Format

Parser directives

Environment replacement

dockerignore

Shell and Exect form

  • Exec form:
    • ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
    • better as it doesnt spawn a new shell, more secure
  • Shell form:
    • RUN source $HOME/.bashrc && echo $HOME
    • spawn a new shell, more complicated (variable expansion, command chaining, …)
    • only use if necessary
  • Use a different Shell:
    • SHELL ["/bin/bash", "-c"]
    • RUN echo hello

Dockerfile FROM

Dockerfile RUN

Dockerfile LABEL

Dockerfile ADD vs Dockerfile COPY

  • Add can be used for download files from the internet, extract tar files
  • Copy is only for copying local to container

Dockerfile CMD vs Dockerfile ENTRYPOINT

CMDENTRYPOINT
Purposedefault arguments for an ENTRYPOINT or a default command that can be easily overridden.Defines the primary executable that will always run when the container starts.
OverridingEasily overridden by any command/arguments provided after the image name in docker run.Not easily overridden by runtime arguments; new arguments are appended to the ENTRYPOINT command. To override, you must use the --entrypoint flag in docker run.
CombiningWhen combined with ENTRYPOINT (in exec form), CMD defines the default arguments for that executable.When combined with CMD (in exec form), ENTRYPOINTdefines the fixed command.
UsageUse when you want to provide a sensible default but allow users to easily run different commands.Use when you want your container to behave like a specific executable (e.g., a CLI tool).

WORKDIR

  • Equivalent to cd
  • The WORKDIR instruction sets the working directory for any RUNCMDENTRYPOINTCOPY and ADDinstructions that follow it in the Dockerfile.
  • If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
    • WORKDIR /app