Skip to content

Commit f0e0b98

Browse files
authored
🎨 Allow to set the PUID and PGID using docker (#12596)
* feat: fix docker permission issues * remove VOLUME call on workspace * move responsibility for user and group creation to entrypoint.sh
1 parent 0292c2c commit f0e0b98

File tree

4 files changed

+139
-34
lines changed

4 files changed

+139
-34
lines changed

Dockerfile

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM node:21 as NODE_BUILD
1+
FROM node:21 AS NODE_BUILD
2+
23
WORKDIR /go/src/github.com/siyuan-note/siyuan/
34
ADD . /go/src/github.com/siyuan-note/siyuan/
45
RUN apt-get update && \
@@ -17,7 +18,7 @@ RUN apt-get purge -y jq
1718
RUN apt-get autoremove -y
1819
RUN rm -rf /var/lib/apt/lists/*
1920

20-
FROM golang:alpine as GO_BUILD
21+
FROM golang:alpine AS GO_BUILD
2122
WORKDIR /go/src/github.com/siyuan-note/siyuan/
2223
COPY --from=NODE_BUILD /go/src/github.com/siyuan-note/siyuan/ /go/src/github.com/siyuan-note/siyuan/
2324
ENV GO111MODULE=on
@@ -30,18 +31,22 @@ RUN apk add --no-cache gcc musl-dev && \
3031
mv /go/src/github.com/siyuan-note/siyuan/app/guide/ /opt/siyuan/ && \
3132
mv /go/src/github.com/siyuan-note/siyuan/app/changelogs/ /opt/siyuan/ && \
3233
mv /go/src/github.com/siyuan-note/siyuan/kernel/kernel /opt/siyuan/ && \
34+
mv /go/src/github.com/siyuan-note/siyuan/kernel/entrypoint.sh /opt/siyuan/entrypoint.sh && \
3335
find /opt/siyuan/ -name .git | xargs rm -rf
3436

3537
FROM alpine:latest
3638
LABEL maintainer="Liang Ding<[email protected]>"
3739

3840
WORKDIR /opt/siyuan/
3941
COPY --from=GO_BUILD /opt/siyuan/ /opt/siyuan/
40-
RUN addgroup --gid 1000 siyuan && adduser --uid 1000 --ingroup siyuan --disabled-password siyuan && apk add --no-cache ca-certificates tzdata && chown -R siyuan:siyuan /opt/siyuan/
42+
43+
RUN apk add --no-cache ca-certificates tzdata su-exec && \
44+
chmod +x /opt/siyuan/entrypoint.sh
4145

4246
ENV TZ=Asia/Shanghai
47+
ENV HOME=/home/siyuan
4348
ENV RUN_IN_CONTAINER=true
4449
EXPOSE 6806
4550

46-
USER siyuan
47-
ENTRYPOINT ["/opt/siyuan/kernel"]
51+
ENTRYPOINT ["/opt/siyuan/entrypoint.sh"]
52+
CMD ["/opt/siyuan/kernel"]

README.md

+52-20
Original file line numberDiff line numberDiff line change
@@ -175,49 +175,80 @@ The overall program is located under `/opt/siyuan/`, which is basically the stru
175175

176176
#### Entrypoint
177177

178-
The entry point is set when building the Docker image: `ENTRYPOINT ["/opt/siyuan/kernel" ]`, use `docker run b3log/siyuan` with parameters to start:
178+
The entry point is set when building the Docker image: `ENTRYPOINT ["/opt/siyuan/entrypoint.sh"]`. This script allows changing the `PUID` and `PGID` of the user that will run inside the container. This is especially relevant to solve permission issues when mounting directories from the host. The `PUID` (User ID) and `PGID` (Group ID) can be passed as environment variables, making it easier to ensure correct permissions when accessing host-mounted directories.
179179

180-
* `--workspace`: Specifies the workspace folder path, mounted to the container via `-v` on the host
181-
* `--accessAuthCode`: Specifies the access authorization code
180+
Use the following parameters when running the container with `docker run b3log/siyuan`:
182181

183-
More parameters can refer to `--help`. The following is an example of a startup command:
182+
- `--workspace`: Specifies the workspace folder path, mounted to the container via `-v` on the host
183+
- `--accessAuthCode`: Specifies the access authorization code
184184

185-
```
186-
docker run -d -v workspace_dir_host:workspace_dir_container -p 6806:6806 b3log/siyuan --workspace=workspace_dir_container --accessAuthCode=xxx
187-
```
188-
189-
* `workspace_dir_host`: The workspace folder path on the host
190-
* `workspace_dir_container`: The path of the workspace folder in the container, which is the same as specified in `--workspace`
191-
* `accessAuthCode`: Access authorization code, please **be sure to modify**, otherwise anyone can read and write your data
192-
193-
To simplify, it is recommended to configure the workspace folder path to be consistent on the host and container, such as: `workspace_dir_host` and `workspace_dir_container` are configured as `/siyuan/workspace`, the corresponding startup commands is:
185+
More parameters can be found using `--help`. Here’s an example of a startup command with the new environment variables:
194186

187+
```bash
188+
docker run -d \
189+
-v workspace_dir_host:workspace_dir_container \
190+
-p 6806:6806 \
191+
-e PUID=1001 -e PGID=1002 \
192+
b3log/siyuan \
193+
--workspace=workspace_dir_container \
194+
--accessAuthCode=xxx
195195
```
196-
docker run -d -v /siyuan/workspace:/siyuan/workspace -p 6806:6806 -u 1000:1000 b3log/siyuan --workspace=/siyuan/workspace/ --accessAuthCode=xxx
196+
197+
- `PUID`: Custom user ID (optional, defaults to `1000` if not provided)
198+
- `PGID`: Custom group ID (optional, defaults to `1000` if not provided)
199+
- `workspace_dir_host`: The workspace folder path on the host
200+
- `workspace_dir_container`: The path of the workspace folder in the container, as specified in `--workspace`
201+
- `accessAuthCode`: Access authorization code (please **be sure to modify**, otherwise anyone can access your data)
202+
203+
To simplify things, it is recommended to configure the workspace folder path to be consistent on the host and container, such as having both `workspace_dir_host` and `workspace_dir_container` configured as `/siyuan/workspace`. The corresponding startup command would be:
204+
205+
```bash
206+
docker run -d \
207+
-v /siyuan/workspace:/siyuan/workspace \
208+
-p 6806:6806 \
209+
-e PUID=1001 -e PGID=1002 \
210+
b3log/siyuan \
211+
--workspace=/siyuan/workspace/ \
212+
--accessAuthCode=xxx
197213
```
198214

199-
Alternatively, see below for an example Docker Compose file:
215+
#### Docker Compose
200216

201-
```
217+
For users running Siyuan with Docker Compose, the environment variables `PUID` and `PGID` can be passed to customize the user and group IDs. Here's an example of a Docker Compose configuration:
218+
219+
```yaml
202220
version: "3.9"
203221
services:
204222
main:
205223
image: b3log/siyuan
206224
command: ['--workspace=/siyuan/workspace/', '--accessAuthCode=${AuthCode}']
207-
user: '1000:1000'
208225
ports:
209226
- 6806:6806
210227
volumes:
211228
- /siyuan/workspace:/siyuan/workspace
212229
restart: unless-stopped
213230
environment:
214231
# A list of time zone identifiers can be found at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
215-
- TZ=${TimeZone}
232+
- TZ=${YOUR_TIME_ZONE}
233+
- PUID=${YOUR_USER_PUID} # Customize user ID
234+
- PGID=${YOUR_USER_PGID} # Customize group ID
216235
```
217236
218-
#### User permissions
237+
In this setup:
238+
- `PUID` and `PGID` are set dynamically and passed to the container.
239+
- If these variables are not provided, the default `1000` will be used.
240+
241+
By specifying `PUID` and `PGID` in the environment, you avoid the need to explicitly set the `user` directive (`user: '1000:1000'`) in the compose file. The container will dynamically adjust the user and group based on these environment variables at startup.
242+
243+
#### User Permissions
244+
245+
In the image, the `entrypoint.sh` script ensures the creation of the `siyuan` user and group with the specified `PUID` and `PGID`. Therefore, when the host creates a workspace folder, pay attention to setting the user and group ownership of the folder to match the `PUID` and `PGID` you plan to use. For example:
246+
247+
```bash
248+
chown -R 1001:1002 /siyuan/workspace
249+
```
219250

220-
In the image, the normal user `siyuan` (uid 1000/gid 1000) created by default is used to start the kernel process. Therefore, when the host creates a workspace folder, please pay attention to setting the user group of the folder: `chown -R 1000:1000 /siyuan/workspace`. The parameter `-u 1000:1000` is required when starting the container.
251+
If you use custom `PUID` and `PGID` values, the entrypoint script will ensure that the correct user and group are created inside the container, and ownership of mounted volumes will be adjusted accordingly. There’s no need to manually pass `-u` in `docker run` or `docker-compose` as the environment variables will handle the customization.
221252

222253
#### Hidden port
223254

@@ -229,6 +260,7 @@ Use NGINX reverse proxy to hide port 6806, please note:
229260

230261
* Be sure to confirm the correctness of the mounted volume, otherwise the data will be lost after the container is deleted
231262
* Do not use URL rewriting for redirection, otherwise there may be problems with authentication, it is recommended to configure a reverse proxy
263+
* If you encounter permission issues, verify that the `PUID` and `PGID` environment variables match the ownership of the mounted directories on your host system
232264

233265
#### Limitations
234266

README_zh_CN.md

+40-9
Original file line numberDiff line numberDiff line change
@@ -178,49 +178,80 @@
178178

179179
#### 启动入口
180180

181-
构建 Docker 镜像时设置了入口:`ENTRYPOINT [ "/opt/siyuan/kernel" ]`,使用 `docker run b3log/siyuan` 并带参即可启动:
181+
入口点在构建 Docker 镜像时设置: `ENTRYPOINT ["/opt/siyuan/entrypoint.sh"]`。该脚本允许更改将在容器内运行的用户的 `PUID``PGID`。这对于解决从主机挂载目录时的权限问题尤为重要。`PUID``PGID` 可以作为环境变量传递,这样在访问主机挂载的目录时就能更容易地确保正确的权限。
182+
183+
使用 `docker run b3log/siyuan` 运行容器时,请使用以下参数:
182184

183185
* `--workspace`:指定工作空间文件夹路径,在宿主机上通过 `-v` 挂载到容器中
184186
* `--accessAuthCode`:指定访问授权码
185187

186188
更多的参数可参考 `--help`。下面是一条启动命令示例:
187189

188-
```
189-
docker run -d -v workspace_dir_host:workspace_dir_container -p 6806:6806 b3log/siyuan --workspace=workspace_dir_container --accessAuthCode=xxx
190+
```bash
191+
docker run -d \
192+
-v workspace_dir_host:workspace_dir_container \
193+
-p 6806:6806 \
194+
-e PUID=1001 -e PGID=1002 \
195+
b3log/siyuan \
196+
--workspace=workspace_dir_container \
197+
--accessAuthCode=xxx
190198
```
191199

200+
* `PUID`: 自定义用户 ID(可选,如果未提供,默认为 `1000)
201+
* `PGID`: 自定义组 ID(可选,如果未提供,默认为 `1000)
192202
* `workspace_dir_host`:宿主机上的工作空间文件夹路径
193203
* `workspace_dir_container`:容器内工作空间文件夹路径,和后面 `--workspace` 指定成一样的
194204
* `accessAuthCode`:访问授权码,请**务必修改**,否则任何人都可以读写你的数据
195205

196206
为了简化,建议将 workspace 文件夹路径在宿主机和容器上配置为一致的,比如将 `workspace_dir_host``workspace_dir_container` 都配置为 `/siyuan/workspace`,对应的启动命令示例:
197207

198-
```
199-
docker run -d -v /siyuan/workspace:/siyuan/workspace -p 6806:6806 -u 1000:1000 b3log/siyuan --workspace=/siyuan/workspace/ --accessAuthCode=xxx
208+
```bash
209+
docker run -d \
210+
-v /siyuan/workspace:/siyuan/workspace \
211+
-p 6806:6806 \
212+
-e PUID=1001 -e PGID=1002 \
213+
b3log/siyuan \
214+
--workspace=/siyuan/workspace/ \
215+
--accessAuthCode=xxx
200216
```
201217

202218
使用 Docker Compose 部署请参考下面的示例:
203219

204-
```
220+
对于使用 Docker Compose 运行思源的用户,可以通过环境变量 `PUID``PGID` 来自定义用户和组的 ID。下面是一个 Docker Compose 配置示例:
221+
222+
```yaml
205223
version: "3.9"
206224
services:
207225
main:
208226
image: b3log/siyuan
209227
command: ['--workspace=/siyuan/workspace/', '--accessAuthCode=${AuthCode}']
210-
user: '1000:1000'
211228
ports:
212229
- 6806:6806
213230
volumes:
214231
- /siyuan/workspace:/siyuan/workspace
215232
restart: unless-stopped
216233
environment:
217234
# A list of time zone identifiers can be found at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
218-
- TZ=${TimeZone}
235+
- TZ=${YOUR_TIME_ZONE}
236+
- PUID=${YOUR_USER_PUID} # 自定义用户 ID
237+
- PGID=${YOUR_USER_PGID} # 自定义组 ID
219238
```
220239
240+
在此设置中:
241+
- PUID “和 ”PGID "是动态设置并传递给容器的。
242+
- 如果没有提供这些变量,将使用默认的 `1000`。
243+
244+
在环境中指定 `PUID` 和 `PGID` 后,就无需在组成文件中明确设置 `user` 指令(`user: '1000:1000'`)。容器将在启动时根据这些环境变量动态调整用户和组。
245+
221246
#### 用户权限
222247
223-
镜像中是使用默认创建的普通用户 `siyuan`(uid 1000/gid 1000)来启动内核进程的,所以在宿主机创建工作空间文件夹时请注意设置该文件夹所属用户组:`chown -R 1000:1000 /siyuan/workspace`,在启动容器时需要带参数 `-u 1000:1000`
248+
在图片中,“entrypoint.sh ”脚本确保以指定的 “PUID ”和 “PGID ”创建 “siyuan ”用户和组。因此,当主机创建工作区文件夹时,请注意设置文件夹的用户和组所有权,使其与计划使用的 `PUID` 和 `PGID` 匹配。例如
249+
250+
```bash
251+
chown -R 1001:1002 /siyuan/workspace
252+
```
253+
254+
如果使用自定义的 `PUID` 和 `PGID` 值,入口点脚本将确保在容器内创建正确的用户和组,并相应调整挂载卷的所有权。无需在 `docker run` 或 `docker-compose` 中手动传递 `-u`,因为环境变量会处理自定义。
224255

225256
#### 隐藏端口
226257

kernel/entrypoint.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Default values
5+
PUID=${PUID:-1000}
6+
PGID=${PGID:-1000}
7+
USER_NAME=${USER_NAME:-siyuan}
8+
GROUP_NAME=${GROUP_NAME:-siyuan}
9+
10+
# Get or create group
11+
group_name="${GROUP_NAME}"
12+
if getent group "${PGID}" > /dev/null 2>&1; then
13+
group_name=$(getent group "${PGID}" | cut -d: -f1)
14+
echo "Using existing group: ${group_name} (${PGID})"
15+
else
16+
echo "Creating group ${group_name} (${PGID})"
17+
addgroup --gid "${PGID}" "${group_name}"
18+
fi
19+
20+
# Get or create user
21+
user_name="${USER_NAME}"
22+
if id -u "${PUID}" > /dev/null 2>&1; then
23+
user_name=$(getent passwd "${PUID}" | cut -d: -f1)
24+
echo "Using existing user ${user_name} (PUID: ${PUID}, PGID: ${PGID})"
25+
else
26+
echo "Creating user ${user_name} (PUID: ${PUID}, PGID: ${PGID})"
27+
adduser --uid "${PUID}" --ingroup "${group_name}" --disabled-password --gecos "" "${user_name}"
28+
fi
29+
30+
# Change ownership of relevant directories
31+
echo "Adjusting ownership of /opt/siyuan and /home/siyuan/"
32+
chown -R "${PUID}:${PGID}" /opt/siyuan
33+
chown -R "${PUID}:${PGID}" /home/siyuan/
34+
35+
# Switch to the newly created user and start the main process
36+
echo "Starting Siyuan with UID:${PUID} and GID:${PGID}"
37+
exec su-exec "${PUID}:${PGID}" /opt/siyuan/kernel "$@"

0 commit comments

Comments
 (0)