Skip to content

Commit 63d81b8

Browse files
committed
fix: simplify Docker setup
Especially the user and group IDs can simply be passed into the Docker container without the need of an entrypoint script.
1 parent 1589382 commit 63d81b8

File tree

4 files changed

+125
-119
lines changed

4 files changed

+125
-119
lines changed

Dockerfile

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,63 @@
11
# Usage:
22
#
3-
# /strictdoc$ docker build . -t strictdoc:latest
4-
# /strictdoc$ docker run --name strictdoc --rm -v "$(pwd):/data" -i -t strictdoc:latest
5-
# bash-5.1# strictdoc export .
6-
# bash-5.1# exit
7-
# strictdoc$ firefox docs/output/html/index.html
3+
# /strictdoc$ docker build \
4+
# ./ \
5+
# --build-arg STRICTDOC_SOURCE=pypi \
6+
# --tag strictdoc:latest
7+
# /strictdoc$ docker run \
8+
# --rm \
9+
# --volume="$(pwd):/data/" \
10+
# --user=$(id -u):$(id -g) \
11+
# --userns=host \
12+
# --network=host \
13+
# --hostname="strictdoc" \
14+
# --name="strictdoc" \
15+
# --init \
16+
# --tty \
17+
# strictdoc:latest \
18+
# export ./
19+
# /strictdoc$ firefox ./output/html/index.html
820

921
FROM ubuntu:24.04
1022

23+
# Workaround: Newly introduced `ubuntu` user in ubuntu:24.04 causes UID/GID
24+
# mapping issues when adding custom user.
25+
RUN touch /var/mail/ubuntu && \
26+
chown ubuntu /var/mail/ubuntu && \
27+
userdel --remove ubuntu
28+
29+
# Main "payload" software
30+
ARG PAYLOAD=strictdoc
31+
32+
# Docker image labels
33+
LABEL maintainer="StrictDoc Project"
34+
LABEL name="${PAYLOAD}"
35+
LABEL description="Software for technical documentation and requirements management."
36+
1137
# Install dependencies
12-
RUN apt-get update && apt-get install -y \
13-
curl \
14-
git \
15-
gosu \
16-
python3 \
17-
python3-pip \
18-
python3-venv \
19-
sudo \
20-
vim \
21-
wget \
22-
&& rm -rf /var/lib/apt/lists/*
38+
RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
39+
curl \
40+
git \
41+
python3 \
42+
python3-pip \
43+
python3-venv \
44+
vim \
45+
wget \
46+
&& \
47+
rm --recursive --force /var/lib/apt/lists/*
2348

2449
# Download and install Google Chrome
25-
RUN wget -q -O google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
26-
&& apt-get update \
27-
&& apt-get install -y ./google-chrome.deb \
28-
&& rm google-chrome.deb
50+
RUN wget --quiet --output-document=google-chrome.deb \
51+
https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
52+
apt-get update && \
53+
apt-get install --assume-yes --no-install-recommends ./google-chrome.deb && \
54+
rm --recursive --force /var/lib/apt/lists/* && \
55+
rm --force google-chrome.deb
2956

30-
# Create a virtual environment in the user's home directory.
31-
RUN python3 -m venv /opt/venv
32-
33-
# Ensure the virtual environment is used by modifying the PATH.
34-
ENV PATH="/opt/venv/bin:$PATH"
57+
# Create a virtual environment and ensure it is used by modifying the PATH.
58+
ENV VIRTUAL_ENV=/opt/venv
59+
RUN python3 -m venv ${VIRTUAL_ENV}
60+
ENV PATH="${VIRTUAL_ENV}/bin:$PATH"
3561

3662
# Install StrictDoc. Set default StrictDoc installation from PyPI but allow
3763
# overriding it with an environment variable.
@@ -44,17 +70,16 @@ RUN if [ "$STRICTDOC_SOURCE" = "pypi" ]; then \
4470
else \
4571
pip install --no-cache-dir --upgrade pip && \
4672
pip install --no-cache-dir git+https://github.com/strictdoc-project/strictdoc.git@${STRICTDOC_SOURCE}; \
47-
fi; \
48-
chmod -R 777 /opt/venv;
49-
50-
# Remove the default 'ubuntu' user.
51-
RUN userdel -r ubuntu 2>/dev/null || true
73+
fi;
5274

53-
# Allow updating the UID/GID dynamically at runtime
54-
COPY entrypoint.sh /entrypoint.sh
55-
RUN chmod +x /entrypoint.sh
75+
# Switch to non-root user
76+
RUN groupadd ${PAYLOAD} && \
77+
useradd --no-log-init --gid ${PAYLOAD} ${PAYLOAD}
78+
USER ${PAYLOAD}
5679

57-
# Set the working directory to the user's home directory.
58-
WORKDIR /data
80+
# Set up working directory.
81+
WORKDIR /data/
5982

60-
ENTRYPOINT ["/entrypoint.sh"]
83+
# Execute StrictDoc by default
84+
ENTRYPOINT ["strictdoc"]
85+
CMD ["--help"]

docs/strictdoc_01_user_guide.sdoc

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ STATEMENT: >>>
130130
TITLE: Requirements management
131131
STATEMENT: StrictDoc shall enable requirements management.
132132

133-
Create a file called ``hello_world.sdoc`` somewhere on your file system and copy the above "Hello World" example text to it. **The file must end with a newline character**.
133+
Create a file called ``hello.sdoc`` somewhere on your file system and copy the above "Hello World" example text to it. **The file must end with a newline character**.
134134

135135
Open a command-line terminal program supported on your system.
136136

137-
Once you have ``strictdoc`` installed (see [LINK: SDOC_UG_GETTING_STARTED] below), switch to the directory with the ``hello_world.sdoc`` file. For example, assuming that the file is now in the ``workspace/hello_world`` directory in your user folder:
137+
Once you have ``strictdoc`` installed (see [LINK: SDOC_UG_GETTING_STARTED] below), switch to the directory with the ``hello.sdoc`` file. For example, assuming that the file is now in the ``workspace/hello_world`` directory in your user folder:
138138

139139
.. code-block:: text
140140

@@ -295,25 +295,48 @@ To build the image:
295295

296296
.. code-block:: text
297297

298-
docker build . \
298+
docker build \
299+
./ \
299300
--build-arg STRICTDOC_SOURCE=pypi \
300-
-t strictdoc:latest
301+
--tag strictdoc:latest
301302

302303
The ``STRICTDOC_SOURCE`` argument is optional (default value is ``pypi``) and can be used to specify which Git revision the container shall check out when installing StrictDoc with ``pip install``. Possible values: a branch name, e.g., ``main``, or a commit hash.
303304

304-
To run the container:
305+
To run the container and execute StrictDoc directly:
305306

306307
.. code-block:: text
307308

308309
docker run \
309-
--name strictdoc \
310310
--rm \
311-
-e HOST_UID=$(id -u) \
312-
-e HOST_GID=$(id -g) \
313-
-v "$(pwd):/data" \
314-
-i -t \
311+
--volume="$(pwd):/data/" \
312+
--user=$(id -u):$(id -g) \
313+
--userns=host \
314+
--network=host \
315+
--hostname="strictdoc" \
316+
--name="strictdoc" \
317+
--init \
318+
--tty \
315319
strictdoc:latest \
316-
/bin/bash -c "strictdoc export --formats=html"
320+
export --formats=html ./
321+
322+
To run an interactive container with a shell:
323+
324+
.. code-block:: text
325+
326+
docker run \
327+
--rm \
328+
--volume="$(pwd):/data/" \
329+
--user=$(id -u):$(id -g) \
330+
--userns=host \
331+
--network=host \
332+
--hostname="strictdoc" \
333+
--name="strictdoc" \
334+
--init \
335+
--interactive \
336+
--tty \
337+
--entrypoint="" \
338+
strictdoc:latest \
339+
bash
317340

318341
.. list-table:: ``docker run`` options breakdown
319342
:widths: 40 60
@@ -325,21 +348,27 @@ To run the container:
325348
* - ``--rm``
326349
- Remove container when it is exited.
327350

328-
* - ``HOST_UID`` and ``HOST_GID``
329-
- These environment variables are used to tell the container that it should create a ``strictdoc`` user that has these values. This enables the content generated by the container to be visible outside the container under the permissions of the host user.
351+
* - ``--volume="<folder on host>:/data/"``
352+
- Normally the folder on the host should be the folder with the StrictDoc documentation. The ``/data/`` folder must always be specified without change because that's where the Docker container is designed to operate.
353+
354+
* - ``--user=$(id -u):$(id -g)``
355+
- Map the host user's user and group ID to the container user's IDs. This enables the content generated by the container to be visible outside the container under the permissions of the host user.
356+
357+
* - ``--network=host``
358+
- Map the host network into the container.
330359

331-
* - ``-v "<folder on host>:/data"``
332-
- Normally the folder on the host should be the folder with the StrictDoc documentation. The ``/data`` folder must always be specified without change because that's where the Docker container is designed to operate.
360+
* - ``--init``
361+
- Forward signals.
333362

334-
* - ``/bin/bash -c "..."``
335-
- This command shall typically be ``strictdoc export`` but it can also be skipped in which case the ``docker run`` will enter the container with SSH.
363+
* - ``--entrypoint=""``
364+
- Override entrypoint as empty to run a different command, e.g. Bash. (The default entrypoint is ``strictdoc``.)
336365

337-
If entering the container manually, the ``strictdoc`` command can be run as follows:
366+
If running an interactive container with a shell, the ``strictdoc`` command can be executed as follows:
338367

339368
.. code-block:: text
340369

341-
bash-5.1# strictdoc export .
342-
bash-5.1# exit
370+
ubuntu@strictdoc:/data$ strictdoc export .
371+
ubuntu@strictdoc:/data$ exit
343372

344373
The documentation shall be generated to ``./output/html`` which is accessible both on the host and inside the container.
345374
<<<

entrypoint.sh

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

tasks.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -969,33 +969,37 @@ def build_docker(
969969
run_invoke(
970970
context,
971971
f"""
972-
docker build .
972+
docker build
973+
./
973974
--build-arg STRICTDOC_SOURCE={source}
974-
-t {image}
975+
--tag {image}
975976
{no_cache_argument}
976977
""",
978+
pty=True,
977979
)
978980

979981

980982
@task(aliases=["rd"])
981983
def run_docker(
982984
context, image: str = "strictdoc:latest", command: Optional[str] = None
983985
):
984-
command_argument = (
985-
f'/bin/bash -c "{command}"' if command is not None else ""
986-
)
986+
command_argument = f"{command}" if command is not None else ""
987987

988988
run_invoke(
989989
context,
990990
f"""
991991
docker run
992-
--name strictdoc
993992
--rm
994-
-it
995-
-e HOST_UID=$(id -u) -e HOST_GID=$(id -g)
996-
-v "$(pwd):/data"
993+
--volume="$(pwd):/data/"
994+
--user=$(id -u):$(id -g)
995+
--userns=host
996+
--network=host
997+
--hostname="strictdoc"
998+
--name="strictdoc"
999+
--init
1000+
--tty
9971001
{image}
998-
{command_argument}
1002+
{command_argument}
9991003
""",
10001004
pty=True,
10011005
)
@@ -1012,7 +1016,7 @@ def test_docker(context, image: str = "strictdoc:latest"):
10121016
run_docker(
10131017
context,
10141018
image=image,
1015-
command="strictdoc export --formats=html,html2pdf .",
1019+
command="export --formats=html,html2pdf ./",
10161020
)
10171021

10181022
def check_file_owner(filepath):

0 commit comments

Comments
 (0)