Skip to content

Instantly share code, notes, and snippets.

@mdouchement
Last active March 9, 2024 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdouchement/5e017ad0355bd70a2b3a48d4c69a8d1a to your computer and use it in GitHub Desktop.
Save mdouchement/5e017ad0355bd70a2b3a48d4c69a8d1a to your computer and use it in GitHub Desktop.
Restore Docker on TrueNAS SCALE 23 and above (no Kubernetes)
#!/usr/bin/env bash
# Using Docker on TrueNAS SCALE 23+ (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.
# 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=/usr/bin/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=/usr/bin/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 <<EOF > ${SCRIPT_DIR}/daemon.json
{"data-root":"/var/docker","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