[英]how to solve the chown permission issue of postgresql docker container when mount the nfs volume?
我在Mac上使用docker並嘗試使用nfs卷獲取postgresql數據庫的持久容器。
我在/etc/exports
放了一行/Users/me/db -alldirs *(rw,sync,no_subtree_check,no_root_squash)
並重啟nfsd。 我認為(糾正我,如果我錯了)關鍵點是no_root_squash,它將允許客戶端root用戶仍然是root用戶。然后在我的docker-compose.yml中,我將nfsmount點聲明為以下內容:
version: '2'
volumes:
nfsmountdbdata:
driver: local
driver_opts:
type: nfs
o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
device: ":/Users/me/db/data"
nfsmountdbinit:
driver: local
driver_opts:
type: nfs
o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
device: ":/Users/me/db/initdb"
services:
## POSTGRES DATABASE
db:
image: postgres:9.6
privileged: true
volumes:
#- ./services/db/initdb:/docker-entrypoint-initdb.d
#- ./services/db/app:/var/lib/postgresql/data
- nfsmountdbinit:/docker-entrypoint-initdb.d
- nfsmountdbdata:/var/lib/postgresql/data
ports:
- 5432:5432
但是當容器db啟動時,它會抱怨chown: changing ownership of '/var/lib/postgresql/data/base/**/**': Operation not permitted
。 這讓我感到非常困惑,因為我已經做了一些事情(nfs中的no_root_squash配置)來修復它。 但它只是不起作用。 我在這里的理解有什么問題? 我使用Mac Mojave和Docker桌面為Mac 2.0.0.0 stabel。
我相信我解決了這個......
Dockerfile
FROM postgres:9.6
ARG GNAME='groupname'
ARG GID='groupid'
ARG USERID=999
# fix permissions so it can persist data on the host nfs file system
RUN groupadd -g $GID $GNAME \
&& usermod -g $GNAME postgres \
&& usermod -u $USERID postgres
# go get the entrypoint script from their git hub link and details to follow
COPY ./docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
在這里獲取Postgres入口點
對其進行編輯,注釋出第34,35,36,52,53,54行。基本上它是在嘗試chmod
和chown
NFS文件夾的地方。
...
if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
#mkdir -p "$PGDATA"
#chown -R postgres "$PGDATA"
#chmod 700 "$PGDATA"
...
if [ "$1" = 'postgres' ]; then
#mkdir -p "$PGDATA"
#chown -R "$(id -u)" "$PGDATA" 2>/dev/null || :
#chmod 700 "$PGDATA" 2>/dev/null || :
...
現在構建圖像......
docker build -t postgres9.6:nfs --build-arg GID=<NFS GROUP ID> ==build-arg GNAME=<NFS GROUP NAME> --build-arg USERID=<NFS USER ID> .
我的意思是NFS GROUP ID,USER ID和GROUP NAME是對NFS文件夾具有讀/寫訪問權限的用戶/組。
現在你應該有一個Postgres Docker鏡像,它能夠使用NFS Host Volumes來存儲數據庫數據。
希望這可以幫助..
對我來說最簡單的事情是添加一個執行以下操作的Dockerfile:
FROM postgres:11.2
ENV TZ=America/Los_Angeles
# Make us the same gid/id as the nfs mount.
RUN sed -i 's/:999:/:5081:/g' /etc/group
RUN sed -i 's/:999:999:/:5081:5081:/g' /etc/passwd
CMD [ "postgres", "-c", "max_connections=10000"]
您無需創建新圖像來更改用戶,您可以將postgres圖像作為其他用戶運行:
postgres:
image: postgres:9.6
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
user: "${UID:?You must do 'export UID' to launch}:${GID:?You must do 'export GID' to launch}"
volumes:
- nfsmountdbdata:/var/lib/postgresql/data
ports:
- 5432:5432
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.