@@ -83,6 +83,17 @@ set -e
8383#
8484# $ sudo sh install-docker.sh --setup-repo
8585#
86+ # Automatic Service Start
87+ #
88+ # By default, this script automatically starts the Docker daemon and enables the docker
89+ # service after installation if systemd is used as init.
90+ #
91+ # If you prefer to start the service manually, use the --no-autostart option:
92+ #
93+ # $ sudo sh install-docker.sh --no-autostart
94+ #
95+ # Note: Starting the service requires appropriate privileges to manage system services.
96+ #
8697# ==============================================================================
8798
8899
119130mirror=' '
120131DRY_RUN=${DRY_RUN:- }
121132REPO_ONLY=${REPO_ONLY:- 0}
133+ NO_AUTOSTART=${NO_AUTOSTART:- 0}
122134while [ $# -gt 0 ]; do
123135 case " $1 " in
124136 --channel)
@@ -140,6 +152,9 @@ while [ $# -gt 0 ]; do
140152 REPO_ONLY=1
141153 shift
142154 ;;
155+ --no-autostart)
156+ NO_AUTOSTART=1
157+ ;;
143158 --* )
144159 echo " Illegal option $1 "
145160 ;;
@@ -280,6 +295,29 @@ get_distribution() {
280295 echo " $lsb_dist "
281296}
282297
298+ start_docker_daemon () {
299+ # Use systemctl if available (for systemd-based systems)
300+ if command_exists systemctl; then
301+ is_dry_run || >&2 echo " Using systemd to manage Docker service"
302+ if (
303+ is_dry_run || set -x
304+ $sh_c systemctl enable --now docker.service 2> /dev/null
305+ ); then
306+ is_dry_run || echo " INFO: Docker daemon enabled and started" >&2
307+ else
308+ is_dry_run || echo " WARNING: unable to enable the docker service" >&2
309+ fi
310+ else
311+ # No service management available (container environment)
312+ if ! is_dry_run; then
313+ >&2 echo " Note: Running in a container environment without service management"
314+ >&2 echo " Docker daemon cannot be started automatically in this environment"
315+ >&2 echo " The Docker packages have been installed successfully"
316+ fi
317+ fi
318+ >&2 echo
319+ }
320+
283321echo_docker_as_nonroot () {
284322 if is_dry_run; then
285323 return
@@ -582,6 +620,9 @@ do_install() {
582620 fi
583621 $sh_c " DEBIAN_FRONTEND=noninteractive apt-get -y -qq install $pkgs >/dev/null"
584622 )
623+ if [ " $NO_AUTOSTART " != " 1" ]; then
624+ start_docker_daemon
625+ fi
585626 echo_docker_as_nonroot
586627 exit 0
587628 ;;
@@ -689,6 +730,9 @@ do_install() {
689730 fi
690731 $sh_c " $pkg_manager $pkg_manager_flags install $pkgs "
691732 )
733+ if [ " $NO_AUTOSTART " != " 1" ]; then
734+ start_docker_daemon
735+ fi
692736 echo_docker_as_nonroot
693737 exit 0
694738 ;;
0 commit comments