Dockerfile

Defines the container environment.

Commands

Docs Builder

FROM

Docs Builder

A base image

a valid Dockerfile must start with a FROM instruction

FROM ubuntu

USER

Docs User

Set the user (and possibly the group)

USER username
USER username:group
USER UID
USER UID:GID

WORKDIR

Docs Workerdir

Sets the working directory. This will be used by any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD commands.

Multiple WORKDIRs can be specified; relative WORKDIRs will be on the previous WORKDIR path.

WORKDIR /test
WORKDIT ing # /test/ing

ENV

Docs Env

Set an environment variable

ENV key value # one variable
ENV key=value another=value # multiple variables, requires equals sign

ADD

Docs Add

Copy files, directories, and URLs from source to destination.

If the source is a recognized archive format, ADD will unpack it.

ADD source destination # UID & GID = 0
ADD --chown=UID:GID source destination
ADD --chown=UID source destination # GID = UID

Source interpreted relative to build's source context.

Destination is either absolute or relative to the WORKDIR

If --chown is not provided, files/directories created with UID & GID of 0.

COPY

Docs Copy

Copy files/directories from source to destination. COPY is like ADD, but doesn't support URLs and does not unpack archives.

COPY source destination
COPY ["source with spaces", "destination location"]
COPY --chown=user:group source destination

CMD

Docs CMD

Run a command, primarily used for provide defaults for executing container.

Only one CMD is allowed per Dockerfile. If there are multiple, the last one will be used.

CMD ["executable", "param", "another"]
CMD command param another

RUN

Docs Run

Run a command and create a new image. Use case: install a package on top of the OS.

The command can either be provided in shell form or exec form.

RUN command # shell form
RUN ["sequence", "of", "values"] # exec form

ARG

Docs Arg

A build-time variable, with optional default value.

ARG name
ARG other=default

Docker comes with some built-in args.

LABEL

Docs Label

Used for adding metadata to an image with key value pairs.

LABEL one="two"
LABEL three="four" five="six"
LABEL seven="eight" \
nine="ten" \
"eleven twelve"="thirteen"

An image's labels can be viewed using docker inspect.

EXPOSE

Docs Expose

Tell docker to listen to specified port. TCP or UDP (TCP is default if not specified)

EXPOSE 80/tcp
EXPOSE 123 # tcp
EXPOSE 456/udp

These can be overriden at runtime with the -p flag.

docker run -p 80:80/tcp

ENTRYPOINT

Docs Entrypoint

configure a container that will run as an executable

ENTRYPOINT ["executable", "one", "two"] # exec form
ENTRYPOINT command one two # shell form

VOLUME

Docs Volume

Create a mount point