What is the Dockerfile?
Dockerfiles are instructions. They contains all of the commands used to build an image.
Best Practices =>
General guidelines:
Lets check the instruction manual for Dockerfile :=>
Dockerfiles are instructions. They contains all of the commands used to build an image.
- Docker images consist of read-only layers.
- Each represents a Dockerfile instruction.
- Layers are stacked. (layered architecture)
- Each layer is a result of the changes from the previous layer.
- Images are built using the
docker image build
command.
Dockerfile Layers
Dockerfile:
FROM ubuntu:15.04
COPY . /app
RUN make /app
CMD python /app/app.py
FROM
creates a layer from the ubuntu:15.04 Docker image.COPY
adds files from your Docker client’s current directory.RUN
builds your application with make.CMD
specifies what command to run within the container.
Best Practices =>
- Keep containers as ephemeral as possible.
- Follow Principle 6 of the 12 Factor App. (https://12factor.net/)
- Avoid including unnecessary files.
- Use
.dockerignore
. - Use multi-stage builds.
- Don’t install unnecessary packages.
- Decouple applications.
- Minimize the number of layers.
- Sort multi-line arguments.
- Leverage build cache.
Lets check the instruction manual for Dockerfile :=>
FROM
: Initializes a new build stage and sets the Base ImageRUN
: Will execute any commands in a new layerCMD
: Provides a default for an executing container. There can only be one CMD instruction in a DockerfileLABEL
: Adds metadata to an imageEXPOSE
: Informs Docker that the container listens on the specified network ports at runtimeENV
: Sets the environment variable <key>
to the value <value>
ADD
: Copies new files, directories or remote file URLs from <src>
and adds them to the filesystem of the image at the path <dest>
.COPY
: Copies new files or directories from <src>
and adds them to the filesystem of the container at the path <dest>
.ENTRYPOINT
: Allows for configuring a container that will run as an executableVOLUME
: Creates a mount point with the specified name
and marks it as holding externally mounted volumes from native host or
other containersUSER
: Sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN
, CMD
, and ENTRYPOINT
instructions that follow it in the DockerfileWORKDIR
: Sets the working directory for any RUN
, CMD
, ENTRYPOINT
, COPY
, and ADD
instructions that follow it in the DockerfileARG
: Defines a variable that users can pass at build-time to the builder with the docker build
command, using the --build-arg <varname>=<value>
flagONBUILD
: Adds a trigger instruction to the image that
will be executed at a later time, when the image is used as the base for
another buildHEALTHCHECK
: Tells Docker how to test a container to check that it is still workingSHELL
: Allows the default shell used for the shell form of commands to be overridden