Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
FROM ubuntu:14.04.2
MAINTAINER Bryan Latten <latten@adobe.com>

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

# Used with alternative CMD (worker.sh), leverages supervisor to maintain long-running processes
ENV CONTAINER_ROLE=web

# Install pre-reqs, security updates
RUN apt-get update && \
apt-get upgrade -yq && \
apt-get -yq install \
openssl=1.0.1f-1ubuntu2.15 \
ca-certificates=20141019ubuntu0.14.04.1 \
software-properties-common \
supervisor \
nano

# Install latest nginx-stable
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ To inject things into the runtime process, add shell scripts (ending in .sh) int

- If script terminates with a non-zero exit code, container will stop, terminating with the script's exit code, unless...
- 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


### Long-running processes (workers)

`docker run {image_id} /worker.sh 3 /bin/binary -parameters -that -binary -receives`

Runs 3 copies of `/bin/binary` that receives any arguments as parameters
31 changes: 31 additions & 0 deletions container/root/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

RUN_SCRIPTS=/run.d
STATUS=0

# Run shell scripts (ending in .sh) in run.d directory

# When .sh run scripts fail (exit non-zero), container run will fail
# NOTE: if a .sh script exits with 99, this is a stop signal, container must exit cleanly

for file in $RUN_SCRIPTS/*.sh; do

echo "[init] executing ${file}"

/bin/bash $file

STATUS=$? # Captures exit code from script that was run

if [[ $STATUS == $SIGNAL_BUILD_STOP ]]
then
echo "[init] exit signalled - ${file}"
exit $STATUS
fi

if [[ $STATUS != 0 ]]
then
echo "[init] failed executing - ${file}"
exit $STATUS
fi

done
2 changes: 1 addition & 1 deletion container/root/run.d/10-nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ CONFIG_SERVER=/etc/nginx/nginx.conf
echo '[nginx] setting sensible defaults'

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

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


echo '[nginx] piping logs to STDOUT'

# Set access/error log, and use them as a placeholder for injecting log_format key
Expand Down
40 changes: 15 additions & 25 deletions container/root/run.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
#!/bin/bash
RUN_SCRIPTS=/run.d
STATUS=0

# Run shell scripts (ending in .sh) in run.d directory
# Begin startup sequence
/init.sh

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

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

echo "[run.d] executing ${file}"

/bin/bash $file

STATUS=$? # Captures exit code from script that was run

if [[ $STATUS == 99 ]]
then
echo "[run.d] exit signalled - ${file}"
exit # Exit cleanly
fi

if [[ $STATUS != 0 ]]
then
echo "[run.d] failed executing - ${file}"
exit $STATUS
fi

done
if [[ $STATUS != 0 ]]
then
echo "[run] failed to init"
exit $STATUS
fi

# Primary command - starting webserver
echo "[nginx] start (foreground)"
exec /usr/sbin/nginx -g "daemon off;"
57 changes: 57 additions & 0 deletions container/root/worker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Entrypoint for utilizing as a worker pool instead of a web server
# Based on configuration, can run multiple instances of a single worker process

SUPERVISOR_CONF=/etc/supervisor/conf.d/worker.conf

# Signal to init processes to avoid any webserver startup
export CONTAINER_ROLE='worker'

# Begin startup sequence
/init.sh

STATUS=$? # Captures exit code from script that was run

# TODO this exit code detection is also present in run.sh, needs to be combined
if [[ $STATUS == $SIGNAL_BUILD_STOP ]]
then
echo "[worker] container exit requested"
exit # Exit cleanly
fi

if [[ $STATUS != 0 ]]
then
echo "[worker] failed to init"
exit $STATUS
fi


WORKER_QUANTITY=$1

# Rebuild worker command as properly escaped parameters from shifted input args
# @see http://stackoverflow.com/questions/7535677/bash-passing-paths-with-spaces-as-parameters
shift
WORKER_COMMAND="$@"

if [ -z "$WORKER_COMMAND" ]
then
echo "[worker] command is required, exiting"
exit 1
fi

echo "[worker] command: '${WORKER_COMMAND}' quantity: ${WORKER_QUANTITY}"

echo "\
[program:worker]
command=${WORKER_COMMAND}
process_name=%(program_name)s%(process_num)s
numprocs=${WORKER_QUANTITY}
autorestart=true
redirect_stderr=true
stdout_logfile=/dev/stdout" > $SUPERVISOR_CONF

echo "[worker] entering supervisor"

# Primary command - starting supervisor after writing with worker program config file
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf --nodaemon