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:
$nameor${name}- Bash-like modifiers:
${name:-default}:$nameif defined, elsedefault${name:+override}empty if$namenot defined, elseoverride
- Bash-like modifiers:
- Instructions that expand:
ADD,COPY,ENV,EXPOSE,FROM,LABEL,STOPSIGNAL,USER,VOLUME,WORKDIRandONBUILD - Instructions that do not expand:
ARG,CMD,ENTRYPOINT,HEALTHCHECKandSHELL.
Main instructions
FROM name:tag: base container imageARG name[=default]: Allow a variable$nameto be set ondocker build --build-arg name=valueADD [--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:srccan be an URL to be downloadedsrccan be a .tar.* archive, which will be extracted intodest
COPY [--from=name|--chown=user:group] src... destLikeADDbut allows refering to parent images (FROM base AS AS name).ENV name=valueSet 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,CMDorENTRYPOINTVOLUME 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: Acdthat affects further instructions at build- and run-timeENTRYPOINT ["binary", "arg1", ...]: The container will runENTRYPOINT + CMD, with CMD being overriden with arguments given todocker runCMD ["binary/arg1", "arg1/arg2", ...]: SeeENTRYPOINT, ifENTRYPOINTis 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 ADDing 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_TAGdocker 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).-tis not compatible with output redirection of the docker run command (use-ainstead).-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 ofCMDinside the container--cpus=2.5maximum 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=1gmaximum 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 thehostdir in the host into thecontainerdir inside the container-v name:/container: Mount adocker volume-managed volume on the container dir.-v /container[:opts]: mount thecontainerdir 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)] namels [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 IDremove containerdocker logs -f IDfollow the output from a detached container