Table of Contents
Docker
Main install
apt-get -y install curl unzip dialog curl -fSSL get.docker.com | sh
Useful alias to exclude container overlays from the df command for use in .bashrc or .profile:
alias df='/usr/bin/df -x overlay'
Show logs
docker logs --follow <containername>
Build image
To build the docker image based on a Dockerfile in the current directory, use:
docker build .
Update container
Update docker container to latest version via docker compose:
#Single container: docker compose pull calibre-web docker compose up -d calibre-web docker image prune #Update all containers: docker compose pull docker compose up -d docker image prune
Health Checks
Docker compose health checks:
https://medium.com/geekculture/how-to-successfully-implement-a-healthcheck-in-docker-compose-efced60bc08e
https://stefanjarina.gitbooks.io/docker/content/swarm-mode/healthchecks.html
Copying files to/from containers
Copying files from/to containers
docker cp dockerimage:/bla ./bla docker cp ./bla dockerimage:/bla
Flatten docker image layers
Flatten docker image layers:
FROM yourbuildimage as build # your existing build steps here FROM scratch COPY --from=build / / CMD ["/your/start/script"]
Change mount points of containers
Change mount points of existing docker containers;
e.g. mount /home/<user-name> folder of host to the /mnt folder of the existing (not running) container.
- stop docker container or whole docker engine
systemctl stop docker.service
- Open configuration file corresponding to the stopped container, which can be found at /var/lib/docker/containers/99d…1fb/config.v2.json (may be config.json for older versions of docker). For pretty print use
vi <(jq . /var/lib/docker/containers/<container-ID>/config.v2.json) Save updates to a file: :w config.v2.json Exit vim: :q! Update existing file: jq -c . config.v2.json > /var/lib/docker/containers/<container-ID>/config.v2.json
- Find MountPoints section: “MountPoints”:{}.
- Replace the contents with something like this (you can copy proper contents from another container with proper settings):
"MountPoints":{"/mnt":{"Source":"/home/<user-name>","Destination":"/mnt","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/<user-name>","Target":"/mnt"},"SkipMountpointCreation":false}}
or the same (formatted):
"MountPoints": { "/mnt": { "Source": "/home/<user-name>", "Destination": "/mnt", "RW": true, "Name": "", "Driver": "", "Type": "bind", "Propagation": "rprivate", "Spec": { "Type": "bind", "Source": "/home/<user-name>", "Target": "/mnt" }, "SkipMountpointCreation": false } }
- Start or restart the docker service:
systemctl start docker.service service docker restart
- Start the container if necessary:
docker start <container-name/ID>
PHP&NGINX image
Containerised PHP & NGINX on Alpine, image size ~60MB:
https://levelup.gitconnected.com/containerizing-nginx-php-fpm-on-alpine-linux-953430ea6dbc
https://github.com/johnathanesanders/docker-nginx-fpm
Log nginx to stdout in Dockerfile:
# forward request and error logs to docker log collector RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log
Rename/Retag image files
docker tag current/image:tag new/image:tag
cmd shell of container
docker exec -it <mycontainer> bash docker exec -it <mycontainer> /bin/sh
GPU hardware acceleration
Make sure the relevant GPU drivers are installed on the base system, then pass through the device via docker compose.
Check devices are available, there should be a device per GPU starting at renderD128 for the first GPU:
ls -la /dev/dri
devices: - /dev/dri:/dev/dri
for AMD:
https://rocm.docs.amd.com/projects/install-on-linux/en/latest/install/install-methods/package-manager/package-manager-debian.html
https://rocm.docs.amd.com/projects/install-on-linux/en/latest/how-to/docker.html
mkdir --parents --mode=0755 /etc/apt/keyrings wget https://repo.radeon.com/rocm/rocm.gpg.key -O - | gpg --dearmor | sudo tee /etc/apt/keyrings/rocm.gpg > /dev/null echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/rocm/apt/6.4.3 jammy main" > tee /etc/apt/sources.list.d/rocm.list echo -e 'Package: *\nPin: release o=repo.radeon.com\nPin-Priority: 600' > /etc/apt/preferences.d/rocm-pin-600 apt update apt install rocm
Then add to the docker containers:
devices: - /dev/kfd - /dev/dri security_opt: - seccomp=unconfined environment: - LIBVA_DRIVER_NAME=radeonsi
Alpine Linux docker images may require libva to be installed:
post_start: # for 13th gen Iris Xe GPUs use intel-media-driver for older gens libva-intel-driver # - command: apk add libva-intel-driver intel-media-driver # user: root # For AMD - command: apk add libva libva-utils mesa-va-gallium user: root
nvtop can be used for AMD and NVidia gpus to see if the gpu is being used.
On Debian, nvtop is in the contrib repositories which need to be added.
#bookworm sed -r 's/^deb(.*)$/deb\1 contrib/g' /etc/apt/sources.list #new deb822 sources format sed -i 's/^Components: main$/& contrib non-free non-free-firmware/' /etc/apt/sources.list.d/debian.sources apt update
On Ubuntu systems, nvtop is in the default repositories.
apt-get install nvtop
docker ps short
Short form of docker ps to only get the container names:
docker ps | sed 's/ */#/g' | cut -d "#" -f 7
docker command template
docker running out of network addresses
Error response from daemon: all predefined address pools have been fully subnetted
- /etc/docker/daemon.json
{ "default-address-pools":[ {"base":"169.254.2.0/23","size":28}, {"base":"169.254.4.0/22","size":28}, {"base":"169.254.8.0/21","size":28}, {"base":"169.254.16.0/20","size":28}, {"base":"169.254.32.0/19","size":28}, {"base":"169.254.64.0/18","size":28}, {"base":"169.254.128.0/18","size":28}, {"base":"169.254.192.0/19","size":28}, {"base":"169.254.224.0/20","size":28}, {"base":"169.254.240.0/21","size":28}, {"base":"169.254.248.0/22","size":28} ] }
docker restart docker network prune