Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 98f5729

Browse files
committed
Fix ownership issue when using Docker Volumes
1 parent 8aa24aa commit 98f5729

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ FROM docker.io/alpine:3
22

33
ENV PLANTUML_VERSION 1.2021.11
44
ENV LANG en_US.UTF-8
5-
RUN apk add --no-cache openjdk8-jre graphviz ttf-droid ttf-droid-nonlatin curl \
5+
RUN apk add --no-cache openjdk8-jre graphviz ttf-droid ttf-droid-nonlatin curl shadow su-exec \
66
&& mkdir /app \
77
&& curl -L https://sourceforge.net/projects/plantuml/files/plantuml.${PLANTUML_VERSION}.jar/download -o /app/plantuml.jar \
88
&& apk del curl
9+
COPY entrypoint /
910

10-
ENTRYPOINT [ "java", "-jar", "/app/plantuml.jar" ]
11+
ENTRYPOINT [ "/entrypoint" ]
1112
CMD [ "-h" ]

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ $ run-plantuml [PLANTUML OPTIONS and ARGUMENTS]
3434
`docker run` and `run-plantuml` accept and pass a set of parameters to PlantUML CLI.
3535
See `docker run miy4/plantuml -h` or `run-plantuml -h` output for more details.
3636

37+
### Environment Variables
38+
39+
You can explicitly set the UID and GID of artifacts using the environment variables `PUML_UID` and `PUML_GID`.
40+
In the following run example, PlantUML generates `sequence_diagram.png` owned by the user with UID 1000 and GID 1000.
41+
42+
``` sh
43+
$ docker run -e PUML_UID=1000 -e PUML_GID=1000 -v ${PWD}:/work -w /work --rm -t miy4/plantuml -tpng sequence_diagram.uml
44+
```
45+
46+
If you don't use `PUML_UID` and `PUML_GID`, PlantUML generates the files to have the same UID and GID as the owner of the working directory given by `-w`, `--workdir`.
47+
3748
## Example
3849

3950
```sh

entrypoint

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
if [ "$(id -u)" -ne 0 ]; then
4+
printf 'Error: You cannot be non-root users\n' 1>&2
5+
printf 'Use environment variables PUML_UID and PUML_GID to pass your UID/GID.\n' 1>&2
6+
exit 1
7+
# or should do nothing in particular?
8+
# exec java -jar /app/plantuml.jar "$@"
9+
fi
10+
11+
# Read UID/GID via environment variables.
12+
# If they are not available, Fetch UID/GID from the working directory.
13+
# These values would be the owner UID/GID of PlantUML's artifacts.
14+
#
15+
# Note: For Windows filesystems, stat always returns UID=0 and GID=0
16+
gid=${PUML_GID:-"$(stat -c '%g' .)"}
17+
uid=${PUML_UID:-"$(stat -c '%u' .)"}
18+
19+
# Do as the root
20+
if [ "$uid" -eq 0 ] || [ "$gid" -eq 0 ]; then
21+
exec java -jar /app/plantuml.jar "$@"
22+
fi
23+
24+
# Create a new working user
25+
user='worker'
26+
groupadd --non-unique --gid "$gid" "$user"
27+
useradd --non-unique --uid "$uid" --gid "$gid" --create-home "$user"
28+
29+
# Execute a PlantUML process as the worker
30+
su-exec "$user" java -jar /app/plantuml.jar "$@"

0 commit comments

Comments
 (0)