Skip to content

Commit b99efe2

Browse files
committed
feat: add --start-daemon option to automatically start Docker service
Add a new --start-daemon command-line option that automatically starts and enables the Docker daemon service after installation. This addresses the cross-platform service management differences between distributions like CentOS and Ubuntu mentioned in issue #124. Features: - Detects systemd availability and uses appropriate service commands - Uses systemctl start/enable on systemd systems - Falls back to service/chkconfig on non-systemd systems - Supports --dry-run to show what commands would be executed - Only starts daemon when explicitly requested via --start-daemon flag - Works across all supported distributions (Ubuntu, Debian, CentOS, RHEL, Fedora) The implementation is safe by default - the daemon is only started when the user explicitly requests it, maintaining backward compatibility. Fixes #124 Signed-off-by: Gajesh Bhat <[email protected]>
1 parent 5944d4d commit b99efe2

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

install.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ set -e
7575
#
7676
# $ sudo sh install-docker.sh --mirror AzureChinaCloud
7777
#
78+
# --start-daemon
79+
#
80+
# Use the --start-daemon option to automatically start and enable the Docker
81+
# daemon service after installation. This option will attempt to start the
82+
# Docker service using the appropriate service management system (systemd,
83+
# etc.) for your distribution:
84+
#
85+
# $ sudo sh install-docker.sh --start-daemon
86+
#
87+
# Note: This option requires appropriate privileges to manage system services.
88+
#
7889
# ==============================================================================
7990

8091

@@ -110,6 +121,7 @@ fi
110121

111122
mirror=''
112123
DRY_RUN=${DRY_RUN:-}
124+
START_DAEMON=${START_DAEMON:-}
113125
while [ $# -gt 0 ]; do
114126
case "$1" in
115127
--channel)
@@ -123,6 +135,9 @@ while [ $# -gt 0 ]; do
123135
mirror="$2"
124136
shift
125137
;;
138+
--start-daemon)
139+
START_DAEMON=1
140+
;;
126141
--version)
127142
VERSION="${2#v}"
128143
shift
@@ -267,6 +282,64 @@ get_distribution() {
267282
echo "$lsb_dist"
268283
}
269284

285+
# Check if systemd is available and running
286+
has_systemd() {
287+
command_exists systemctl && systemctl --version >/dev/null 2>&1
288+
}
289+
290+
# Start and enable Docker daemon service
291+
start_docker_daemon() {
292+
if is_dry_run; then
293+
echo "# DRY RUN: Would start and enable Docker daemon service"
294+
if has_systemd; then
295+
echo "# DRY RUN: systemctl start docker"
296+
echo "# DRY RUN: systemctl enable docker"
297+
else
298+
echo "# DRY RUN: service docker start"
299+
echo "# DRY RUN: chkconfig docker on (or equivalent)"
300+
fi
301+
return
302+
fi
303+
304+
echo
305+
echo "Starting and enabling Docker daemon service..."
306+
307+
if has_systemd; then
308+
# Use systemd for modern distributions
309+
echo "Using systemd to manage Docker service"
310+
(
311+
set -x
312+
$sh_c 'systemctl start docker'
313+
$sh_c 'systemctl enable docker'
314+
)
315+
echo "Docker daemon started and enabled successfully"
316+
else
317+
# Fallback for older systems without systemd
318+
echo "Using traditional service management"
319+
(
320+
set -x
321+
$sh_c 'service docker start'
322+
)
323+
# Try to enable service on boot (distribution-specific)
324+
if command_exists chkconfig; then
325+
(
326+
set -x
327+
$sh_c 'chkconfig docker on'
328+
)
329+
elif command_exists update-rc.d; then
330+
(
331+
set -x
332+
$sh_c 'update-rc.d docker defaults'
333+
)
334+
else
335+
echo "Warning: Could not enable Docker service to start on boot"
336+
echo "Please manually configure Docker to start on boot for your system"
337+
fi
338+
echo "Docker daemon started successfully"
339+
fi
340+
echo
341+
}
342+
270343
echo_docker_as_nonroot() {
271344
if is_dry_run; then
272345
return
@@ -564,6 +637,9 @@ do_install() {
564637
fi
565638
$sh_c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq install $pkgs >/dev/null"
566639
)
640+
if [ -n "$START_DAEMON" ]; then
641+
start_docker_daemon
642+
fi
567643
echo_docker_as_nonroot
568644
exit 0
569645
;;
@@ -666,6 +742,9 @@ do_install() {
666742
fi
667743
$sh_c "$pkg_manager $pkg_manager_flags install $pkgs"
668744
)
745+
if [ -n "$START_DAEMON" ]; then
746+
start_docker_daemon
747+
fi
669748
echo_docker_as_nonroot
670749
exit 0
671750
;;

0 commit comments

Comments
 (0)