Docker Commands
Docker misc.
Install
Alpine
|
|
Ubuntu
|
|
Non-root
|
|
Import/Export Image
Manual
|
|
Push
Push from source to target.
|
|
OR
|
|
Pull
|
|
Pull All
Pull all images latest version.
|
|
Prune
Prune (remove) old versions of all images. Images in used will not be removed. Running containers will use new images after restart.
|
|
Dockerfile
Alpine Base
apk update
is not necessary if apk --no-cache add ...
is used for pulling packages.
Common Steps
If creating a lot of Dockerfile with similar base packages like tzdata
, ca-certificates
, they should be moved to the top and separate from container specific steps.
Container & Startup Script
GID/UID
Environment Variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/ash PUSR=mpd PHOME=/${PUSR} echo PGID:${PGID} echo PUID:${PUID} if [ "${PUID}" -lt "1000" ] then echo PUID cannot be \< 1000 exit 1 fi if [ "${PGID}" -lt "1000" ] then echo PGID cannot be \< 1000 exit 1 fi addgroup -g ${PGID} ${PUSR} adduser -D -h ${PHOME} -G ${PUSR} -u ${PUID} ${PUSR}
Full example here.
Docker Option
1
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
Time Zone
There are multiple ways to set time zone inside container. Following are 2:
In Dockerfile
, install tzdata
.
Environment Variables
Pass
P_TZ=America/New_York
into container.In start up script:
1 2 3 4 5 6 7 8 9 10
echo P_TZ:${P_TZ} if [ "${#P_TZ}" -gt "0" ]; then TZ="/usr/share/zoneinfo/${P_TZ}" if [ -f "${TZ}" ]; then cp ${TZ} /etc/localtime echo "${P_TZ}" >/etc/timezone else echo "${P_TZ}" not available. fi fi
This is more reliable if no control of hosting OS, like cloud or Windows.
Direct Mapping
Use:
1 2
-v /etc/localtime:/etc/localtime \ -v /etc/timezone:/etc/timezone
This is simpler if Linux host is guaranteed and always follow host’s time zone.
Exit Shell
If shell script is used in CMD
or ENTRYPOINT
to setup container environment, use exec
to execute the final command so the shell can exit.
|
|
This work for su <cmd>
also.
Command Check
|
|
Full example here.
Override ENTRYPOINT
Use --entrypoint sh
to start shell in container using ENTRYPOINT
:
|
|
Docker Compose
Specify compose file
|
|
Daemon mode
|
|
This will also start compose container if docker is auto start during reboot.
Restart
|
|
Enter shell of running compose container
|
|
Docker Daemon Configuration
URI
TCP
To enable remote/tcp docker daemon access, edit docker.server
|
|
with following content:
|
|
IPv6 use:
|
|
Unix Socket
Docker API socket is at /var/run/docker.sock
Data Root
–data-root, used to be -g, –graph, default to /var/lib/docker
Override in Systemd
|
|
with following content:
|
|
Override with /etc/docker/daemon.json
|
|
Log to Journald
All process stdout/stderr inside container go into docker log. To have that log into journald:
/etc/docker/daemon.json
|
|
/dev/log
For processes(eg. postfix) that write to system log, map /dev/log
:
|
|
In compose
|
|
daemon.json
From: Docker daemon configuration file
|
|