Docker Grimoire
Base containers
openjdk:<version>
(debian with openJDK at<version>
) Tag examples (all denoted the same image at 2021-06-21)- Tags:
$version-{,jdk,jre}-$debian_codename
- version e.g., 11.0.11-9, omit rightmost for latest
- current (2021-06) debian codename is buster, can be omitted for latest
- default is jdk instead of jre
- Tags:
- busybox: minimal image without even libc (unless tagged)
- Tags:
{$version,stable,latest}-{,uclibc,glibc,musl}
.
- Tags:
- alpine: minimal distro with package manager and musl libc
- Tags:
{$version,latest,edge}
- FAQ
- package manager – apk
- OpenRC init system
- Tags:
Dockerfile
# comment
INSTRUCTION arg1 arg2 ...
...
Environment variables
- Syntax:
$name
or${name}
- Bash-like modifiers:
${name:-default}
:$name
if defined, elsedefault
${name:+override}
empty if$name
not defined, elseoverride
- Bash-like modifiers:
- Instructions that expand:
ADD
,COPY
,ENV
,EXPOSE
,FROM
,LABEL
,STOPSIGNAL
,USER
,VOLUME
,WORKDIR
andONBUILD
- Instructions that do not expand:
ARG
,CMD
,ENTRYPOINT
,HEALTHCHECK
andSHELL
.
Main instructions
FROM name:tag
: base container imageARG name[=default]
: Allow a variable$name
to be set ondocker build --build-arg name=value
ADD [--chown=user:group] src... dest
: copies files/dirs, inside to the context dir todest
, relative to theWORKDIR
. Allows*
as wildcard onsrc
. Features not present inCOPY:
src
can be an URL to be downloadedsrc
can be a .tar.* archive, which will be extracted intodest
COPY [--from=name|--chown=user:group] src... dest
LikeADD
but allows refering to parent images (FROM base AS AS name
).ENV name=value
Set env vars (will affect build and run-time)- at runtime:
docker run --env name=value
- at runtime:
EXPOSE port[/protocol]...
expose a listening port- at runtime:
docker run -p 80:80/tcp
- at runtime:
LABEL name=value ...
Set metadata (version
,description
,com.example.vendor
, …)USER user[:group]
Set the user and group that will run furtherRUN
,CMD
orENTRYPOINT
VOLUME mountpoint...
: Declare a mountpoint for an external volume. Writing to mountpoint at build time after this instruction will have no visible effects at run-time. The backing storage of the volume must be specified when lauching the container.WORKDIR dir
: Acd
that affects further instructions at build- and run-timeENTRYPOINT ["binary", "arg1", ...]
: The container will runENTRYPOINT + CMD
, with CMD being overriden with arguments given todocker run
CMD ["binary/arg1", "arg1/arg2", ...]
: SeeENTRYPOINT
, ifENTRYPOINT
is omitted the first item is assumed to be a binary
Some best practices: - The RUN
, COPY
and ADD
instructions are the ones that create new layers. - Instances of RUN
should be minimized (with scripts or &&
) - Instances of COPY
/ADD
add should be as granular and late as possible - Splitting a copy and delaying part of it avoids invalidating cached layers between the two copy instructions when only the later sources change - Between COPY
and ADD
, prefer COPY
for its simplicity - Instead of ADD
ing remote files used only at build time, wget/curl them and then remove - Use ENTRYPOINT
as the main binary and CMD
as the default arguments. - Use VOLUME
for mutable databases or config files to be edited by who launches the container
Building/managing/distributing Images
- Local build:
docker build -t TAG .
(without a tag, the image is named by its hash) - Local images:
docker image ls
: list imagesdocker image rm name[:tag]
: remove imagedocker image prune -a
: remove all images not used by any container
- Publish image:
docker tag LOCAL_TAG alexishuf/NAME:PUB_TAG
docker push alexishuf/NAME:TAG
(tag defaults to latest)
Container management
Common flags for docker run image [CMD]
:
-it
: Run interactive (i attaches docker run stdin with container entrypoint stdin) and with TTY (t).-t
is not compatible with output redirection of the docker run command (use-a
instead).-d
: Run detached (in background)-p hostPort:containerPort
: Connections to hostPort of the host are tuneled to containerPort of the container.-w dir
: set working directory ofCMD
inside the container--cpus=2.5
maximum CPUs usable by the container default is 0.0 (no limit)-m=512m
: maximum RAM usable by the container (suffixes are m and g), minimum is 4m and default is 0 (no limit)--memory-swap=1g
maximum memory that the container may swap to disk. Disable swap by setting this to the value given to -m. A value of 0 imposes no limit.
Reverse forwarding / acessing services on the host: Make the container point to the IPv4 addres in ip addr show docker0
.
Volume mounting:
-v /host:/container[:opts]
: bind mount thehost
dir in the host into thecontainer
dir inside the container-v name:/container
: Mount adocker volume
-managed volume on the container dir.-v /container[:opts]
: mount thecontainer
dir inside the container to a volume created by docker for this container- mount options (
:opts
) are ,-delimited
Volume management (docker volume
):
create --driver=local [--opt (driver-opt)] name
ls [dangling|DRIVER_NAME|label=KEY[=VALUE]|NAME]
list volumes matching filterinspect NAME...
show metadatarm [-f|--force] NAME...
remove a container
Container management (docker container
):
ls [-a]
list running (-a
: all) containersrm ID
remove containerdocker logs -f ID
follow the output from a detached container