Skip to content

Instantly share code, notes, and snippets.

@dz0ny
Forked from mdouchement/boot-docker.sh
Last active August 9, 2024 10:37
Show Gist options
  • Save dz0ny/05d34ab37c9a2709d29f1e8dcf21f488 to your computer and use it in GitHub Desktop.
Save dz0ny/05d34ab37c9a2709d29f1e8dcf21f488 to your computer and use it in GitHub Desktop.
Restore Docker on TrueNAS SCALE 23 & 24 and above (no Kubernetes)
#!/usr/bin/env bash
# Using Docker on TrueNAS SCALE 23+ & 24+ (no Kubernetes)
#
# Don't setup Apps via the TrueNAS Web GUI (don't choose a pool for Apps when asked).
# Make a dedicated docker dataset on one of your data pools.
#
# Store this script somewhere else on your pool (not in the Docker dataset).
# Download binaries archive from https://download.docker.com/linux/static/stable/x86_64/ and unarchive them in a `docker' folder in the same directory.
# Like https://download.docker.com/linux/static/stable/x86_64/docker-27.1.1.tgz
#
# $ ll
# total 55K
# drwxr-xr-x 3 root root 6 Mar 9 22:49 .
# drwxr-xr-x 15 root root 17 Mar 9 22:01 ..
# #-rwxr-xr-x 1 root root 3.7K Mar 9 22:46 boot-docker.sh
# drwxr-xr-x 2 root root 10 Mar 1 19:39 docker
#
# $ ll docker/
# total 75M
# drwxr-xr-x 2 root root 10 Mar 1 19:39 .
# drwxr-xr-x 3 root root 6 Mar 9 22:49 ..
# -rwxr-xr-x 1 root root 38M Mar 1 19:39 containerd
# -rwxr-xr-x 1 root root 12M Mar 1 19:39 containerd-shim-runc-v2
# -rwxr-xr-x 1 root root 19M Mar 1 19:39 ctr
# -rwxr-xr-x 1 root root 34M Mar 1 19:39 docker
# -rwxr-xr-x 1 root root 692K Mar 1 19:39 docker-init
# -rwxr-xr-x 1 root root 1.9M Mar 1 19:39 docker-proxy
# -rwxr-xr-x 1 root root 65M Mar 1 19:39 dockerd
# -rwxr-xr-x 1 root root 15M Mar 1 19:39 runc
#
# Make a daemon.json file in the same directory with the following contents:
# {"data-root": "/mnt/path/to/desired/docker/dataset/", "exec-opts": ["native.cgroupdriver=cgroupfs"]}
# ----> If this file does not exist, a default one will be created in the same directory.
# Then schedule this script to start via System Settings -> Advanced -> Init/Shutdown Scripts -> Add:
# Choose Type: Script and choose this script, choose to run at Pre Init under When
#
# Now install e.g. Portainer to manage your containers
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Avoid any conflicts with processes
systemctl disable k3s
systemctl stop k3s
# Create missing files
mkdir -p /etc/docker
if [ ! -f /etc/docker.env ]
then
# See /etc/systemd/system/docker.service.d/override.conf
touch /etc/docker.env
fi
# Write/Overwrite docker socket Systemd Unit file
cat << EOF > /lib/systemd/system/docker.socket
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target]
EOF
# Write/Overwrite containerd service Systemd Unit file
cat << EOF > /lib/systemd/system/containerd.service
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/mnt/data/docker/docker/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
EOF
# Write/Overwrite docker service Systemd Unit file
cat << EOF > /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket
[Service]
Type=notify
ExecStart=/mnt/data/docker/docker/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP \$MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
EOF
# Write docker default configuration file if it does not exist yet
# https://docs.docker.com/reference/cli/dockerd/#daemon-configuration-file
if [ ! -f ${SCRIPT_DIR}/daemon.json ]; then
cat cat << EOF > ${SCRIPT_DIR}/daemon.json
{"data-root":"/mnt/data/docker/data","exec-opts":["native.cgroupdriver=cgroupfs"],"iptables":true,"bridge":"none"}
EOF
fi
# Setup custom Docker config (overwrite if contents changed due to system update)
cmp --silent "${SCRIPT_DIR}/daemon.json" /etc/docker/daemon.json || echo "Updating Docker config..." && cp "${SCRIPT_DIR}/daemon.json" /etc/docker/daemon.json
# Setup Docker binaries
if [ ! -d ${SCRIPT_DIR}/docker ]; then
echo "You must download Docker binaries from https://download.docker.com/linux/static/stable/x86_64/ and"
echo "extract them in a 'docker' folder in the same directory of this script"
exit 1
fi
cp ${SCRIPT_DIR}/docker/* /usr/bin/
echo "Starting Docker"
systemctl start docker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment