Skip to content

Commit 9acab14

Browse files
committed
doc: Replicate detailed nonroot user docs
Node.js has great docs for rootless usage. Since their docs are MIT licensed, we're replicating them here with attribution.
1 parent f6138f4 commit 9acab14

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

Dockerfile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,5 @@ RUN curl -sSL https://install.python-poetry.org | python3 -
3737
###############################################################################
3838
FROM python-poetry-base AS python-poetry
3939
COPY --from=python-poetry-builder $POETRY_HOME $POETRY_HOME
40-
41-
###############################################################################
42-
# POETRY RUNTIME IMAGE - Add a 'nonroot' unprivileged user to run the apps
43-
###############################################################################
44-
# Add the non-root user with UID/GID 1000:1000
4540
RUN groupadd --gid 1000 nonroot \
4641
&& useradd --uid 1000 --gid 1000 --no-create-home --shell /bin/bash nonroot

README.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,68 @@ make build-version \
6363
PYTHON_IMAGE_TAG="3.10-slim"
6464
```
6565

66-
This image will also defined an unprivileged 'nonroot' user with UID:GID 1000:1000 to be used in your derived
67-
images with the USER directive and run your apps more safely. In this case of course remeber to assign the
68-
corresponding ownership to your application tree.
66+
## Non-root User
67+
68+
> [!NOTE]
69+
>
70+
> This section was adapted from the Node.js docs for [**Non-root
71+
> user**](https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#non-root-user)
72+
> in their Docker images.
73+
74+
75+
By default, Docker runs commands inside the container as root which violates the [Principle of Least Privilege (PoLP)](https://en.wikipedia.org/wiki/Principle_of_least_privilege) when superuser permissions are not strictly required. You want to run the container as an unprivileged user whenever possible. The nonroot images provide the `nonroot` user for such purpose. The Docker Image can then be run with the `nonroot` user in the following way:
76+
77+
```
78+
-u "nonroot"
79+
```
80+
81+
Alternatively, the user can be activated in the `Dockerfile`:
82+
83+
```Dockerfile
84+
FROM thehale/python-poetry:1.8.3
85+
...
86+
# At the end, set the user to use when running this image
87+
USER nonroot
88+
```
89+
90+
> [!TIP]
91+
>
92+
> When using the `nonroot` user, remember to assign the corresponding ownership
93+
> to your application tree (e.g. `chmod`).
94+
95+
Note that the `nonroot` user is neither a build-time nor a run-time dependency
96+
and it can be removed or altered, as long as the functionality of the
97+
application you want to add to the container does not depend on it.
98+
99+
If you do not want nor need the user created in this image, you can remove it with the following:
100+
101+
```Dockerfile
102+
# For debian based images use:
103+
RUN userdel -r nonroot
104+
105+
# For alpine based images use:
106+
RUN deluser --remove-home nonroot
107+
```
108+
109+
If you need to change the uid/gid of the user, you can use:
110+
111+
```Dockerfile
112+
RUN groupmod -g 999 nonroot && usermod -u 999 -g 999 nonroot
113+
```
114+
115+
If you need another name for the user (ex. `myapp`), execute:
116+
117+
```Dockerfile
118+
RUN usermod -d /home/myapp -l myapp nonroot
119+
```
120+
121+
For alpine based images, you do not have `groupmod` nor `usermod`, so to change the uid/gid you have to delete the previous user:
122+
123+
```Dockerfile
124+
RUN deluser --remove-home nonroot \
125+
&& addgroup -S nonroot -g 999 \
126+
&& adduser -S -G nonroot -u 999 nonroot
127+
```
69128

70129
## License
71130

0 commit comments

Comments
 (0)