Keywords: Docker daemon | systemctl | process management | Ubuntu | container runtime
Abstract: This article provides an in-depth exploration of various methods to stop the Docker daemon in Linux systems. Based on practical issues encountered in Ubuntu 16.04 environment, it focuses on analyzing why the systemctl stop docker command fails when Docker is manually started using sudo dockerd command. The article details systemd service management, process signal handling, and relationships between Docker architecture components, offering complete solutions and best practice recommendations. Through code examples and system analysis, it helps readers comprehensively understand Docker process management mechanisms.
Problem Background and Phenomenon Analysis
In Ubuntu 16.04 operating system, users attempt to stop Docker service using sudo systemctl stop docker command, but checking with ps ax | grep docker reveals that Docker-related processes are still running. Specific processes include:
11347 ? Sl 0:00 containerd-shim 487e3784f983274131d37bde1641db657e76e41bdd056f43ef4ad5adc1bfc518 /var/run/docker/libcontainerd/487e3784f983274131d37bde1641db657e76e41bdd056f43ef4ad5adc1bfc518 runc
29914 ? S 0:00 sudo dockerd -H gridsim1103:2376
29915 ? Sl 4:45 dockerd -H gridsim1103:2376
29922 ? Ssl 0:24 containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime runc
30107 ? Sl 1:01 /usr/bin/docker-proxy -proto tcp -host-ip 188.184.80.77 -host-port 8500 -container-ip 192.17.0.2 -container-port 8500
30139 ? Sl 0:00 /usr/bin/docker-proxy -proto tcp -host-ip 188.184.80.77 -host-port 8400 -container-ip 192.17.0.2 -container-port 8400
Root Cause Analysis
From the process list, it's evident that the Docker daemon was manually started using sudo dockerd -H gridsim1103:2376 command, rather than through systemd service manager. This startup method causes the process to be uncontrolled by systemd, thus making the systemctl stop docker command ineffective.
Detailed Solutions
Immediately Stopping Currently Running Docker Processes
For manually started Docker processes, the most direct stopping method is sending termination signals to relevant processes:
# Find Docker-related process IDs
sudo ps aux | grep dockerd
# Use kill command to terminate processes
sudo kill -TERM 29915 # Replace with actual dockerd process ID
# If processes don't respond to TERM signal, use forced termination
sudo kill -KILL 29915
Correct Docker Service Management Approach
To avoid such issues, Docker service should be managed using systemd:
# Enable Docker service (auto-start on boot)
sudo systemctl enable docker
# Start Docker service
sudo systemctl start docker
# Stop Docker service
sudo systemctl stop docker
# Restart Docker service
sudo systemctl restart docker
# Check Docker service status
sudo systemctl status docker
Handling Socket Activation Mechanism
In some Docker installation configurations, socket activation mechanism may exist. When stopping docker.service, if warning message appears:
Warning: Stopping docker.service, but it can still be activated by: docker.socket
This indicates that docker.socket needs to be stopped simultaneously:
sudo systemctl stop docker.socket
sudo systemctl stop docker.service
Docker Architecture Component Analysis
From the process list, complete Docker runtime includes multiple components:
- dockerd: Docker daemon, managing container lifecycle
- containerd: Container runtime, responsible for actual container operations
- containerd-shim: Container runtime shim, managing individual container processes
- docker-proxy: Network proxy, handling container network traffic
Best Practice Recommendations
Based on actual problems and solutions, the following best practices are proposed:
- Always use systemd to manage Docker service, avoid manually starting dockerd processes
- Before stopping Docker service, ensure all running containers are properly handled
- Regularly check Docker service status to ensure service management consistency
- In production environments, consider using container orchestration tools for finer process management
Supplementary Solution Comparison
Besides main solutions, other methods include:
- Using
sudo service docker stop(suitable for non-systemd systems) - For snap-installed Docker, using
sudo snap stop docker - Directly using Ctrl+C to terminate manually started dockerd processes
Conclusion
Properly stopping Docker daemon requires understanding process startup methods and service management mechanisms. Through standardized Docker service management using systemd, various problems caused by manual process management can be avoided, ensuring system stability and maintainability.