Skip to content

Commit

Permalink
added systemd and openrc containers
Browse files Browse the repository at this point in the history
  • Loading branch information
heywoodlh committed Oct 30, 2024
1 parent 8ca1dba commit 9496db8
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 0 deletions.
11 changes: 11 additions & 0 deletions openrc/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM docker.io/alpine

RUN apk add --no-cache openrc
RUN mkdir -p /run/openrc && touch /run/openrc/softlevel
RUN sed -i 's/VSERVER/DOCKER/Ig' /lib/rc/sh/init.sh

# configured /etc/rc.conf for docker
RUN sed -i '/getty/d' /etc/inittab

COPY openrc.sh /openrc.sh
ENTRYPOINT ["/openrc.sh"]
3 changes: 3 additions & 0 deletions openrc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Credit

https://github.com/robertdebock/docker-alpine-openrc/tree/master
3 changes: 3 additions & 0 deletions openrc/openrc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/ash
openrc default
exec "$@"
8 changes: 8 additions & 0 deletions openrc/test-openrc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
dir=$(dirname -- "$( readlink -f -- "$0"; )";)
dockerfile="Dockerfile.alpine"
set -ex
echo "=== Testing ${dockerfile} ==="
iid=$(docker build -q -f ${dir}/${dockerfile} ${dir})
docker run -t --rm --privileged $iid ash -c "rc-status && printf 'OpenRC running!\n'"
docker image rm $iid
6 changes: 6 additions & 0 deletions systemd/Dockerfile.archlinux
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM docker.io/heywoodlh/archlinux:latest
RUN pacman -Sy --noconfirm systemd systemd-sysvcompat \
&& pacman -Scc --noconfirm \
&& rm /var/lib/pacman/sync/*
COPY systemd.sh /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
3 changes: 3 additions & 0 deletions systemd/Dockerfile.centos
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM docker.io/centos:latest
COPY systemd.sh /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
7 changes: 7 additions & 0 deletions systemd/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM debian:latest
RUN apt-get update && \
apt-get install -y --no-install-recommends \
systemd systemd-sysv dbus dbus-user-session \
&& rm -rf /var/lib/apt/lists/*
COPY systemd.sh /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
5 changes: 5 additions & 0 deletions systemd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Container images running systemd for testing

# Credit

https://github.com/AkihiroSuda/containerized-systemd
15 changes: 15 additions & 0 deletions systemd/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
operating_systems=("debian" "centos" "archlinux")
dir=$(dirname -- "$( readlink -f -- "$0"; )";)
date_tag=$(date +%Y_%m_snapshot)

for os in "${operating_systems[@]}"
do
arches="amd64,arm64"
# Skip arm64 if archlinux
[[ ${os} == "archlinux" ]] && arches="amd64"
docker buildx build --no-cache --platform "${arches}" --squash -t "heywoodlh/systemd:${os}" -t "docker.io/heywoodlh/systemd:${os}_${date_tag}" -f ${dir}/Dockerfile.${os} . --push
done

# Set default image to Debian
docker buildx build --no-cache --platform "amd64,arm64" --squash -t "heywoodlh/systemd:latest" -t "docker.io/heywoodlh/systemd:${date_tag}" -f ${dir}/Dockerfile.debian . --push
62 changes: 62 additions & 0 deletions systemd/systemd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
set -e
container=docker
export container

if [ $# -eq 0 ]; then
echo >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?'
exit 1
fi

if [ ! -t 0 ]; then
echo >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).'
exit 1
fi

env >/etc/docker-entrypoint-env

cat >/etc/systemd/system/docker-entrypoint.target <<EOF
[Unit]
Description=the target for docker-entrypoint.service
Requires=docker-entrypoint.service systemd-logind.service systemd-user-sessions.service
EOF

quoted_args="$(printf " %q" "${@}")"
echo "${quoted_args}" >/etc/docker-entrypoint-cmd

cat >/etc/systemd/system/docker-entrypoint.service <<EOF
[Unit]
Description=docker-entrypoint.service
[Service]
ExecStart=/bin/bash -exc "source /etc/docker-entrypoint-cmd"
# EXIT_STATUS is either an exit code integer or a signal name string, see systemd.exec(5)
ExecStopPost=/bin/bash -ec "if echo \${EXIT_STATUS} | grep [A-Z] > /dev/null; then echo >&2 \"got signal \${EXIT_STATUS}\"; systemctl exit \$(( 128 + \$( kill -l \${EXIT_STATUS} ) )); else systemctl exit \${EXIT_STATUS}; fi"
StandardInput=tty-force
StandardOutput=inherit
StandardError=inherit
WorkingDirectory=$(pwd)
EnvironmentFile=/etc/docker-entrypoint-env
[Install]
WantedBy=multi-user.target
EOF

systemctl mask systemd-firstboot.service systemd-udevd.service systemd-modules-load.service
systemctl unmask systemd-logind
systemctl enable docker-entrypoint.service

systemd=
if [ -x /lib/systemd/systemd ]; then
systemd=/lib/systemd/systemd
elif [ -x /usr/lib/systemd/systemd ]; then
systemd=/usr/lib/systemd/systemd
elif [ -x /sbin/init ]; then
systemd=/sbin/init
else
echo >&2 'ERROR: systemd is not installed'
exit 1
fi
systemd_args="--show-status=false --unit=docker-entrypoint.target"
echo "$0: starting $systemd $systemd_args"
exec $systemd $systemd_args
12 changes: 12 additions & 0 deletions systemd/test-systemd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
dir=$(dirname -- "$( readlink -f -- "$0"; )";)
dockerfiles=("Dockerfile.centos" "Dockerfile.debian")

set -ex
for dockerfile in "${dockerfiles[@]}"; do
echo "=== Testing ${dockerfile} ==="
iid=$(docker build -q -f ${dir}/${dockerfile} ${dir})
# NOTE: old version of systemd doesn't wait for `systemctl is-system-running --wait`, so we have `until` loop here.
docker run -t --rm --privileged $iid sh -exc "until systemctl is-system-running --wait; do sleep 1; done; systemctl status --no-pager"
docker image rm $iid
done

0 comments on commit 9496db8

Please sign in to comment.