From d8dde9cde23fd5a8cb019decafe6bc45dc392127 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 19 Jan 2024 16:07:07 +0100 Subject: [PATCH] confd: add support for custom container ENTRYPOINT This was a tough nut to crack. Turns out the trick to changing the ENTRYPOINT is to set --entrypoint=command and then call 'podman create ... command args'. Not entirely obvious since the documented approach is to use a JSON array as the argument: podman create --entrypoint='["command", "args" ]'. Admittedly, encoding this in C to transfer it via a POSIX shell script to the command line, is not the easiest task I've undertaken. So I gave up and found this workaround. Worth noting, however, is that one *must* set `--entrypoint`, it is not enough to just append the command to the 'podman create' command line. Due to differences in docker and podman, we cannot supply the full args to an alternate entrypoint command. But for the command to actually run we need to override the image's ENTRYPOINT and send the command and args as the last arguments on the command line to podman create. Signed-off-by: Joachim Wiberg --- board/common/rootfs/usr/sbin/container | 9 +++++++-- src/confd/src/infix-containers.c | 6 ++++++ src/confd/yang/infix-containers@2023-12-14.yang | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/board/common/rootfs/usr/sbin/container b/board/common/rootfs/usr/sbin/container index 5bd21106..72ed7151 100755 --- a/board/common/rootfs/usr/sbin/container +++ b/board/common/rootfs/usr/sbin/container @@ -43,9 +43,10 @@ create() fi # --syslog --log-level info - log "Calling \"podman create --name $name --conmon-pidfile=$pidfn $hostname $env $network $port $args $image\"" + log "Calling \"podman create --name $name --conmon-pidfile=$pidfn $hostname $entrypoint $env $network $port $args $image $*\"" set -x - if podman create --name "$name" --conmon-pidfile="$pidfn" $hostname $env $network $port $args "$image"; then + # shellcheck disable=SC2048 + if podman create --name "$name" --conmon-pidfile="$pidfn" $hostname $entrypoint $env $network $port $args "$image" $*; then log "Successfully created container $name from $image" rm -f "/run/containers/env/${name}.env" initctl show "container:$name" |grep -q manual:yes || initctl -bq start "container:$name" @@ -83,6 +84,7 @@ options: --dns NAMESERVER Set nameserver(s) when creating a container --dns-search LIST Set host lookup search list when creating container -e, --env FILE Environment variables when creating container + --entrypoint Disable container image's ENTRYPOINT, run cmd + arg -f, --force Force operation, e.g. remove -h, --help Show this help text --hostname NAME Set hostname when creating container @@ -122,6 +124,9 @@ while [ "$1" != "" ]; do shift env="$env --env-file=$1" ;; + --entrypoint) + entrypoint="--entrypoint=\"\"" + ;; -f | --force) force="-f" ;; diff --git a/src/confd/src/infix-containers.c b/src/confd/src/infix-containers.c index c24e8c6a..98019e67 100644 --- a/src/confd/src/infix-containers.c +++ b/src/confd/src/infix-containers.c @@ -86,8 +86,14 @@ static int add(const char *name, struct lyd_node *cif) if (lydx_is_enabled(cif, "host-network")) fprintf(fp, " --net host"); + if ((string = lydx_get_cattr(cif, "entrypoint"))) + fprintf(fp, " --entrypoint"); + fprintf(fp, " create %s %s", name, image); + if ((string = lydx_get_cattr(cif, "entrypoint"))) + fprintf(fp, " %s", string); + fprintf(fp, "\n"); fchmod(fileno(fp), 0700); fclose(fp); diff --git a/src/confd/yang/infix-containers@2023-12-14.yang b/src/confd/yang/infix-containers@2023-12-14.yang index c86f0f33..80ab3e51 100644 --- a/src/confd/yang/infix-containers@2023-12-14.yang +++ b/src/confd/yang/infix-containers@2023-12-14.yang @@ -83,6 +83,11 @@ module infix-containers { } } + leaf entrypoint { + description "Override the default ENTRYPOINT from the image."; + type string; + } + leaf hostname { description "Sets the container host name that is available inside the container."; type inet:domain-name;