-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-systemd-entrypoint
More file actions
executable file
·77 lines (66 loc) · 3.25 KB
/
docker-systemd-entrypoint
File metadata and controls
executable file
·77 lines (66 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
set -e
#
# This script performs some magic to allow Docker container "Commands" to be run
# as a process other than 1 so that SystemD can be running in the background.
#
# Currently, if CMD runs successfully, the container stays running with SystemD.
# If CMD fails, it triggers the "shutdown" command to exit the container.
#
# The DOCKER_COMMAND is the actual Docker CMD used for this container image.
# $@ is every argument and option passed to the entrypoint (which is the same way CMD/ENTRPOINT work)
DOCKER_COMMAND=${@}
SYSTEM_STATUS_COMMAND=${SYSTEM_STATUS_COMMAND:-systemctl status --no-pager}
WELCOME_MESSAGE="Enjoy the DevShop Containers!"
PATH="$DEVSHOP_PATH/bin:$DEVSHOP_PATH/scripts:$PATH"
export PATH
echo "Hi! I'm docker-systemd-entrypoint."
echo "In a few seconds, I'm going to launch the Docker Command you specified as a new process."
echo "Then, I'm going to pass my process off to SystemD!"
echo "That way, you can specify any docker CMD but it will run inside a container with SystemD running properly."
echo "---------------------"
echo "Environment Variables"
echo "---------------------"
echo "INIT_COMMAND: $INIT_COMMAND"
echo "DOCKER_COMMAND: $DOCKER_COMMAND"
echo "DEVSHOP_DOCKER_COMMAND_RUN: $DEVSHOP_DOCKER_COMMAND_RUN"
echo "SYSTEM_STATUS_COMMAND: $SYSTEM_STATUS_COMMAND"
echo "PATH: $PATH"
echo "-----------------"
if [ ! -z "$DOCKER_COMMAND" ]; then
# This launches a group of commands in a new process:
# Sleep for 3 seconds, to allow INIT_COMMAND to launch.
# Run $DOCKER_COMMAND, and exit with it's exit code.
(( \
sleep ${INIT_COMMAND_WAIT:-3} && \
echo "System Status before running DOCKER_COMMAND: " && $SYSTEM_STATUS_COMMAND &&
echo "Running Docker Command '$DOCKER_COMMAND' ..." && $DOCKER_COMMAND &&
# If not successful, kill the container.
# If successful, run DEVSHOP_DOCKER_COMMAND_RUN and mention that system D continues.
echo "The DOCKER_COMMAND completed. (exit 0) Continuing on to DEVSHOP_DOCKER_COMMAND_RUN." && \
echo "System Status after running Docker command: " && $SYSTEM_STATUS_COMMAND && \
(
echo "Running DEVSHOP_DOCKER_COMMAND_RUN Command '$DEVSHOP_DOCKER_COMMAND_RUN' ..." && $DEVSHOP_DOCKER_COMMAND_RUN &&
echo "The DEVSHOP_DOCKER_COMMAND_RUN completed. (exit 0) Continuing on."
) || (
echo "The DEVSHOP_DOCKER_COMMAND_RUN failed. (exit $?) Continuing on. If you wish to exit on failure, set docker command."
)
) || ( \
echo "The DOCKER_COMMAND failed. (exit $?) Triggering a shutdown to end INIT_COMMAND ..." && \
shutdown
)) &
DOCKER_COMMAND_PID=$!
echo "The Docker Command is about to start with PID $DOCKER_COMMAND_PID ..."
else
echo "There is no Docker command set. If you set one, it would run after INIT_COMMAND in a separate process."
# Let INIT COMMAND run, then wait 3 seconds, then show some messages and check system status.
( \
echo "Waiting 3 seconds for systemd to start..." &&
sleep ${INIT_COMMAND_WAIT:-3} && \
echo "System Status: " && $SYSTEM_STATUS_COMMAND && \
echo $WELCOME_MESSAGE \
) &
fi
# @TODO: Detect an unprivileged container or missing cgroup volume and inform the user.
echo "Running $INIT_COMMAND ..."
exec "$INIT_COMMAND"