Skip to content

Commit d7985cd

Browse files
committed
Updated base image and some more functionality
1. Rebased the image on the fedora:31 image 2. Removed all env-var handling code as its basically superseded by the systemd `PassEnvironment` option that can be specified on a per-unit-file basis 3. Made systemd and journald output go to `/dev/console` which should be collected by the container engine automatically 4. Made a new service unit file that tries to run the arguments given to the container as commands after all systemd services have started and exit the container once those commands are done while returning an appropriate return value. 5. It is possible to have environment variables passed to the invoked commands by setting variable names in the `ARGS_ENV_INCLUDE` variable either when launching the container or when building derived containers Note: The CentOs version upgrade is required, among other things, because the `systemd` version in CentOS 7 does not support returning exit codes on exit. Note: Certain versions of Docker have an issue with collecting `/dev/console` properly. See the following for explanation: - systemd/systemd#4262 - moby/moby#27202 - https://bugzilla.redhat.com/show_bug.cgi?id=1373780 This image also include a workaround for the following Podman issue: - containers/podman#4625 Signed-off-by: Barak Korren <bkorren@redhat.com>
1 parent 93effd3 commit d7985cd

File tree

9 files changed

+116
-45
lines changed

9 files changed

+116
-45
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

Dockerfile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
FROM centos:7.6.1810
1+
FROM fedora:31
22

33
ENV container docker
4+
VOLUME ["/sys/fs/cgroup"]
5+
46
RUN ( \
57
cd /lib/systemd/system/sysinit.target.wants/; \
68
for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done \
@@ -13,9 +15,13 @@ RUN ( \
1315
rm -f /lib/systemd/system/basic.target.wants/*; \
1416
rm -f /lib/systemd/system/anaconda.target.wants/*; \
1517
touch /etc/sysconfig/network
16-
COPY sbin/export_environment.sh /sbin/export_environment
17-
COPY systemd/* /etc/systemd/system/
18-
RUN chmod +x /sbin/export_environment
19-
RUN systemctl enable export-environment.service
20-
VOLUME ["/sys/fs/cgroup"]
21-
CMD ["/usr/sbin/init"]
18+
19+
COPY sbin/ /sbin/
20+
COPY etc/ /etc/
21+
22+
ENV ARGS_EXPORT_PATH=/etc/ci-container.args
23+
# A list of variables to be made available in the environment the given command
24+
# line args run in
25+
ENV ARGS_ENV_INCLUDE="ARGS_EXPORT_PATH"
26+
27+
ENTRYPOINT ["/sbin/entrypoint.sh"]

etc/systemd/journald.conf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This file is part of systemd.
2+
#
3+
# systemd is free software; you can redistribute it and/or modify it
4+
# under the terms of the GNU Lesser General Public License as published by
5+
# the Free Software Foundation; either version 2.1 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# Entries in this file show the compile time defaults.
9+
# You can change settings by editing this file.
10+
# Defaults can be restored by simply deleting this file.
11+
#
12+
# See journald.conf(5) for details.
13+
14+
[Journal]
15+
#Storage=auto
16+
#Compress=yes
17+
#Seal=yes
18+
#SplitMode=uid
19+
#SyncIntervalSec=5m
20+
#RateLimitIntervalSec=30s
21+
#RateLimitBurst=10000
22+
#SystemMaxUse=
23+
#SystemKeepFree=
24+
#SystemMaxFileSize=
25+
#SystemMaxFiles=100
26+
#RuntimeMaxUse=
27+
#RuntimeKeepFree=
28+
#RuntimeMaxFileSize=
29+
#RuntimeMaxFiles=100
30+
#MaxRetentionSec=
31+
#MaxFileSec=1month
32+
#ForwardToSyslog=no
33+
#ForwardToKMsg=no
34+
ForwardToConsole=yes
35+
#ForwardToWall=yes
36+
TTYPath=/dev/console
37+
#MaxLevelStore=debug
38+
#MaxLevelSyslog=debug
39+
#MaxLevelKMsg=notice
40+
MaxLevelConsole=debug
41+
#MaxLevelWall=emerg
42+
#LineMax=48K
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Unit]
2+
Description=Run container command
3+
4+
[Service]
5+
Type=oneshot
6+
PassEnvironment=ARGS_EXPORT_PATH $ARGS_ENV_INCLUDE
7+
# Remove this service file so that if the container layer is committed, the
8+
# resulting image will not contain the given command information
9+
ExecStartPre=-/usr/bin/systemctl disable --no-reload run-args.service
10+
ExecStartPre=-/usr/bin/rm -f /etc/systemd/system/run-args.service
11+
ExecStart=/sbin/run_args.sh ${ARGS_EXPORT_PATH}
12+
13+
[Install]
14+
WantedBy=multi-user.target

sbin/entrypoint.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash -e
2+
# Read command-line arguments and store them in a file to be used later
3+
#
4+
if [[ $# -gt 0 ]] && [[ $1 ]]; then
5+
# Podman seems to have an issue where `podman commit` cannot create images
6+
# without a CMD setting, and adding `--change='CMD []'` results in the
7+
# command being an array with a single string in it. Therefor we detect that
8+
# particular case above and treat it as if a command was not given
9+
echo "Got $# command-line arguments, enabling run-args service"
10+
printf '%s\n' "$@" > "$ARGS_EXPORT_PATH"
11+
# Update list of variables that systemd will pass to invoked process on the
12+
# fly. Unfortunately this dirty `sed` is the only way to do that
13+
#
14+
# We create the *.service file from in *.service.in file rather then making
15+
# the change to the file in-place, so that the change can be undone without
16+
# leaving overlayfs records behind
17+
#
18+
/usr/bin/sed -re "s/\\\$ARGS_ENV_INCLUDE/$ARGS_ENV_INCLUDE/" \
19+
/etc/systemd/system/run-args.service.in \
20+
> /etc/systemd/system/run-args.service \
21+
# Enable service to run the arguments
22+
systemctl enable run-args.service
23+
fi
24+
25+
exec /usr/sbin/init

sbin/export_environment.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

sbin/run_args.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
# run_args.sh - Run command from a given file
3+
#
4+
(
5+
# Run in a subshell so -e only applies to the commands in parantheses
6+
set -e
7+
CMD_FILE="${1:?Args file not passed to run_args.sh}"
8+
if ! [[ -r "$CMD_FILE" ]]; then
9+
echo "run_args.sh: Args file: '$CMD_FILE' not found"
10+
fi
11+
mapfile -t CMD < "$CMD_FILE"
12+
# remove the file since we don't need it anymore
13+
rm -f "$CMD_FILE" || :
14+
# Finally run the command
15+
"${CMD[@]}"
16+
)
17+
# Since this script is not running with -e the command below will always run
18+
systemctl exit $?
19+
# Exit with 0 so systemd doesn't think the service had failed
20+
exit 0

systemd/export-environment.service

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)