Skip to content

Instantly share code, notes, and snippets.

@mrjk
Created November 19, 2024 22:58
Show Gist options
  • Save mrjk/0b5035a089152fb303c649ac0012162a to your computer and use it in GitHub Desktop.
Save mrjk/0b5035a089152fb303c649ac0012162a to your computer and use it in GitHub Desktop.
services:
init-container-debian:
restart: "no"
build:
context: .
dockerfile: ./Dockerfile-debian
environment:
# Config
- ENTRYPOINT_XTRACE=false
- ENTRYPOINT_INFINITE=false
# volumes:
init-container-rhel:
restart: "no"
build:
context: .
dockerfile: ./Dockerfile-rhel
environment:
# Config
- ENTRYPOINT_XTRACE=false
- ENTRYPOINT_INFINITE=false
# volumes:
#!/usr/bin/env bash
# Source: https://github.com/kronostechnologies/docker-init-entrypoint/blob/master/entrypoint.sh
set -eu
process_scripts() {
local path=$1
if [[ ! -d $path ]]; then
echo "> Skipping '${path}' empty directory."
else
for script in $(ls $path | sort -n); do
echo "> Running '${path}/${script}'.."
bash "${path}/${script}"
done
fi
}
kill_remaining_process() {
# Gather all PIDs except for pid 1 (entrypoint script) into a
# space separated list, send SIGTERM and wait for those process to finish properly
ps -e > /tmp/ps
local PIDS=`grep -v -E "PID|\s1\s|ps" /tmp/ps | awk 'BEGIN { ORS=" " }; {print $1}'`;
rm /tmp/ps
if [ -n "$PIDS" ]; then
kill -TERM $PIDS
for PID in $PIDS; do
while [[ -d /proc/$PID ]]; do
sleep 0.1
done
done
fi
}
finish() {
trap "" SIGTERM SIGQUIT SIGINT
echo '> Stopping all services..'
process_scripts "${ENTRYPOINT_ROOT}/stop.d"
echo '> Killing remaining process..'
kill_remaining_process
echo '> Shutting down now.'
}
main ()
{
# Prepare env
echo BOOT > /tmp/status
ENTRYPOINT_XTRACE=${ENTRYPOINT_XTRACE:-false}
ENTRYPOINT_INFINITE=${ENTRYPOINT_INFINITE:-false}
if [ -z ${ENTRYPOINT_ROOT+x} ]; then
ENTRYPOINT_ROOT="/docker-entrypoints"
fi
# Prepare trap exit
trap finish SIGTERM SIGQUIT SIGINT
[ "${ENTRYPOINT_XTRACE}" == "false" ] || set -x
# Ensure init process has been run
echo '> Starting init processes..'
process_scripts "${ENTRYPOINT_ROOT}/init.d"
echo INIT > /tmp/status
if [[ -d "${ENTRYPOINT_ROOT}/start.d" ]]; then
# Start all services
echo '> Starting all services..'
process_scripts "${ENTRYPOINT_ROOT}/start.d"
echo '> Fully Booted.'
echo STARTED > /tmp/status
if [[ -n "${*}" ]]; then
echo "> Executing \`${*}\`"
/bin/bash -c "${*}" &
else
sleep infinity &
fi
wait $!
finish
else
# No services
if [[ "$ENTRYPOINT_INFINITE" == true ]]; then
echo '> One shot container asked to run infinitely, sleeping..'
sleep infinity &
wait $!
finish
else
echo '> One shot container fully terminated.'
fi
fi
}
main "$@"
# Common part
# ================
FROM debian:12 AS base
# Configuration
ENV ENTRYPOINT_INFINITE=false
ENV ENTRYPOINT_XTRACE=false
ENV ENTRYPOINT_ROOT=/docker-entrypoints
# Install dependencies
RUN apt update && apt install -y procps gettext \
&& apt clean all \
&& rm -rf /var/cache/apt
# Provide a simple but generic init script
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
CMD ["/docker-entrypoint.sh"]
# Healthcheck
HEALTHCHECK --interval=1s --timeout=1s --retries=20 \
CMD grep -q STARTED /tmp/status
# Common part
# ================
FROM rockylinux:9.3 AS base
# Configuration
ENV ENTRYPOINT_INFINITE=false
ENV ENTRYPOINT_XTRACE=false
ENV ENTRYPOINT_ROOT=/docker-entrypoints
# Install dependencies
RUN dnf install -y procps gettext \
&& dnf clean all \
&& rm -rf /var/cache/yum
# Provide a simple but generic init script
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
CMD ["/docker-entrypoint.sh"]
# Healthcheck
HEALTHCHECK --interval=1s --timeout=1s --retries=20 \
CMD grep -q STARTED /tmp/status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment