0. Why This Article Exists
It's Sunday. I slept until 10 a.m., got up to play with my Ring Fit Adventure for a while, and after lunch, I entered a boring mode. Thinking about a client-server data transmission issue I encountered at work under extremely unstable satellite network conditions, I decided to open my computer to think it through and ask ChatGPT for advice.
Following the principle of not wasting mental energy or time, I went straight to AnduinOS to practice learning Linux through daily operations. After all, my Linux knowledge is self-taught; I never studied it systematically and was thrust into the role without proper preparation.
I plan to use Docker with the ubuntu 22.04 image to run two or three containers, simulating one server and one or two clients.
First, I opened the AnduinOS Docker installation documentation: https://docs.anduinos.com/Applications/Development/Docker/Docker.html, and ran the Install Docker section and the Allow current user to run Docker commands without sudo section, because I am aware that malicious Docker images exist, and obviously, you wouldn't want them running within a Docker engine with root privileges.
Note: Later, I mistakenly thought this installation method was incorrect, so I uninstalled it and reinstalled Docker using the official documentation's install-using-the-repository method: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository. However, I did not revert the Allow current user to run Docker commands without sudo part from the AnduinOS documentation. I'm noting this here so I don't forget.
Additionally, the AnduinOS documentation helped me avoid some pitfalls. For example, it clearly mentions several reasons why using Docker Desktop is not recommended, as well as
Then I referred to online tutorials to configure registry mirrors. Most tutorials instruct you to create a /etc/docker/daemon.json file and write in a list of registry mirror addresses. I followed the instructions, and then I tried to pull some images, but I encountered this type of error:
Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
I naturally assumed it was a network issue. I then tried adding, removing, and modifying configurations multiple times, restarting the docker service, adjusting settings in various locations, and running commands similar to the one below to create a drop-in configuration file for the docker service:
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo nano /etc/systemd/system/docker.service.d/override.conf
However, all these attempts failed. During this period, I repeatedly asked ChatGPT and others, but none of them solved the problem. I believe it was because I never quite hit the right point in my questions.
Since I had previously configured Allow current user to run Docker commands without sudo, I mistakenly assumed that the docker command and the sudo docker command were equivalent. In reality, the problem stemmed precisely from this misunderstanding—they are not equivalent. This led me to start a series of follow-up questions to ChatGPT to learn more, which ultimately introduced me to a completely new and unfamiliar area: rootless mode.
1. What is rootless mode?
When I accidentally re-added a long list of mirror addresses to /etc/docker/daemon.json, and then accidentally added sudo to the docker pull command (sudo docker pull xxx), I discovered an interesting phenomenon: I was able to pull the image. I immediately used the docker images command to check my images, but was nearly stunned: the images I had just pulled were nowhere to be found.
After repeatedly confirming that the previous pull command had not thrown any errors and had correctly output the image ID, I became puzzled. My mind raced, and I thought of the countless instances of "computer mysticism" I had encountered before, concepts like "Heaven and Earth, Mystery and Yellow," and the Five Elements (metal, wood, water, fire, earth). Then a possible cause came to mind: sudo. I quickly ran the sudo docker images command, and the images I had just pulled appeared.
Only then did I realize that docker and sudo docker are different. Once I understood the phenomenon, I knew how to ask ChatGPT for an explanation. This led to a series of knowledge I had not previously known.
First, I used the groups command to confirm that my current user is in the docker user group. I used the ls -l /var/run/docker.sock command to check the permissions of the docker socket, and found that everything was fine. I then ran the docker info and sudo docker info commands separately, and noticed that the sudo command output the registry mirrors I had previously configured, while the command without sudo did not. Clearly, I had solved the mystery.
ChatGPT provided me with explanations from the following aspects:
1. Running Mode
Without
sudo:- Rootless mode is being used (
Security Options: rootless). - The
Docker Root Diris/home/kit/.local/share/docker, indicating that Docker's storage location is based on your user directory, and the running containers and images are also located within this directory.
- Rootless mode is being used (
With
sudo:- The standard root mode is used, and the
Docker Root Diris/var/lib/docker. - Container and image data is stored in the global system directory, managed by the root user.
- The standard root mode is used, and the
2. Separation of Data for Images and Containers
Without
sudo, you are viewing the user-level rootless Docker instance, which does not share images and containers from the root mode instance.- Images:
Images: 0(no images in the current user instance). - Containers:
Containers: 0(no containers in the current user instance).
- Images:
With
sudo, you are viewing the global root mode instance:- Images:
Images: 3. - Containers:
Containers: 1(1 stopped container).
- Images:
3. Security Options
Without
sudo:- Runs in rootless mode. In this mode, Docker's operational permissions are restricted, making it suitable for unprivileged users and providing higher security, though it may limit certain features (such as CPU and I/O limit support).
- It reports many disabled features (such as
cpu cfs quota,io.max, etc.), which are limitations of rootless mode.
With
sudo:- Runs in full privilege mode. Features are more complete (for example,
apparmoris enabled), but it requires elevated root permissions.
- Runs in full privilege mode. Features are more complete (for example,
4. Registry and Plugin Configuration
- When
sudois added, multiple configured registry mirrors (such asRegistry Mirrors) are displayed, which are not enabled in the rootless mode whensudois not used. - The plugin information is consistent, indicating that the Docker CLI is the same in both modes.
5. Why is this happening?
- Your current configuration is actually running two separate Docker instances simultaneously on the same system:
- One is a user-level rootless Docker instance.
- The other is a global root-mode Docker instance.
- Rootless Docker runs with your standard user permissions, and its storage and runtime environment are independent from the global Docker engine.
- Rootless mode can run independently without requiring root privileges; however, due to its limitations, it cannot share images and containers with the root mode.
Clearly, rootless is a more secure mode, but it also has certain limitations, such as the inability to share images and containers with root mode, and the lack of support for some features (such as CPU and I/O limits). On the other hand, root mode is a fully privileged mode with more complete functionality, but it requires elevated root privileges.
Additionally, since the two modes run two separate Docker daemons, I am able to stop one of them.
Considering that I currently do not need to use any advanced features, and that simulating poor satellite network conditions is a matter handled within the Linux system itself, unrelated to Docker, I have decided to continue using rootless mode. I attempted to run the following command to stop the root mode:
sudo systemctl stop docker.service
# 如果只 stop 了 docker.service,docker.socket 仍然在运行,所以要一并停掉
sudo systemctl stop docker.socket
sudo systemctl disable docker.service
sudo systemctl disable docker.socket
I will enable it when I have a need.
2. What is docker.socket?
docker.socketis a systemd socket unit that listens on Docker's UNIX socket/var/run/docker.sock.- When an application attempts to connect to this socket,
docker.socketactivates and startsdocker.service. - This is an on-demand service startup mechanism that conserves system resources.
3. How to configure the registry address in Docker rootless mode?
First, you need to resolve the registry mirrors issue. For rootless setups, the daemon.json configuration file is typically located at: ~/.config/docker/daemon.json. If it does not exist, you can create it manually and then add the mirror addresses.
After configuring the container Docker service, you obviously should no longer use the sudo systemctl restart docker command. Instead, use:
systemctl --user restart docker
This is another new piece of knowledge for me; I had no idea that the systemctl tool also has a --user option. All my understanding of systemd comes from the MSDN documentation on deploying ASP.NET Core applications. Additionally, because the name contains the word "system," I always assumed it was a system-level, root-level tool that could only be used with sudo.
Some other points are included in the supplementary content later in this document. However, since all of this information was provided by ChatGPT, I have not yet verified each point individually.
4. What other famous applications have a rootless mode?
In addition to docker and systemd mentioned earlier, there are other common applications that support rootless mode:
- podman, a container engine similar to docker
- Caddy, similar to Nginx
5. systemd rootless mode
Clearly, systemd is a commonly used tool. Since I have come this far in my understanding, I naturally want to explore its rootless mode in more detail.
Taking Docker as an example, it is evident that in root mode, there is a docker.service file under /etc/systemd/system/. In rootless mode, however, there is a docker.service file under ~/.config/systemd/user/. Therefore, systemd service files in rootless mode are located in the user-level directory ~/.config/systemd/user/.
If we want to use systemd in rootless mode in the future, we can create a my-service.service file under ~/.config/systemd/user/ and then manage it using the systemctl --user command.
Additionally, the data storage directory for Docker differs between the two modes: in root mode, it is /var/lib/docker, while in rootless mode, it is ~/.local/share/docker.
Below is some supplementary content, mostly consisting of my conversations with ChatGPT. These are unverified knowledge points that I will revisit and validate when I need to use them in the future.
Supplementary Content 1. ChatGPT's Explanation of Rootless Docker
Rootless Docker is a highly important feature for Linux users, especially in scenarios that require enhanced security and user isolation. Below is a comprehensive introduction to help you quickly understand its core concepts, advantages, limitations, and applicable use cases.
What is Rootless Docker?
Rootless Docker is a mode of operation for the Docker engine that does not require running with root privileges. Users can run containers within their own permissions without escalating to a superuser.
Traditional Docker mode:
- The Docker engine requires
rootprivileges to run. - Containers also run under
rootprivileges. - While
rootprivileges enhance functionality, they also introduce security risks: malicious containers may affect the host system.
- The Docker engine requires
Rootless Docker mode:
- Runs Docker containers with regular user privileges.
- The execution and storage environment of containers is fully isolated and cannot affect the global environment of the host system.
- The container runtime (such as
runc) uses User Namespace technology to map therootuser inside the container to a regular user on the host.
Why Do We Need Rootless Docker?
Security:
- Avoid sharing
rootprivileges between Docker containers and the host system. - Reduce the risk of container escape attacks (i.e., malicious code inside a container gaining control of the host).
- Avoid sharing
No
rootprivileges required:- Users can run Docker without system administrator privileges.
- On multi-user shared servers, regular users can run containers independently.
Isolation:
- Each user's Rootless Docker instance is completely independent.
- Data storage, networking, and configurations do not interfere with each other.
Simplified permission management:
- There is no need to configure
sudofor each user or add users to thedockergroup.
- There is no need to configure
How Rootless Docker Works
User Namespace:
- The
rootuser inside the container is actually mapped to a regular user on the host. - Other users inside the container (such as UID 1000) are also mapped to non-privileged users on the host.
- The
Filesystem and Storage:
- Container data is stored in the user directory (default path:
~/.local/share/docker). - It does not rely on
/var/lib/docker, avoiding conflicts with system-level storage.
- Container data is stored in the user directory (default path:
Network Isolation:
- Rootless mode uses
slirp4netnsorVPNKitby default to emulate user-space networking. - No modifications to the host's network configuration (such as
iptables) are required, preventing interference with other users.
- Rootless mode uses
Advantages of Rootless Docker
Enhanced Security:
- Even if a container is compromised, it cannot obtain
rootprivileges on the host.
- Even if a container is compromised, it cannot obtain
More Flexible Deployment:
- In shared hosting or multi-tenant environments, containers can be run without administrator intervention.
User Independence:
- Each user's Docker instance is independent and does not interfere with others.
No Complex Configuration Required:
- No system-level storage or network configuration is needed, making it suitable for lightweight requirements.
Limitations of Rootless Docker
Lower Performance:
- User-space network emulation (e.g.,
slirp4netns) is slower than system-level networking. - File system operations may experience slight performance loss due to permission transitions.
- User-space network emulation (e.g.,
Limited Functionality:
- Some features requiring high privileges are not supported, such as:
- Enabling
Cgroups(e.g., to limit CPU and memory usage). - Direct access to the host network (requires bridge configuration).
- Enabling
iptablesrules are not supported by default.
- Some features requiring high privileges are not supported, such as:
Compatibility Issues:
- Some images or tools may require additional configuration to run in rootless mode.
Higher System Requirements:
- Linux Kernel >= 5.11 is required for the best experience.
- Support for rootless mode may vary across different Linux distributions.
Use Cases for Rootless Docker
Development Environment:
- Prevent developers from inadvertently affecting the host system when testing containerized applications locally.
Shared Hosting:
- Provide isolated Docker instances on multi-user shared servers to avoid conflicts.
Security-Critical Production Environments:
- In scenarios with extremely high security requirements, run containers without
rootprivileges to reduce the attack surface.
- In scenarios with extremely high security requirements, run containers without
Education and Experimentation:
- Conduct container experiments without requiring administrator privileges.
How to Use Rootless Docker?
Check System Support:
- Ensure the kernel supports user namespaces:
The output should include:zgrep CONFIG_USER_NS /proc/config.gzCONFIG_USER_NS=y
- Ensure the kernel supports user namespaces:
Install Docker and enable Rootless mode:
dockerd-rootless-setuptool.sh installConfigure environment variables: Add the following content to
~/.bashrcor~/.zshrc:export PATH=$HOME/bin:$PATH export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sockStart the rootless Docker service:
systemctl --user start dockerVerify rootless mode: Run:
docker infoCheck whether
Security Optionsincludesrootless.
Optimization Suggestions for Rootless Docker
Enhance Network Performance:
- Replace
slirp4netnswithvpnkit, or configure bridged networking.
- Replace
Container Resource Management:
- Although Cgroups are not supported by default, you can enable some functionality by setting
Delegate=yesin thesystemduser service configuration.
- Although Cgroups are not supported by default, you can enable some functionality by setting
Shared Configuration:
- Configure a unified
~/.config/docker/daemon.jsonfor each user to avoid redundant configuration.
- Configure a unified
Monitoring and Debugging:
- Use
docker logsanddocker statsto monitor the performance of rootless containers.
- Use
Rootless Docker is a powerful tool, especially suitable for development and security-sensitive scenarios. If you are interested, you can try experimenting locally to explore its full capabilities!
Supplementary Content 2. Comparison of Key Configuration Files and Directories in the Two Modes
The configuration files and related directories for Rootless Docker and Root Docker are indeed different, exhibiting clear distinctions in their operational modes and storage structures. This design ensures the independence and isolation of Rootless Docker while allowing it to coexist with Root Docker without conflicts.
Below is a comparison of the key configuration files and directories in both modes:
Comparison of Root Docker and Rootless Docker Configuration File Locations
| Configuration Item | Root Docker (requires sudo) |
Rootless Docker (regular user) |
|---|---|---|
| Docker main config file | /etc/docker/daemon.json |
~/.config/docker/daemon.json |
| Runtime socket | /var/run/docker.sock |
$XDG_RUNTIME_DIR/docker.sock |
| Storage directory | /var/lib/docker |
~/.local/share/docker |
| Log directory | /var/lib/docker/containers/<container-id> |
~/.local/share/docker/containers/<container-id> |
| System service configuration | /lib/systemd/system/docker.service |
~/.config/systemd/user/docker.service |
A Detailed Explanation of Rootless Docker Configuration Files
~/.config/docker/daemon.jsonThedaemon.jsonfor Rootless mode is used to configure the behavior of the Docker daemon, and it has the same functionality as/etc/docker/daemon.json.
Common configurations:
{
"registry-mirrors": [
"https://mirror1.example.com",
"https://mirror2.example.com"
],
"log-driver": "json-file",
"storage-driver": "overlay2"
}
- Runtime Socket
The socket for Rootless Docker is located under
$XDG_RUNTIME_DIR, with the default path:
/run/user/$(id -u)/docker.sock
You can view it using the following command:
echo $XDG_RUNTIME_DIR
- Data Storage Directory All data for containers, images, and volumes is stored in:
~/.local/share/docker
This is the regular user directory, to avoid conflicts with the system-wide /var/lib/docker.
Coexistence with Root Docker
- How to elegantly manage Rootless and Root Docker?
Separate configuration files:
- Root Docker: Edit
/etc/docker/daemon.json. - Rootless Docker: Edit
~/.config/docker/daemon.json.
- Root Docker: Edit
Independent service execution:
- Root Docker service: Managed by the system-level
systemctl.sudo systemctl start docker - Rootless Docker service: managed by the user-level
systemctl.systemctl --user start docker
- Root Docker service: Managed by the system-level
Independent Runtime Environment:
- The networks, images, and container data of the two are completely isolated.
- Use the
DOCKER_HOSTenvironment variable to specify the Docker instance to operate on.
Comments (0)
No comments yet. Be the first to leave one!