Skip to content

Commit 40f45c0

Browse files
authored
simplify service start (#1)
- unconditionally try "systemctl enable --now" if systemctl is found, but print a warning if it failed. - tweak some dry-run cases - remove fallbacks for legacy systems without systemd - change AUTOSTART to NO_AUTOSTART With this change; DRY_RUN=1 sh -c ./install.sh dnf -y -q --setopt=install_weak_deps=False install dnf-plugins-core rm -f /etc/yum.repos.d/docker-ce.repo /etc/yum.repos.d/docker-ce-staging.repo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo dnf makecache dnf -y -q --best install docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin docker-model-plugin systemctl enable --now docker.service Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent aa7ba22 commit 40f45c0

File tree

1 file changed

+13
-68
lines changed

1 file changed

+13
-68
lines changed

install.sh

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ set -e
8686
# Automatic Service Start
8787
#
8888
# By default, this script automatically starts the Docker daemon and enables the docker
89-
# service after installation using the appropriate service management system
90-
# (systemd, etc.) for your distribution.
89+
# service after installation if systemd is used as init.
9190
#
9291
# If you prefer to start the service manually, use the --no-autostart option:
9392
#
@@ -131,7 +130,7 @@ fi
131130
mirror=''
132131
DRY_RUN=${DRY_RUN:-}
133132
REPO_ONLY=${REPO_ONLY:-0}
134-
AUTOSTART=${AUTOSTART:-1}
133+
NO_AUTOSTART=${NO_AUTOSTART:-0}
135134
while [ $# -gt 0 ]; do
136135
case "$1" in
137136
--channel)
@@ -154,7 +153,7 @@ while [ $# -gt 0 ]; do
154153
shift
155154
;;
156155
--no-autostart)
157-
AUTOSTART=0
156+
NO_AUTOSTART=1
158157
;;
159158
--*)
160159
echo "Illegal option $1"
@@ -296,72 +295,18 @@ get_distribution() {
296295
echo "$lsb_dist"
297296
}
298297

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-
309298
start_docker_daemon() {
310-
>&2 echo
311-
>&2 echo "Starting and enabling Docker daemon service..."
312-
313299
# Use systemctl if available (for systemd-based systems)
314300
if command_exists systemctl; then
315-
if ! is_dry_run; then
316-
if has_systemd; then
317-
>&2 echo "Using systemd to manage Docker service"
318-
else
319-
>&2 echo "Configuring Docker service for systemd (not running as init)"
320-
fi
321-
fi
322-
(
323-
set -x
324-
# Use --now to start and enable simultaneously when systemd is running
325-
if has_systemd; then
326-
$sh_c 'systemctl enable --now docker'
327-
else
328-
# Only enable for boot when systemd is not running (e.g., containers)
329-
# This supports image portability - service will start when booted with systemd
330-
$sh_c 'systemctl enable docker'
331-
fi
332-
)
333-
if ! is_dry_run; then
334-
if has_systemd; then
335-
>&2 echo "Docker daemon started and enabled"
336-
else
337-
>&2 echo "Docker service configured (will start on boot when systemd runs)"
338-
fi
339-
fi
340-
elif command_exists service; then
341-
# Fallback for older systems without systemd
342-
if ! is_dry_run; then
343-
>&2 echo "Using traditional service management"
344-
fi
345-
(
346-
set -x
347-
$sh_c 'service docker start'
348-
)
349-
# Try to enable service on boot (distribution-specific commands)
350-
if command_exists chkconfig; then
351-
# RHEL/CentOS/Fedora legacy systems
352-
(
353-
set -x
354-
$sh_c 'chkconfig docker on'
355-
)
356-
elif command_exists update-rc.d; then
357-
# Debian/Ubuntu legacy systems
358-
(
359-
set -x
360-
$sh_c 'update-rc.d docker defaults'
361-
)
362-
fi
363-
if ! is_dry_run; then
364-
>&2 echo "Docker daemon started successfully"
301+
is_dry_run || >&2 echo "Using systemd to manage Docker service"
302+
# Use --now to start and enable simultaneously when systemd is running
303+
if (
304+
is_dry_run || set -x
305+
$sh_c systemctl enable --now docker.service 2>/dev/null
306+
); then
307+
is_dry_run || echo "INFO: Docker daemon enabled and started" >&2
308+
else
309+
is_dry_run || echo "WARNING: unable to enable the docker service" >&2
365310
fi
366311
else
367312
# No service management available (container environment)
@@ -676,7 +621,7 @@ do_install() {
676621
fi
677622
$sh_c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq install $pkgs >/dev/null"
678623
)
679-
if [ "$AUTOSTART" = "1" ]; then
624+
if [ "$NO_AUTOSTART" != "1" ]; then
680625
start_docker_daemon
681626
fi
682627
echo_docker_as_nonroot

0 commit comments

Comments
 (0)