繁体   English   中英

如何在挂载nfs卷时解决postgresql docker容器的chown权限问题?

[英]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行。基本上它是在尝试chmodchown 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM