Dockerfile
Defines the container environment.
Commands
FROM
A base image
a valid Dockerfile must start with a
FROM
instruction
FROM ubuntu
USER
Set the user (and possibly the group)
USER usernameUSER username:groupUSER UIDUSER UID:GID
WORKDIR
Sets the working directory. This will be used by any subsequent RUN
, CMD
, ENTRYPOINT
, COPY
, and ADD
commands.
Multiple WORKDIR
s can be specified; relative WORKDIR
s will be on the previous WORKDIR
path.
WORKDIR /testWORKDIT ing # /test/ing
ENV
Set an environment variable
ENV key value # one variableENV key=value another=value # multiple variables, requires equals sign
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 = 0ADD --chown=UID:GID source destinationADD --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
Copy files/directories from source to destination. COPY
is like ADD
, but doesn't support URLs and does not unpack archives.
COPY source destinationCOPY ["source with spaces", "destination location"]COPY --chown=user:group source destination
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
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 formRUN ["sequence", "of", "values"] # exec form
ARG
A build-time variable, with optional default value.
ARG nameARG other=default
Docker comes with some built-in args.
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
Tell docker to listen to specified port. TCP or UDP (TCP is default if not specified)
EXPOSE 80/tcpEXPOSE 123 # tcpEXPOSE 456/udp
These can be overriden at runtime with the -p
flag.
docker run -p 80:80/tcp
ENTRYPOINT
configure a container that will run as an executable
ENTRYPOINT ["executable", "one", "two"] # exec formENTRYPOINT command one two # shell form
VOLUME
Create a mount point