Installing Rootless Docker on Ubuntu in China
Recently, I wanted to install Docker to run some processes isolated from my Ubuntu 26.04.1 LTS Linux system. Here you can find the necessary commands that I used to do this in rootless mode along with instructions how to use the domestic Chinese package repository and registry mirrors.
I have no background in security, rootless mode seems to be a good idea. This means that the Docker daemon and containers run as non-root users. If some process inside goes haywire and captures the container or even the daemon, at least it won’t have root privileges. It can see and do what the normal user can see or do on my system, but it cannot really install packages or modify other protected parts of the system configuration.
Additionally, I am located in China mainland. Therefore, I want to use China-domestic package repository and registry mirrors. This leads to much better performance. Also, the setup should be compliant with domestic requirements regarding system security, at least I hope that it is.
Here are the steps that I took in order to make this work.
I put them into two Bash scripts, which can easily be step-by-step into the shell.
You can download them from rootlessDockerCN1.sh and rootlessDockerCN2.sh.
You would first execute rootlessDockerCN1.sh step-by-step, then reboot, then execute rootlessDockerCN2.sh step-by-step, and then reboot.#!/bin/bash -
# Install rootless docker using domestic Chinese mirrors and registries, step 1.
# strict error handling
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
set -o nounset # set -u : exit the script if you try to use an uninitialized variable
set -o errexit # set -e : exit the script if any statement returns a non-true return value
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Welcome to the Rootless Docker Installation in China Script, Step 1."
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): First we update the list of packages from the package repository."
sudo apt-get -y update --fix-missing
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We install the needed packages 'ca-certificates', 'curl', 'gnupg', and 'lsb-release', if they have not yet been installed."
sudo apt-get install --fix-missing -y ca-certificates curl gnupg lsb-release
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Now we enable the domestic Chinese package repository from aliyun."
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Now we install the docker binaries step 1."
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Docker is now installed in version:"
docker --version
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We prepare some domestic registry mirrors in '/etc/docker'."
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://mirror.ccs.tencentyun.com",
"https://docker.m.daocloud.io",
"https://huecker.io"
]
}
EOF
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We re-load and restart docker."
sudo systemctl daemon-reload
sudo systemctl restart docker
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We install some requirements for running rootless docker."
sudo apt-get install --fix-missing -y uidmap
sudo apt-get install --fix-missing -y slirp4netns
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Now we need to stop and deactivate the root-full docker service."
sudo systemctl stop docker
sudo systemctl stop docker.socket
sudo systemctl stop containerd
sudo systemctl stop docker
sudo systemctl stop docker.socket
sudo systemctl stop containerd
sudo systemctl disable docker
sudo systemctl disable docker.socket
sudo systemctl disable containerd
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Please reboot now and continue with second installation script 'rootlessDockerCN2.sh'."
Reboot now.#!/bin/bash -
# Install rootless docker using domestic Chinese mirrors and registries, step 2.
# strict error handling
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
set -o nounset # set -u : exit the script if you try to use an uninitialized variable
set -o errexit # set -e : exit the script if any statement returns a non-true return value
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Welcome to the Rootless Docker Installation in China Script, Step 2."
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Making sure that we have the basic scripts available."
sudo apt install -y docker-ce-rootless-extras
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Ensuring that the 'nf_tables' kernel module is loaded."
sudo sh -eux <<EOF
# Load nf_tables module
modprobe nf_tables
EOF
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Now installing the rootless docker daemon."
dockerd-rootless-setuptool.sh install
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Providing the rootless daemon with the Chinese registry mirrors."
mkdir -p ~/.config/docker
tee ~/.config/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.xuanyuan.me",
"https://docker.1panel.live",
"https://docker.m.daocloud.io",
"https://hub.rat.dev"
],
"no-new-privileges": false
}
EOF
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Enabling/Starting/Restarting the rootless docker daemon for good measures."
systemctl --user enable docker.service
systemctl --user start docker.service
systemctl --user restart docker
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Making sure that the rootless docker daemon always runs for the current user."
sudo loginctl enable-linger "$USER"
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Ensuring that 'DOCKER_HOST' environment is always set at login."
echo "export DOCKER_HOST=unix:///run/user/1000/docker.sock" >> ~/.bashrc
echo "$(date +'%0Y-%0m-%0d %0R:%0S'): All done. You can now restart the computer. Docker should now be available as user process and it should use the Chinese domestic package registry mirrors."
Reading the scripts, you will notice some inconsistencies. For example, the registry mirrors stored for the root-daemon and for the rootless one are different. Well, this is the order of the commands I executed. It worked. So I will not uninstall everything to try again with different commands…
Anyway.
After rebooting again, you can executedocker run hello-world:latest
Notice that you do not need sudo to run this, it will run as a normal process under your current user.
Nice.