Dockerfile
A declarative setup file with steps of instructions for the Image.
Syntax Example
FROM image:lastest
RUN command
EXPOSE 8000Working with multiple Dockerfiles
If necessary when creating multiple containers, you can create multiple Dockerfiles in the same folder like.
/dockerfiles
├── php.dockerfile
├── nginx.dockerfile
├── mysql.dockerfile
└── ...Or one folder per Dockerfile.
/php
└── Dockerfile
/nginx
└── Dockerfile
/mysql
└── Dockerfiledocker build [OPTIONS] (Path | Url | -)
Add annotation to the image.
--build-arg
Overwrite or set ARG values.
--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
ARGinstructions.
Defines variables to be used ONLY at build-time. So they are accessable to the Dockerfile and docker 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.
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 DESTIf 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.
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 DESTIf 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.
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).
Set <key>=<value> pairs that will be available:
During build stages AND future running containers.
In Dockerfile as soon as you define them through
ENVinstructions.
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.
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