Skip to content

Commit dd90d37

Browse files
committed
WIP
1 parent 977d854 commit dd90d37

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

Dockerfile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM ubuntu:24.04
44
RUN apt-get update && apt-get install -y \
55
curl \
66
git \
7+
gosu \
78
python3 \
89
python3-pip \
910
python3-venv \
@@ -18,15 +19,6 @@ RUN wget -q -O google-chrome.deb https://dl.google.com/linux/direct/google-chrom
1819
&& apt-get install -y ./google-chrome.deb \
1920
&& rm google-chrome.deb
2021

21-
# Create a new non-root user and group.
22-
# NOTE: It is important that a non-root user is used because otherwise the
23-
# Chrome Driver fails with: "User data directory is already in use"
24-
# https://github.com/SeleniumHQ/selenium/issues/15327#issuecomment-2688613182
25-
RUN groupadd -r html2print && useradd -r -m -g html2print html2print
26-
27-
# Grant the new user sudo privileges.
28-
RUN echo "html2print ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/html2print
29-
3022
# Create a virtual environment in the user's home directory.
3123
RUN python3 -m venv /opt/venv
3224

@@ -47,9 +39,14 @@ RUN if [ "$HTML2PRINT_SOURCE" = "pypi" ]; then \
4739
fi; \
4840
chmod -R 777 /opt/venv;
4941

50-
USER html2print
42+
# Remove the default 'ubuntu' user.
43+
RUN userdel -r ubuntu 2>/dev/null || true
44+
45+
# Allow updating the UID/GID dynamically at runtime
46+
COPY entrypoint.sh /entrypoint.sh
47+
RUN chmod +x /entrypoint.sh
5148

5249
# Set the working directory to the user's home directory.
5350
WORKDIR /data
5451

55-
ENTRYPOINT ["/bin/bash"]
52+
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Ensure we have the environment variables
5+
if [ -z "$HOST_UID" ] || [ -z "$HOST_GID" ]; then
6+
echo "HOST_UID and HOST_GID must be set!"
7+
exit 1
8+
fi
9+
10+
echo "html2print/docker: Running a Docker container entrypoint."
11+
echo "html2print/docker: Ensuring html2print user with UID=$HOST_UID and GID=$HOST_GID exists"
12+
13+
# Check if a user with this UID already exists (e.g., "ubuntu")
14+
EXISTING_USER=$(getent passwd "$HOST_UID" | cut -d: -f1)
15+
16+
if [ -n "$EXISTING_USER" ]; then
17+
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'."
18+
exit 1
19+
else
20+
# Ensure the group exists.
21+
EXISTING_GROUP=$(getent group "$HOST_GID" | cut -d: -f1)
22+
if [ -z "$EXISTING_GROUP" ]; then
23+
echo "html2print/docker: creating new group html2print with GID=$HOST_GID"
24+
groupadd -g "$HOST_GID" html2print
25+
else
26+
echo "html2print/docker: group with GID=$HOST_GID already exists: $EXISTING_GROUP, reusing it."
27+
fi
28+
29+
# Create the user.
30+
echo "html2print/docker: creating new user html2print with UID=$HOST_UID"
31+
useradd -m -u "$HOST_UID" -g "$HOST_GID" -s /bin/bash html2print
32+
33+
# Give the user root privileges. Useful for debugging.
34+
echo "html2print ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/html2print
35+
fi
36+
37+
# Debugging: Show user info.
38+
id html2print
39+
40+
# Run as the correct user. If no command is provided, run a shell.
41+
if [ $# -eq 0 ]; then
42+
echo "html2print/docker: no command provided, opening an interactive shell."
43+
exec gosu html2print /bin/bash
44+
else
45+
# Otherwise, run the provided command.
46+
exec gosu html2print "$@"
47+
fi

tasks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def run_docker(
296296
command_argument = (
297297
f'/bin/bash -c "{command}"' if command is not None else ""
298298
)
299-
entry_point_argument = '--entrypoint=""' if command_argument else ""
300299

301300
run_invoke(
302301
context,
@@ -305,8 +304,8 @@ def run_docker(
305304
--name html2print
306305
--rm
307306
-it
308-
-v "$(pwd):/data"
309-
{entry_point_argument}
307+
-e HOST_UID=$(id -u) -e HOST_GID=$(id -g)
308+
-v "$(pwd):/data:rw"
310309
{image}
311310
{command_argument}
312311
""",
@@ -326,6 +325,6 @@ def test_docker(context, image: str = "html2print:latest"):
326325
context,
327326
image=image,
328327
command=(
329-
"cd tests/integration/01_hello_world && html2print print --debug index.html /data/output/index.pdf && cat /tmp/chromedriver.log"
328+
"pwd; whoami; echo $HOME; id -u; cd tests/integration/01_hello_world && html2print print index.html /data/output/nested/index.pdf"
330329
),
331330
)

0 commit comments

Comments
 (0)