2018-10-15 07:52:53 +00:00
#!/bin/sh
# dockerd-rootless.sh executes dockerd in rootless mode.
#
2020-03-10 02:36:30 +00:00
# Usage: dockerd-rootless.sh [DOCKERD_OPTIONS]
2018-10-15 07:52:53 +00:00
#
# External dependencies:
# * newuidmap and newgidmap needs to be installed.
# * /etc/subuid and /etc/subgid needs to be configured for the current user.
2020-05-08 11:45:12 +00:00
# * Either one of slirp4netns (>= v0.4.0), VPNKit, lxc-user-nic needs to be installed.
2020-06-24 06:43:45 +00:00
#
# Recognized environment variables:
2024-01-15 02:21:18 +00:00
# * DOCKERD_ROOTLESS_ROOTLESSKIT_NET=(slirp4netns|vpnkit|pasta|lxc-user-nic): the rootlesskit network driver. Defaults to "slirp4netns" if slirp4netns (>= v0.4.0) is installed. Otherwise defaults to "vpnkit".
2020-06-24 06:43:45 +00:00
# * DOCKERD_ROOTLESS_ROOTLESSKIT_MTU=NUM: the MTU value for the rootlesskit network driver. Defaults to 65520 for slirp4netns, 1500 for other drivers.
2024-01-15 02:21:18 +00:00
# * DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER=(builtin|slirp4netns|implicit): the rootlesskit port driver. Defaults to "builtin".
2020-06-24 06:43:45 +00:00
# * DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX=(auto|true|false): whether to protect slirp4netns with a dedicated mount namespace. Defaults to "auto".
# * DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP=(auto|true|false): whether to protect slirp4netns with seccomp. Defaults to "auto".
2024-01-15 02:21:18 +00:00
# To apply an environment variable via systemd, create ~/.config/systemd/user/docker.service.d/override.conf as follows,
# and run `systemctl --user daemon-reload && systemctl --user restart docker`:
# --- BEGIN ---
# [Service]
# Environment="DOCKERD_ROOTLESS_ROOTLESSKIT_NET=pasta"
# Environment="DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER=implicit"
# --- END ---
# Guide to choose the network driver and the port driver:
2018-10-15 07:52:53 +00:00
#
2024-01-15 02:21:18 +00:00
# Network driver | Port driver | Net throughput | Port throughput | Src IP | No SUID | Note
# ---------------|----------------|----------------|-----------------|--------|---------|---------------------------------------------------------
# slirp4netns | builtin | Slow | Fast ✅ | ❌ | ✅ | Default in typical setup
# vpnkit | builtin | Slow | Fast ✅ | ❌ | ✅ | Default when slirp4netns is not installed
# slirp4netns | slirp4netns | Slow | Slow | ✅ | ✅ |
# pasta | implicit | Slow | Fast ✅ | ✅ | ✅ | Experimental; Needs recent version of pasta (2023_12_04)
2024-01-16 14:11:40 +00:00
# lxc-user-nic | builtin | Fast ✅ | Fast ✅ | ❌ | ❌ | Experimental
2024-01-15 02:21:18 +00:00
# (bypass4netns) | (bypass4netns) | Fast ✅ | Fast ✅ | ✅ | ✅ | (Not integrated to RootlessKit)
2021-02-25 11:11:50 +00:00
# See the documentation for the further information: https://docs.docker.com/go/rootless/
2018-10-15 07:52:53 +00:00
set -e -x
2021-02-02 06:19:50 +00:00
case " $1 " in
"check" | "install" | "uninstall" )
echo " Did you mean 'dockerd-rootless-setuptool.sh $@ ' ? "
exit 1
; ;
esac
2021-10-17 10:14:39 +00:00
if ! [ -w " $XDG_RUNTIME_DIR " ] ; then
2018-10-15 07:52:53 +00:00
echo "XDG_RUNTIME_DIR needs to be set and writable"
exit 1
fi
2021-10-17 10:14:39 +00:00
if ! [ -d " $HOME " ] ; then
2021-04-20 19:28:50 +00:00
echo "HOME needs to be set and exist."
2018-10-15 07:52:53 +00:00
exit 1
fi
rootlesskit = ""
for f in docker-rootlesskit rootlesskit; do
2021-04-23 05:47:18 +00:00
if command -v $f > /dev/null 2>& 1; then
2018-10-15 07:52:53 +00:00
rootlesskit = $f
break
fi
done
2021-10-17 10:14:39 +00:00
if [ -z " $rootlesskit " ] ; then
2018-10-15 07:52:53 +00:00
echo "rootlesskit needs to be installed"
exit 1
fi
2019-04-19 08:54:29 +00:00
: " ${ DOCKERD_ROOTLESS_ROOTLESSKIT_NET : = } "
: " ${ DOCKERD_ROOTLESS_ROOTLESSKIT_MTU : = } "
2020-06-24 06:43:45 +00:00
: " ${ DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER : =builtin } "
2019-09-02 05:58:58 +00:00
: " ${ DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX : =auto } "
: " ${ DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP : =auto } "
2019-04-19 08:54:29 +00:00
net = $DOCKERD_ROOTLESS_ROOTLESSKIT_NET
mtu = $DOCKERD_ROOTLESS_ROOTLESSKIT_MTU
2021-10-17 10:14:39 +00:00
if [ -z " $net " ] ; then
2021-04-23 05:47:18 +00:00
if command -v slirp4netns > /dev/null 2>& 1; then
2020-05-08 11:45:12 +00:00
# If --netns-type is present in --help, slirp4netns is >= v0.4.0.
if slirp4netns --help | grep -qw -- --netns-type; then
2019-04-19 08:54:29 +00:00
net = slirp4netns
2021-10-17 10:14:39 +00:00
if [ -z " $mtu " ] ; then
2019-04-19 08:54:29 +00:00
mtu = 65520
fi
else
2020-05-08 11:45:12 +00:00
echo "slirp4netns found but seems older than v0.4.0. Falling back to VPNKit."
2019-04-19 08:54:29 +00:00
fi
2018-10-15 07:52:53 +00:00
fi
2021-10-17 10:14:39 +00:00
if [ -z " $net " ] ; then
2021-04-23 05:47:18 +00:00
if command -v vpnkit > /dev/null 2>& 1; then
2019-04-19 08:54:29 +00:00
net = vpnkit
else
2020-05-08 11:45:12 +00:00
echo "Either slirp4netns (>= v0.4.0) or vpnkit needs to be installed"
2019-04-19 08:54:29 +00:00
exit 1
fi
fi
fi
2021-10-17 10:14:39 +00:00
if [ -z " $mtu " ] ; then
2019-04-19 08:54:29 +00:00
mtu = 1500
2018-10-15 07:52:53 +00:00
fi
2022-02-20 18:21:10 +00:00
dockerd = " ${ DOCKERD :- dockerd } "
2021-10-17 10:14:39 +00:00
if [ -z " $_DOCKERD_ROOTLESS_CHILD " ] ; then
2018-10-15 07:52:53 +00:00
_DOCKERD_ROOTLESS_CHILD = 1
export _DOCKERD_ROOTLESS_CHILD
2021-02-17 05:58:04 +00:00
if [ " $( id -u) " = "0" ] ; then
echo "This script must be executed as a non-privileged user"
exit 1
fi
2021-03-25 08:23:37 +00:00
# `selinuxenabled` always returns false in RootlessKit child, so we execute `selinuxenabled` in the parent.
# https://github.com/rootless-containers/rootlesskit/issues/94
if command -v selinuxenabled > /dev/null 2>& 1 && selinuxenabled; then
_DOCKERD_ROOTLESS_SELINUX = 1
export _DOCKERD_ROOTLESS_SELINUX
fi
2018-10-15 07:52:53 +00:00
# Re-exec the script via RootlessKit, so as to create unprivileged {user,mount,network} namespaces.
#
# --copy-up allows removing/creating files in the directories by creating tmpfs and symlinks
# * /etc: copy-up is required so as to prevent `/etc/resolv.conf` in the
# namespace from being unexpectedly unmounted when `/etc/resolv.conf` is recreated on the host
# (by either systemd-networkd or NetworkManager)
# * /run: copy-up is required so that we can create /run/docker (hardcoded for plugins) in our namespace
2019-05-15 11:52:59 +00:00
exec $rootlesskit \
2019-04-19 08:54:29 +00:00
--net= $net --mtu= $mtu \
2019-09-02 05:58:58 +00:00
--slirp4netns-sandbox= $DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX \
--slirp4netns-seccomp= $DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP \
2020-06-24 06:43:45 +00:00
--disable-host-loopback --port-driver= $DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER \
2018-10-15 07:52:53 +00:00
--copy-up= /etc --copy-up= /run \
2020-03-06 14:22:41 +00:00
--propagation= rslave \
2018-10-15 07:52:53 +00:00
$DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS \
2023-04-13 13:42:43 +00:00
" $0 " " $@ "
2018-10-15 07:52:53 +00:00
else
2021-10-17 10:14:39 +00:00
[ " $_DOCKERD_ROOTLESS_CHILD " = 1 ]
2018-10-15 07:52:53 +00:00
# remove the symlinks for the existing files in the parent namespace if any,
# so that we can create our own files in our mount namespace.
2020-06-26 07:45:49 +00:00
rm -f /run/docker /run/containerd /run/xtables.lock
2021-03-25 08:23:37 +00:00
if [ -n " $_DOCKERD_ROOTLESS_SELINUX " ] ; then
# iptables requires /run in the child to be relabeled. The actual /run in the parent is unaffected.
# https://github.com/containers/podman/blob/e6fc34b71aa9d876b1218efe90e14f8b912b0603/libpod/networking_linux.go#L396-L401
# https://github.com/moby/moby/issues/41230
chcon system_u:object_r:iptables_var_run_t:s0 /run
fi
2021-06-03 07:08:27 +00:00
if [ " $( stat -c %T -f /etc) " = "tmpfs" ] && [ -L "/etc/ssl" ] ; then
# Workaround for "x509: certificate signed by unknown authority" on openSUSE Tumbleweed.
# https://github.com/rootless-containers/rootlesskit/issues/225
realpath_etc_ssl = $( realpath /etc/ssl)
rm -f /etc/ssl
mkdir /etc/ssl
mount --rbind ${ realpath_etc_ssl } /etc/ssl
fi
2023-04-13 13:42:43 +00:00
exec " $dockerd " " $@ "
2018-10-15 07:52:53 +00:00
fi