|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This custom entrypoint script is needed for creating a container user with |
| 4 | +# a host's UID/GUI which enables sharing of the files between the container |
| 5 | +# and the host. |
| 6 | +# NOTE: It is important that a non-root user is used because otherwise the |
| 7 | +# Chrome Driver fails with: "User data directory is already in use" |
| 8 | +# https://github.com/SeleniumHQ/selenium/issues/15327#issuecomment-2688613182 |
| 9 | + |
| 10 | +set -e |
| 11 | + |
| 12 | +# Ensure we have the environment variables |
| 13 | +if [ -z "$HOST_UID" ] || [ -z "$HOST_GID" ]; then |
| 14 | + echo "HOST_UID and HOST_GID must be set!" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +echo "html2print/docker: running a Docker container entrypoint." |
| 19 | +echo "html2print/docker: ensuring html2print user with UID=$HOST_UID and GID=$HOST_GID exists" |
| 20 | + |
| 21 | +# Check if a user with this UID already exists (e.g., "ubuntu") |
| 22 | +EXISTING_USER=$(getent passwd "$HOST_UID" | cut -d: -f1) |
| 23 | + |
| 24 | +if [ -n "$EXISTING_USER" ]; then |
| 25 | + echo "error: html2print/docker: detected a wrong user: '$EXISTING_USER'. Ensure that any default users are removed from the Dockerfile. This entrypoint script is supposed to create a new user 'html2print'." |
| 26 | + exit 1 |
| 27 | +else |
| 28 | + # Ensure the group exists. |
| 29 | + EXISTING_GROUP=$(getent group "$HOST_GID" | cut -d: -f1) |
| 30 | + if [ -z "$EXISTING_GROUP" ]; then |
| 31 | + echo "html2print/docker: creating new group html2print with GID=$HOST_GID" |
| 32 | + groupadd -g "$HOST_GID" html2print |
| 33 | + else |
| 34 | + echo "html2print/docker: group with GID=$HOST_GID already exists: $EXISTING_GROUP, reusing it." |
| 35 | + fi |
| 36 | + |
| 37 | + # Create the user. |
| 38 | + echo "html2print/docker: creating new user html2print with UID=$HOST_UID" |
| 39 | + useradd -m -u "$HOST_UID" -g "$HOST_GID" -s /bin/bash html2print |
| 40 | + |
| 41 | + # Give the user root privileges. Useful for debugging. |
| 42 | + echo "html2print ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/html2print |
| 43 | +fi |
| 44 | + |
| 45 | +echo "html2print/docker: show created user info:" |
| 46 | +id html2print |
| 47 | + |
| 48 | +# Run as the correct user. If no command is provided, run a shell. |
| 49 | +if [ $# -eq 0 ]; then |
| 50 | + echo "html2print/docker: no command provided, opening an interactive shell." |
| 51 | + exec gosu html2print /bin/bash |
| 52 | +else |
| 53 | + # Otherwise, run the provided command. |
| 54 | + exec gosu html2print "$@" |
| 55 | +fi |
0 commit comments