Index

dockerfile

build an image
docker build [directory]
docker build [git_url]
docker build [tar]
custom docker file
docker build -f [dockerfile] [directory]
don't use cache
docker build --no-cache [directory]
tag the created image
docker build -t [mytag] .
docker build -t [mytag]:latest .
docker build --tag [mytag] .
docker build --tag [mytag]:latest .

dockerfile contents

specify base image
FROM ubuntu
copy files to container (from build dir or filesystem)
COPY [src]... [dest]
COPY --chown=[user]:[group] [src]... [dest]
ADD [src]... [dest]
ADD --chown=[user]:[group] [src]... [dest]
default command to run on container startup (overridable)
CMD [command]
CMD ["command", "param1", "param2"]
command prefix to run on container startup (fixed)
ENTRYPOINT [command]
ENTRYPOINT ["command", "param1", "param2"]
run a command on image as part of build
RUN [command]
RUN ["command", "param1", "param2"]
run a command as a specific user
USER [name]
RUN [cmd]
run a command as a specific user in specific group
USER [name]:[group]
RUN [cmd]
set an environment property
ENV [key]=[value]
ENV [key1]=[value1] [key2]=[value2]
inform docker of ports used by container (for run -P)
EXPOSE [port]...
add metadata to image
LABEL readme="http://example.com/readme.txt"
example: copy script from build dir to container and run it
FROM ubuntu
COPY script /bin/test
RUN ["test"]
example: create ubuntu image with development tools pre-installed
FROM ubuntu
RUN apt update && apt install -y tree less vim net-tools
to install the image as dev
docker build -f dockerfile -t dev .

documentation

https://docs.docker.com/engine/reference/builder/
https://docs.docker.com/engine/reference/commandline/build/