【问题标题】:how to solve the chown permission issue of postgresql docker container when mount the nfs volume?挂载nfs卷时如何解决postgresql docker容器的chown权限问题?
【发布时间】:2018-12-08 13:03:34
【问题描述】:

我在 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 2.0.0.0 稳定使用 Mac Mojave 和 Docker 桌面。

【问题讨论】:

  • 你找到解决办法了吗?

标签: docker nfs


【解决方案1】:

您无需创建新图像来更改用户,只需以其他用户身份运行 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

【讨论】:

    【解决方案2】:

    我相信我解决了这个问题......

    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 入口点脚本here

    对其进行编辑,注释掉第 34、35、36、52、53、54 行。基本上,它会尝试在 NFS 文件夹中使用 chmodchown

    ...
    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 文件夹具有读/写访问权限的用户/组。

    现在您应该拥有一个能够使用 NFS 主机卷来存储数据库数据的 Postgres Docker 映像。

    希望这会有所帮助..

    【讨论】:

      【解决方案3】:

      对我来说最简单的方法是添加一个执行以下操作的 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"]
      
      

      【讨论】:

        猜你喜欢
        • 2021-10-03
        • 2020-02-23
        • 2013-11-25
        • 2019-10-04
        • 1970-01-01
        • 2019-12-26
        • 2020-10-23
        • 1970-01-01
        • 2017-06-30
        相关资源
        最近更新 更多