From 7d4c5e207f7e3cf5992ed255e281055c9bc94501 Mon Sep 17 00:00:00 2001 From: Gajesh Bhat Date: Sat, 18 Oct 2025 00:44:47 -0700 Subject: [PATCH] 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: Sebastiaan van Stijn Signed-off-by: Gajesh Bhat --- install.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/install.sh b/install.sh index 424c9763..53d3a246 100755 --- a/install.sh +++ b/install.sh @@ -83,6 +83,17 @@ set -e # # $ sudo sh install-docker.sh --setup-repo # +# Automatic Service Start +# +# By default, this script automatically starts the Docker daemon and enables the docker +# service after installation if systemd is used as init. +# +# If you prefer to start the service manually, use the --no-autostart option: +# +# $ sudo sh install-docker.sh --no-autostart +# +# Note: Starting the service requires appropriate privileges to manage system services. +# # ============================================================================== @@ -119,6 +130,7 @@ fi mirror='' DRY_RUN=${DRY_RUN:-} REPO_ONLY=${REPO_ONLY:-0} +NO_AUTOSTART=${NO_AUTOSTART:-0} while [ $# -gt 0 ]; do case "$1" in --channel) @@ -140,6 +152,9 @@ while [ $# -gt 0 ]; do REPO_ONLY=1 shift ;; + --no-autostart) + NO_AUTOSTART=1 + ;; --*) echo "Illegal option $1" ;; @@ -280,6 +295,29 @@ get_distribution() { echo "$lsb_dist" } +start_docker_daemon() { + # Use systemctl if available (for systemd-based systems) + if command_exists systemctl; then + is_dry_run || >&2 echo "Using systemd to manage Docker service" + if ( + is_dry_run || set -x + $sh_c systemctl enable --now docker.service 2>/dev/null + ); then + is_dry_run || echo "INFO: Docker daemon enabled and started" >&2 + else + is_dry_run || echo "WARNING: unable to enable the docker service" >&2 + fi + else + # No service management available (container environment) + if ! is_dry_run; then + >&2 echo "Note: Running in a container environment without service management" + >&2 echo "Docker daemon cannot be started automatically in this environment" + >&2 echo "The Docker packages have been installed successfully" + fi + fi + >&2 echo +} + echo_docker_as_nonroot() { if is_dry_run; then return @@ -582,6 +620,9 @@ do_install() { fi $sh_c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq install $pkgs >/dev/null" ) + if [ "$NO_AUTOSTART" != "1" ]; then + start_docker_daemon + fi echo_docker_as_nonroot exit 0 ;; @@ -689,6 +730,9 @@ do_install() { fi $sh_c "$pkg_manager $pkg_manager_flags install $pkgs" ) + if [ "$NO_AUTOSTART" != "1" ]; then + start_docker_daemon + fi echo_docker_as_nonroot exit 0 ;;