Skip to content

Commit c71834f

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 <[email protected]>
1 parent a00c5c3 commit c71834f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

install.sh

Lines changed: 85 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,69 @@ 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+
if has_systemd; then
314+
# Use systemd for modern distributions (most current Linux distributions)
315+
if ! is_dry_run; then
316+
>&2 echo "Using systemd to manage Docker service"
317+
fi
318+
(
319+
set -x
320+
$sh_c 'systemctl start docker'
321+
$sh_c 'systemctl enable docker'
322+
)
323+
if ! is_dry_run; then
324+
>&2 echo "Docker daemon started and enabled successfully"
325+
fi
326+
else
327+
# Fallback for older systems without systemd (legacy init systems)
328+
if ! is_dry_run; then
329+
>&2 echo "Using traditional service management"
330+
fi
331+
(
332+
set -x
333+
$sh_c 'service docker start'
334+
)
335+
# Try to enable service on boot (distribution-specific commands)
336+
if command_exists chkconfig; then
337+
# RHEL/CentOS/Fedora legacy systems
338+
(
339+
set -x
340+
$sh_c 'chkconfig docker on'
341+
)
342+
elif command_exists update-rc.d; then
343+
# Debian/Ubuntu legacy systems
344+
(
345+
set -x
346+
$sh_c 'update-rc.d docker defaults'
347+
)
348+
else
349+
# Unknown init system - warn the user
350+
if ! is_dry_run; then
351+
>&2 echo "Warning: Could not enable Docker service to start on boot"
352+
>&2 echo "Please manually configure Docker to start on boot for your system"
353+
fi
354+
fi
355+
if ! is_dry_run; then
356+
>&2 echo "Docker daemon started successfully"
357+
fi
358+
fi
359+
>&2 echo
360+
}
361+
283362
echo_docker_as_nonroot() {
284363
if is_dry_run; then
285364
return
@@ -582,6 +661,9 @@ do_install() {
582661
fi
583662
$sh_c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq install $pkgs >/dev/null"
584663
)
664+
if [ "$AUTOSTART" = "1" ]; then
665+
start_docker_daemon
666+
fi
585667
echo_docker_as_nonroot
586668
exit 0
587669
;;
@@ -689,6 +771,9 @@ do_install() {
689771
fi
690772
$sh_c "$pkg_manager $pkg_manager_flags install $pkgs"
691773
)
774+
if [ "$AUTOSTART" = "1" ]; then
775+
start_docker_daemon
776+
fi
692777
echo_docker_as_nonroot
693778
exit 0
694779
;;

0 commit comments

Comments
 (0)