Files
floci/docker/entrypoint.sh
T
Conor Heine 43e9ca18a0 fix(docker): make bind-mounted /var/run/docker.sock work on all hosts (#602)
The published images run as USER 1001 with primary group root. On Docker
Desktop (macOS/Windows) /var/run/docker.sock is owned root:root inside
containers so the floci process can open it via the group-0 bit, but on
native Linux Docker (and Rootless/Podman) the socket is owned by
root:<docker-group> with mode 660 — and the floci user is not a member
of that group. This silently breaks ECR, Lambda, and RDS service
emulation on those hosts: floci's docker-java client gets BindException
/ SocketException and reports CREATE_FAILED on AWS::ECR::Repository.

Fix: ship a docker/entrypoint.sh modeled on the docker-library/postgres
pattern. It starts as root, stats the bind-mounted socket to learn the
host's docker GID, ensures a group with that GID exists, adds the floci
user to it, then `exec gosu floci` re-execs the same script as uid 1001
with the supplementary group set. The 'if id -u == 0' guard means
downstream `user:` overrides in compose files keep working unchanged.

Verified end-to-end on native Linux (Pop!_OS, docker GID 999) using a
minimal CDK bootstrap repro: AWS::ECR::Repository goes from CREATE_FAILED
to CREATE_COMPLETE and the registry:2 sidecar comes up cleanly. pid 1
runs as Uid:1001 Gid:0 Groups:999.

Implementation notes:
- All three Dockerfiles keep their original base images
  (quarkus-micro-image, ubi9-minimal, eclipse-temurin alpine).
- Dockerfile.native gains a tools builder stage that uses the canonical
  Red Hat pattern (`dnf install --installroot`) to install shadow-utils
  into a stagable /toolsroot, which the runtime stage wholesale-copies
  into quarkus-micro-image. Image growth: ~42 MB (quarkus-micro has no
  package manager and lacks groupadd/usermod by itself).
- Dockerfile.native-package adds shadow-utils via microdnf (already had
  the package manager).
- Dockerfile.jvm-package adds shadow + ca-certificates via apk so the
  entrypoint can use the same useradd/groupadd/usermod syntax across
  all three image variants.
- gosu 1.17 is downloaded and SHA256-pinned (rather than GPG-verified
  as Postgres does) to avoid pulling gnupg2 into the runtime layers
  and to sidestep a gpgme dependency conflict in ubi9-minimal.
2026-04-25 14:09:11 -05:00

41 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
# Starts as root, normalizes the bind-mounted Docker socket's group
# ownership so the unprivileged `floci` user can reach it on any host,
# then re-executes this script as `floci` via gosu. The second invocation
# falls through to exec the user's command.
#
# Why: on native Linux Docker, /var/run/docker.sock is owned by
# root:docker with mode 660 and the docker GID varies by distro. On
# Docker Desktop (macOS/Windows) the socket is typically root:root (GID
# 0). Without the group fix-up below, floci (uid 1001, group 0) can open
# the socket on Docker Desktop but not on native Linux — which breaks
# ECR, Lambda, and RDS service emulation there. Discovering the GID at
# runtime handles every host transparently.
set -eu
if [ "$(id -u)" = '0' ]; then
if [ -S /var/run/docker.sock ]; then
sock_gid="$(stat -c '%g' /var/run/docker.sock)"
if [ "$sock_gid" != '0' ]; then
group_name="$(getent group "$sock_gid" | cut -d: -f1)" || group_name=''
if [ -z "$group_name" ]; then
groupadd -g "$sock_gid" docker-host
group_name='docker-host'
fi
usermod -aG "$group_name" floci
fi
fi
# Re-own state dir for the case where a host bind-mount arrives with
# ownership the floci user cannot write to. Ignore errors (read-only
# mounts, unusual filesystems) so the container still starts.
if [ -d /app/data ]; then
chown -R floci:root /app/data 2>/dev/null || true
fi
exec gosu floci "$0" "$@"
fi
exec "$@"