Skip to content

Commit 2794aef

Browse files
committed
Automatically start Docker daemon after installation by default
This PR makes the Docker daemon start automatically after installation by default, with a --no-autostart opt-out flag. This addresses issue #124 and aligns with the "opinionated convenience" philosophy of get.docker.com. Changes: - Autostart enabled by default (AUTOSTART=1) - Added --no-autostart flag to opt-out - Smart systemd detection using /run/systemd/system directory check - Falls back to traditional service management (service/chkconfig/update-rc.d) - Uses sh_c pattern for proper dry-run support - All informational messages output to stderr The systemd detection checks for /run/systemd/system directory which only exists when systemd is actually running as PID 1. This prevents failures in container environments where systemctl is installed but systemd is not running. Tested across all supported distributions: - Ubuntu (20.04, 22.04, 24.04) - Debian (11, 12) - CentOS Stream (8, 9) - Fedora (40, 41) - RHEL/AlmaLinux (8, 9) Fixes #124 Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
1 parent a00c5c3 commit 2794aef

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

install.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ set -e
8383
#
8484
# $ sudo sh install-docker.sh --setup-repo
8585
#
86+
# Automatic Service Start
87+
#
88+
# By default, this script will automatically start and enable the Docker daemon
89+
# service after installation using the appropriate service management system
90+
# (systemd, etc.) for your distribution.
91+
#
92+
# If you prefer to start the service manually, use the --no-autostart option:
93+
#
94+
# $ sudo sh install-docker.sh --no-autostart
95+
#
96+
# Note: Starting the service requires appropriate privileges to manage system services.
97+
#
8698
# ==============================================================================
8799

88100

@@ -119,6 +131,7 @@ fi
119131
mirror=''
120132
DRY_RUN=${DRY_RUN:-}
121133
REPO_ONLY=${REPO_ONLY:-0}
134+
AUTOSTART=${AUTOSTART:-1}
122135
while [ $# -gt 0 ]; do
123136
case "$1" in
124137
--channel)
@@ -140,6 +153,9 @@ while [ $# -gt 0 ]; do
140153
REPO_ONLY=1
141154
shift
142155
;;
156+
--no-autostart)
157+
AUTOSTART=0
158+
;;
143159
--*)
144160
echo "Illegal option $1"
145161
;;
@@ -280,6 +296,76 @@ get_distribution() {
280296
echo "$lsb_dist"
281297
}
282298

299+
# Check if systemd is available and running
300+
# Returns 0 if systemd is available, 1 otherwise
301+
has_systemd() {
302+
if [ -d /run/systemd/system ]; then
303+
return 0
304+
else
305+
return 1
306+
fi
307+
}
308+
309+
start_docker_daemon() {
310+
>&2 echo
311+
>&2 echo "Starting and enabling Docker daemon service..."
312+
313+
# In container environments, systemd may be installed but not running as PID 1.
314+
# We try systemctl if it exists, regardless of whether systemd is the init system.
315+
if command_exists systemctl; then
316+
if ! is_dry_run; then
317+
if has_systemd; then
318+
>&2 echo "Using systemd to manage Docker service"
319+
else
320+
>&2 echo "Attempting to use systemctl (systemd not running as init)"
321+
fi
322+
fi
323+
(
324+
set -x
325+
# In containers, these commands may fail but we try anyway
326+
$sh_c 'systemctl start docker' || true
327+
$sh_c 'systemctl enable docker' || true
328+
)
329+
if ! is_dry_run; then
330+
>&2 echo "Docker service configuration attempted"
331+
fi
332+
elif command_exists service; then
333+
# Fallback for older systems without systemd
334+
if ! is_dry_run; then
335+
>&2 echo "Using traditional service management"
336+
fi
337+
(
338+
set -x
339+
$sh_c 'service docker start'
340+
)
341+
# Try to enable service on boot (distribution-specific commands)
342+
if command_exists chkconfig; then
343+
# RHEL/CentOS/Fedora legacy systems
344+
(
345+
set -x
346+
$sh_c 'chkconfig docker on'
347+
)
348+
elif command_exists update-rc.d; then
349+
# Debian/Ubuntu legacy systems
350+
(
351+
set -x
352+
$sh_c 'update-rc.d docker defaults'
353+
)
354+
fi
355+
if ! is_dry_run; then
356+
>&2 echo "Docker daemon started successfully"
357+
fi
358+
else
359+
# No service management available (container environment)
360+
if ! is_dry_run; then
361+
>&2 echo "Note: Running in a container environment without service management"
362+
>&2 echo "Docker daemon cannot be started automatically in this environment"
363+
>&2 echo "The Docker packages have been installed successfully"
364+
fi
365+
fi
366+
>&2 echo
367+
}
368+
283369
echo_docker_as_nonroot() {
284370
if is_dry_run; then
285371
return
@@ -582,6 +668,9 @@ do_install() {
582668
fi
583669
$sh_c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq install $pkgs >/dev/null"
584670
)
671+
if [ "$AUTOSTART" = "1" ]; then
672+
start_docker_daemon
673+
fi
585674
echo_docker_as_nonroot
586675
exit 0
587676
;;
@@ -689,6 +778,9 @@ do_install() {
689778
fi
690779
$sh_c "$pkg_manager $pkg_manager_flags install $pkgs"
691780
)
781+
if [ "$AUTOSTART" = "1" ]; then
782+
start_docker_daemon
783+
fi
692784
echo_docker_as_nonroot
693785
exit 0
694786
;;

0 commit comments

Comments
 (0)