Skip to content

Commit 09a41bf

Browse files
author
Bryan Latten
committed
Merge pull request #11 from bryanlatten/feature-supervisor
Dockerfile: adding a worker CMD entrypoint
2 parents 8e5a2b3 + 0487ccf commit 09a41bf

File tree

6 files changed

+116
-26
lines changed

6 files changed

+116
-26
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
FROM ubuntu:14.04.2
22
MAINTAINER Bryan Latten <[email protected]>
33

4+
# Use in multi-phase builds, when an init process requests for the container to gracefully exit, so that it may be committed
45
ENV SIGNAL_BUILD_STOP 99
56

7+
# Used with alternative CMD (worker.sh), leverages supervisor to maintain long-running processes
8+
ENV CONTAINER_ROLE=web
9+
610
# Install pre-reqs, security updates
711
RUN apt-get update && \
812
apt-get upgrade -yq && \
913
apt-get -yq install \
1014
openssl=1.0.1f-1ubuntu2.15 \
1115
ca-certificates=20141019ubuntu0.14.04.1 \
1216
software-properties-common \
17+
supervisor \
1318
nano
1419

1520
# Install latest nginx-stable

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ To inject things into the runtime process, add shell scripts (ending in .sh) int
1717

1818
- If script terminates with a non-zero exit code, container will stop, terminating with the script's exit code, unless...
1919
- If script terminates with exit code of $SIGNAL_BUILD_STOP (99), this will signal the container to stop cleanly. This can be used for multi-stage builds that can be committed
20+
21+
22+
### Long-running processes (workers)
23+
24+
`docker run {image_id} /worker.sh 3 /bin/binary -parameters -that -binary -receives`
25+
26+
Runs 3 copies of `/bin/binary` that receives any arguments as parameters

container/root/init.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
RUN_SCRIPTS=/run.d
4+
STATUS=0
5+
6+
# Run shell scripts (ending in .sh) in run.d directory
7+
8+
# When .sh run scripts fail (exit non-zero), container run will fail
9+
# NOTE: if a .sh script exits with 99, this is a stop signal, container must exit cleanly
10+
11+
for file in $RUN_SCRIPTS/*.sh; do
12+
13+
echo "[init] executing ${file}"
14+
15+
/bin/bash $file
16+
17+
STATUS=$? # Captures exit code from script that was run
18+
19+
if [[ $STATUS == $SIGNAL_BUILD_STOP ]]
20+
then
21+
echo "[init] exit signalled - ${file}"
22+
exit $STATUS
23+
fi
24+
25+
if [[ $STATUS != 0 ]]
26+
then
27+
echo "[init] failed executing - ${file}"
28+
exit $STATUS
29+
fi
30+
31+
done

container/root/run.d/10-nginx.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ CONFIG_SERVER=/etc/nginx/nginx.conf
55
echo '[nginx] setting sensible defaults'
66

77
# Configure nginx to use as many workers as there are cores for the running container
8+
# NOTE: worker_processes is only replaced when *not* set to auto
89
sed -i "s/worker_processes [0-9]\+/worker_processes $(nproc)/" $CONFIG_SERVER
910
sed -i "s/worker_connections [0-9]\+/worker_connections 1024/" $CONFIG_SERVER
1011

1112
# Uncomment prod-level tokens (none)
1213
sed -i "s/\#\ server_tokens/server_tokens/" $CONFIG_SERVER
1314

14-
1515
echo '[nginx] piping logs to STDOUT'
1616

1717
# Set access/error log, and use them as a placeholder for injecting log_format key

container/root/run.sh

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
#!/bin/bash
2-
RUN_SCRIPTS=/run.d
3-
STATUS=0
42

5-
# Run shell scripts (ending in .sh) in run.d directory
3+
# Begin startup sequence
4+
/init.sh
65

7-
# When .sh run scripts fail (exit non-zero), container run will fail
8-
# NOTE: if a .sh script exits with 99, this is our stop signal, container will exit cleanly
6+
STATUS=$? # Captures exit code from script that was run
97

10-
for file in $RUN_SCRIPTS/*.sh; do
8+
# TODO this exit code detection is also present in worker.sh, needs to be combined
9+
if [[ $STATUS == $SIGNAL_BUILD_STOP ]]
10+
then
11+
echo "[run] container exit requested"
12+
exit # Exit cleanly
13+
fi
1114

12-
echo "[run.d] executing ${file}"
13-
14-
/bin/bash $file
15-
16-
STATUS=$? # Captures exit code from script that was run
17-
18-
if [[ $STATUS == 99 ]]
19-
then
20-
echo "[run.d] exit signalled - ${file}"
21-
exit # Exit cleanly
22-
fi
23-
24-
if [[ $STATUS != 0 ]]
25-
then
26-
echo "[run.d] failed executing - ${file}"
27-
exit $STATUS
28-
fi
29-
30-
done
15+
if [[ $STATUS != 0 ]]
16+
then
17+
echo "[run] failed to init"
18+
exit $STATUS
19+
fi
3120

21+
# Primary command - starting webserver
3222
echo "[nginx] start (foreground)"
3323
exec /usr/sbin/nginx -g "daemon off;"

container/root/worker.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# Entrypoint for utilizing as a worker pool instead of a web server
4+
# Based on configuration, can run multiple instances of a single worker process
5+
6+
SUPERVISOR_CONF=/etc/supervisor/conf.d/worker.conf
7+
8+
# Signal to init processes to avoid any webserver startup
9+
export CONTAINER_ROLE='worker'
10+
11+
# Begin startup sequence
12+
/init.sh
13+
14+
STATUS=$? # Captures exit code from script that was run
15+
16+
# TODO this exit code detection is also present in run.sh, needs to be combined
17+
if [[ $STATUS == $SIGNAL_BUILD_STOP ]]
18+
then
19+
echo "[worker] container exit requested"
20+
exit # Exit cleanly
21+
fi
22+
23+
if [[ $STATUS != 0 ]]
24+
then
25+
echo "[worker] failed to init"
26+
exit $STATUS
27+
fi
28+
29+
30+
WORKER_QUANTITY=$1
31+
32+
# Rebuild worker command as properly escaped parameters from shifted input args
33+
# @see http://stackoverflow.com/questions/7535677/bash-passing-paths-with-spaces-as-parameters
34+
shift
35+
WORKER_COMMAND="$@"
36+
37+
if [ -z "$WORKER_COMMAND" ]
38+
then
39+
echo "[worker] command is required, exiting"
40+
exit 1
41+
fi
42+
43+
echo "[worker] command: '${WORKER_COMMAND}' quantity: ${WORKER_QUANTITY}"
44+
45+
echo "\
46+
[program:worker]
47+
command=${WORKER_COMMAND}
48+
process_name=%(program_name)s%(process_num)s
49+
numprocs=${WORKER_QUANTITY}
50+
autorestart=true
51+
redirect_stderr=true
52+
stdout_logfile=/dev/stdout" > $SUPERVISOR_CONF
53+
54+
echo "[worker] entering supervisor"
55+
56+
# Primary command - starting supervisor after writing with worker program config file
57+
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf --nodaemon

0 commit comments

Comments
 (0)