Dockerfile
A declarative setup file with steps of instructions for the Image.
By default an Image starts from a blank image, but most of the time you will start from another base image.
Syntax Example
Working with multiple Dockerfiles
If necessary when creating multiple containers, you can create multiple Dockerfiles in the same folder like.
Or one folder per Dockerfile.
docker build [OPTIONS] (Path | Url | -)
Add annotation to the image.
--build-arg
Overwrite or set ARG
values.
The name of the Dockerfile. (default: PATH/Dockerfile
)
--no-cache
Don't use cache when building the Image.
-o
or --output
Output destination. (format: type=local,dest=path
)
-t
or --tag
Name and optionally a tag (format: name:tag
)
Dockerfile Commands
Set <varname>=<value>
pairs that will be available:
During ONLY at build stages.
In Dockerfile as soon as you define them through
ARG
instructions.
Defines variables to be used ONLY at build-time. So they are accessable to the Dockerfile and docker build
.
ARG
s are useful for variables that should be used or changed at build-time, and that should not be persisted after the image was build.
ARG
values can be inspected with docker history
on an Image.
For this reason it is not recommended to use them for credentials, secrets or sensitive data, if untrusted users have access to the Image.
ARG <varname>=<value>
Not accessible at run-time, what it means in practice
This means that these variables are not accessible inside running containers.
But it also means that CMD
and ENTRYPOINT
instructions won't see these values by default.
Default <value>
You can define these variables without value, and expect the value inline at docker build
. If not provided Docker will generate an error.
You may also specify default values for these variables.
Access the ARG
values within the Dockerfile
ARG
values within the DockerfileAccess with $
or ${}
.
Overwriting <value>
You can overwrite values from an inline docker build --build-arg <varname>=<value>
.
Copies new files or folders from the SOURCE
and adds them to the filesystem of the image at DEST
.
Allows you to copy files from remote URL, or GIT repositories. ADD
is ideal for remote file copy.
Also if files are TAR files, they are automatically decompressed.
ADD [OPTIONS] ...SOURCE DEST
ADD [OPTIONS] [...SOURCE, DEST]
Required for paths with whitespace.
If multiple source files are specified, the last argument must be a DEST
, with a trailing /
.
Trailing /
at DEST
/
at DEST
If the destination has a trailing /
, the file or folder is copied inside this path.
If the destination doesn't have, the file or folder is copied beside the path.
DEST
path
DEST
pathDestinations that begin with /
, are considered absolute paths.
Destinations without /
at the start, are considered relative to the working directory.
Run variable commands when running a container from an image.
It doesn't run on build time, only specify intended commands for the image.
If more than one CMD is specified, ONLY the LAST one takes effect.
CMD
can specify only params (The 2° form), if there is an ENTRYPOINT
command specified before. Both CMD
and ENTRYPOINT
must be in Exec form.
Inline specified COMMAND
on a docker run
WILL overwrite the CMD
of the Dockerfile.
CMD ["executable","param1","param2"]
Exec form as Array.
CMD ["param1","param2"]
As default params for a previous ENTRYPOINT
.
CMD command param1 param2
Shell form as string.
Copies new files or folders from the SOURCE
and adds them to the filesystem of the image at DEST
.
COPY [OPTIONS] ...SOURCE DEST
COPY [OPTIONS] [...SOURCE, DEST]
Required for paths with whitespace.
If multiple source files are specified, the last argument must be a DEST
, with a trailing /
.
Trailing /
at DEST
/
at DEST
If the destination has a trailing /
, the file or folder is copied inside this path.
If the destination doesn't have, the file or folder is copied beside the path.
DEST
path
DEST
pathDestinations that begin with /
, are considered absolute paths.
Destinations without /
at the start, are considered relative to the working directory.
Run fixed commands when running a container from an image.
If more than one ENTRYPOINT
is specified, ONLY the LAST one takes effect.
You may pass inline arguments to docker run "image-name" -d
, that will be appended to the ENTRYPOINT
arguments.
You can overwrite entirely an ENTRYPOINT
with the --entrypoint
flag docker run "image-name" --entrypoint
.
ENTRYPOINT ["executable","param1","param2"]
Exec form as Array.
ENTRYPOINT command param1 param2
Shell form as string.
Informs Docker that the container listens on the specified network ports at runtime.
You may specify if the port is UDP
or TCP
(Default).
The EXPOSE
instruction doesn't actually publish the port.
It functions as a type of documentation between the person who builds the image and the person who runs the container.
Set <key>=<value>
pairs that will be available:
During build stages AND future running containers.
In Dockerfile as soon as you define them through
ENV
instructions.
You can inspect Image ENV values with docker inspect "image-id"
.
It is not recommended to use them for credentials, secrets or sensitive data, if untrusted users have access to the Image.
These variable leave traces in the Docker Image.
Don't just place them at the top of the Dockerfile.
Their placement might impact the caching of layers when developing Images, if their values are constantly updated.
ENV <key>=<value> ...
String <value> can be in the form of
Access the ENV
values within the Dockerfile
ENV
values within the DockerfileAccess with $
or ${}
.
Overwriting <value>
To overwrite values at build-time, you must use ARG
to set the default value for ENV
.
You can overwrite values at run-time docker run "image-name" --env <key>=<value>
.
.env
File
.env
FileRead ENV variables from a file by specifiyng it when running the container.
Execute commands creating a new layer on top of the current image. (The added layer is used in the next step inside the Dockerfile)
RUN [OPTIONS] [...COMMAND]
Exec form as Array.
RUN [OPTIONS] ...COMMAND
Shell form as string.
The cache for RUN instructions can be forcely invalidated by using --no-cache
flag.
The USER
instruction sets the user name (or UID) and optionally the user group (or GID) to use as the default user and group for the remainder of the current stage.
When the user doesn't have a primary group, then it will use as part of the root
group.
The specified user is used for RUN
instructions and at runtime, runs the relevant ENTRYPOINT
and CMD
commands.
USER <user>[:<group>]
USER <UID>[:GID]
Sets the working directory for any RUN
, CMD
, ENTRYPOINT
, COPY
and ADD
instructions that follow it in the Dockerfile.
If the specified WORKDIR
doesn't exist, it will be created.
If multiple WORKDIR
are defined, instructions will always use the last previous one for them.
Relative paths on multiple WORKDIR
will add them up.
Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers.
VOLUME /var/log
As string with multiple arguments.
VOLUME ["/var/log"]
As JSON Array. (Preferred way)
Last updated