|
| 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 | +## Apply changes |
| 6 | + |
| 7 | +You can apply the following patches to your `Dockerfile`, `compose.override.yaml` and `compose.prod.yaml` to run the FrankenPHP container as non-root for development and production usage. |
| 8 | + |
| 9 | +`Dockerfile` |
| 10 | + |
| 11 | +```diff |
| 12 | +--- Dockerfile |
| 13 | ++++ Dockerfile |
| 14 | +@@ -1,4 +1,8 @@ |
| 15 | + #syntax=docker/dockerfile:1 |
| 16 | ++ARG PUID=${PUID:-1000} |
| 17 | ++ARG PGID=${PGID:-1000} |
| 18 | ++ARG USER=${USER:-frankenphp} |
| 19 | ++ARG GROUP=${GROUP:-frankenphp} |
| 20 | + |
| 21 | + # Versions |
| 22 | + FROM dunglas/frankenphp:1-php8.4 AS frankenphp_upstream |
| 23 | +@@ -11,6 +15,11 @@ |
| 24 | + # Base FrankenPHP image |
| 25 | + FROM frankenphp_upstream AS frankenphp_base |
| 26 | + |
| 27 | ++ARG PUID |
| 28 | ++ARG PGID |
| 29 | ++ARG USER |
| 30 | ++ARG GROUP |
| 31 | ++ |
| 32 | + WORKDIR /app |
| 33 | + |
| 34 | + VOLUME /app/var/ |
| 35 | +@@ -46,6 +55,12 @@ |
| 36 | + COPY --link --chmod=755 frankenphp/docker-entrypoint.sh /usr/local/bin/docker-entrypoint |
| 37 | + COPY --link frankenphp/Caddyfile /etc/frankenphp/Caddyfile |
| 38 | + |
| 39 | ++RUN set -eux; \ |
| 40 | ++ groupadd -g $PGID $GROUP; \ |
| 41 | ++ useradd -u $PUID -g $PGID --no-create-home $USER; \ |
| 42 | ++ mkdir -p var/cache var/log; \ |
| 43 | ++ chown -R $PUID:$PGID /data/ /config/ var/cache var/log |
| 44 | ++ |
| 45 | + ENTRYPOINT ["docker-entrypoint"] |
| 46 | + |
| 47 | + HEALTHCHECK --start-period=60s CMD curl -f http://localhost:2019/metrics || exit 1 |
| 48 | +@@ -54,6 +69,8 @@ |
| 49 | + # Dev FrankenPHP image |
| 50 | + FROM frankenphp_base AS frankenphp_dev |
| 51 | + |
| 52 | ++ARG USER |
| 53 | ++ |
| 54 | + ENV APP_ENV=dev |
| 55 | + ENV XDEBUG_MODE=off |
| 56 | + ENV FRANKENPHP_WORKER_CONFIG=watch |
| 57 | +@@ -67,11 +84,17 @@ |
| 58 | + |
| 59 | + COPY --link frankenphp/conf.d/20-app.dev.ini $PHP_INI_DIR/app.conf.d/ |
| 60 | + |
| 61 | ++USER $USER |
| 62 | ++ |
| 63 | + CMD [ "frankenphp", "run", "--config", "/etc/frankenphp/Caddyfile", "--watch" ] |
| 64 | + |
| 65 | + # Prod FrankenPHP image |
| 66 | + FROM frankenphp_base AS frankenphp_prod |
| 67 | + |
| 68 | ++ARG PUID |
| 69 | ++ARG PGID |
| 70 | ++ARG USER |
| 71 | ++ |
| 72 | + ENV APP_ENV=prod |
| 73 | + |
| 74 | + RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" |
| 75 | +@@ -92,4 +115,7 @@ |
| 76 | + composer dump-autoload --classmap-authoritative --no-dev; \ |
| 77 | + composer dump-env prod; \ |
| 78 | + composer run-script --no-dev post-install-cmd; \ |
| 79 | +- chmod +x bin/console; sync; |
| 80 | ++ chmod +x bin/console; sync; \ |
| 81 | ++ chown -R $PUID:$PGID var/cache var/log |
| 82 | ++ |
| 83 | ++USER $USER |
| 84 | +``` |
| 85 | + |
| 86 | +`compose.override.yaml` |
| 87 | +```yaml |
| 88 | +--- compose.override.yaml |
| 89 | ++++ compose.override.yaml |
| 90 | +@@ -5,6 +5,10 @@ |
| 91 | + build: |
| 92 | + context: . |
| 93 | + target: frankenphp_dev |
| 94 | ++ args: |
| 95 | ++ PUID: ${PUID:-1000} |
| 96 | ++ PGID: ${PGID:-1000} |
| 97 | ++ user: "${PUID:-1000}:${PGID:-1000}" |
| 98 | + volumes: |
| 99 | + - ./:/app |
| 100 | + - ./frankenphp/Caddyfile:/etc/frankenphp/Caddyfile:ro |
| 101 | +``` |
| 102 | +
|
| 103 | +`compose.prod.yaml` |
| 104 | +```yaml |
| 105 | +--- compose.prod.yaml |
| 106 | ++++ compose.prod.yaml |
| 107 | +@@ -5,6 +5,11 @@ |
| 108 | + build: |
| 109 | + context: . |
| 110 | + target: frankenphp_prod |
| 111 | ++ args: |
| 112 | ++ PUID: ${PUID:-1000} |
| 113 | ++ PGID: ${PGID:-1000} |
| 114 | ++ user: "${PUID:-1000}:${PGID:-1000}" |
| 115 | + environment: |
| 116 | + APP_SECRET: ${APP_SECRET} |
| 117 | + MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET} |
| 118 | +``` |
| 119 | + |
| 120 | +> [!TIP] |
| 121 | +> You can copy-paste the contents of the above diffs into patch files and run `patch <original> <patched>` to apply the changes directly. |
| 122 | +> Example: `patch Dockerfile < Dockerfile.patch` |
| 123 | + |
| 124 | +## Usage |
| 125 | + |
| 126 | +After applying the previous changes, you have to pass the `PUID` and `PGID` as environment variables to the Dockerfile. |
| 127 | + |
| 128 | +You can do this in a myriad of different ways: |
| 129 | + |
| 130 | +- Export your `PUID` and `PGID` to your current shell before running `docker compose`. |
| 131 | + |
| 132 | +```shell |
| 133 | +$ export PUID=$(id -u); export PGID=$(id -g); docker compose ... |
| 134 | +``` |
| 135 | + |
| 136 | +- Pass `PUID` and `PGID` directly as arguments to `docker compose`. |
| 137 | + |
| 138 | +```shell |
| 139 | +$ PUID=$(id -u) PGID=$(id -g) docker compose ... |
| 140 | +``` |
| 141 | + |
| 142 | +- Add `PUID` and `PGID` to your dotenv (`.env`) file. |
| 143 | + |
| 144 | +```dotenv |
| 145 | +PUID=1000 |
| 146 | +PGID=1000 |
| 147 | +``` |
| 148 | + |
| 149 | +> [!CAUTION] |
| 150 | +> This method is not recommended as it can cause issues in CI environment where the runner has a different UID/GID. |
| 151 | + |
| 152 | +- Use third-party tools, like [`Task`](https://taskfile.dev/), to do the heavy lifting for you. |
| 153 | + |
| 154 | +```yaml |
| 155 | +version: '3' |
| 156 | +
|
| 157 | +env: |
| 158 | + PUID: |
| 159 | + sh: id -u |
| 160 | + PGID: |
| 161 | + sh: id -g |
| 162 | +
|
| 163 | +tasks: |
| 164 | + up: |
| 165 | + desc: Up stack |
| 166 | + cmds: |
| 167 | + - docker compose ... |
| 168 | +``` |
0 commit comments