Skip to content

Commit 90fb72e

Browse files
committed
add non-root user documentation
1 parent dab1308 commit 90fb72e

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

docs/non-root-user.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Non-root user
2+
3+
Following [docker best practices](https://docs.docker.com/build/building/best-practices/#user), it is recommended to run your services as non-root user whenever possible.
4+
5+
You can apply the following patches to your `Dockerfile`, `compose.prod.yaml` and `compose.override.yaml` to run the FrankenPHP container as non-root for development and production usage.
6+
7+
`Dockerfile`
8+
9+
```diff
10+
--- Dockerfile
11+
+++ Dockerfile
12+
@@ -1,4 +1,8 @@
13+
#syntax=docker/dockerfile:1
14+
+ARG UID=${UID:-1000}
15+
+ARG GID=${GID:-1000}
16+
+ARG USER=${USER:-frankenphp}
17+
+ARG GROUP=${GROUP:-frankenphp}
18+
19+
# Versions
20+
FROM dunglas/frankenphp:1-php8.4 AS frankenphp_upstream
21+
@@ -11,6 +15,11 @@
22+
# Base FrankenPHP image
23+
FROM frankenphp_upstream AS frankenphp_base
24+
25+
+ARG UID
26+
+ARG GID
27+
+ARG USER
28+
+ARG GROUP
29+
+
30+
WORKDIR /app
31+
32+
VOLUME /app/var/
33+
@@ -46,6 +55,12 @@
34+
COPY --link --chmod=755 frankenphp/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
35+
COPY --link frankenphp/Caddyfile /etc/frankenphp/Caddyfile
36+
37+
+RUN set -eux; \
38+
+ groupadd -g $GID $GROUP; \
39+
+ useradd -u $UID -g $GID --no-create-home $USER; \
40+
+ mkdir -p var/cache var/log; \
41+
+ chown -R $UID:$GID /data/ /config/ var/cache var/log
42+
+
43+
ENTRYPOINT ["docker-entrypoint"]
44+
45+
HEALTHCHECK --start-period=60s CMD curl -f http://localhost:2019/metrics || exit 1
46+
@@ -54,6 +69,8 @@
47+
# Dev FrankenPHP image
48+
FROM frankenphp_base AS frankenphp_dev
49+
50+
+ARG USER
51+
+
52+
ENV APP_ENV=dev
53+
ENV XDEBUG_MODE=off
54+
ENV FRANKENPHP_WORKER_CONFIG=watch
55+
@@ -67,11 +84,17 @@
56+
57+
COPY --link frankenphp/conf.d/20-app.dev.ini $PHP_INI_DIR/app.conf.d/
58+
59+
+USER $USER
60+
+
61+
CMD [ "frankenphp", "run", "--config", "/etc/frankenphp/Caddyfile", "--watch" ]
62+
63+
# Prod FrankenPHP image
64+
FROM frankenphp_base AS frankenphp_prod
65+
66+
+ARG UID
67+
+ARG GID
68+
+ARG USER
69+
+
70+
ENV APP_ENV=prod
71+
72+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
73+
@@ -92,4 +115,7 @@
74+
composer dump-autoload --classmap-authoritative --no-dev; \
75+
composer dump-env prod; \
76+
composer run-script --no-dev post-install-cmd; \
77+
- chmod +x bin/console; sync;
78+
+ chmod +x bin/console; sync; \
79+
+ chown -R $UID:$GID var/cache var/log
80+
+
81+
+USER $USER
82+
```
83+
84+
`compose.override.yaml`
85+
```yaml
86+
--- compose.override.yaml
87+
+++ compose.override.yaml
88+
@@ -5,6 +5,10 @@
89+
build:
90+
context: .
91+
target: frankenphp_dev
92+
+ args:
93+
+ UID: ${UID:-1000}
94+
+ GID: ${GID:-1000}
95+
+ user: "${UID:-1000}:${GID:-1000}"
96+
volumes:
97+
- ./:/app
98+
- ./frankenphp/Caddyfile:/etc/frankenphp/Caddyfile:ro
99+
```
100+
101+
`compose.prod.yaml`
102+
```yaml
103+
--- compose.prod.yaml
104+
+++ compose.prod.yaml
105+
@@ -5,6 +5,11 @@
106+
build:
107+
context: .
108+
target: frankenphp_prod
109+
+ args:
110+
+ UID: ${UID:-1000}
111+
+ GID: ${GID:-1000}
112+
+ user: "${UID:-1000}:${GID:-1000}"
113+
environment:
114+
APP_SECRET: ${APP_SECRET}
115+
MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
116+
```
117+
118+
> [!TIP]
119+
> You can copy-paste the contents of the above diffs into patch files and run `patch <original> <patched>` to apply the changes directly.
120+
> Example: `patch Dockerfile < Dockerfile.patch`

0 commit comments

Comments
 (0)