首页 > 解决方案 > Docker for Mac 和 mkdir 权限

问题描述

使用 docker for mac 应用程序。昨天刚刚安装了所有东西。终于让应用程序运行起来了。但是在安装 postgis 之前我无法运行迁移。所以我放弃了postgis:11-alpine图像的官方 postgres dockerhub图像。但是当 docker 尝试为 pg_data 卷创建 mkdir 时,我不断收到权限被拒绝的问题。

Dockerfile:

        version: '3'

    # Containers we are going to run
    services:
      # Our Phoenix container
      phoenix:
        # The build parameters for this container.
        build:
          # Here we define that it should build from the current directory
          context: .
        environment:
          # Variables to connect to our Postgres server
          PGUSER: postgres
          PGPASSWORD: postgres
          PGDATABASE: gametime_dev
          PGPORT: 5432
          # Hostname of our Postgres container
          PGHOST: db
        ports:
          # Mapping the port to make the Phoenix app accessible outside of the container
          - "4000:4000"
        depends_on:
          # The db container needs to be started before we start this container
          - db
          - redis

      redis:
        image: "redis:alpine"
        ports:
          - "6379:6379"
        sysctls:
          net.core.somaxconn: 1024

      db:
        # We use the predefined Postgres image
        image: mdillon/postgis:11-alpine
        environment:
          # Set user/password for Postgres
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          # Set a path where Postgres should store the data
          PGDATA: /var/lib/postgresql/data/pgdata
        restart: always
        volumes:
          - pgdata:/usr/local/var/postgres_data
    # Define the volumes
    volumes:
      pgdata:

我得到的错误:

db_1       | mkdir: can't create directory '/var/lib/postgresql/data/pgdata': Permission denied

但是在使用 postgres(官方)图像时不会发生这种情况。我用谷歌搜索了高低。我确实读过一些关于 Docker for Mac 在它作为当前用户的本地主机用户而不是 root 用户在 VM 中创建的容器上运行命令的内容。但这对我来说没有意义——如果是这样的话,我该如何解决?

[额外说明:] - 我确实尝试了 :z 和 :Z - 仍然得到与上面完全相同的错误。

珍惜时间 - 提前。

标签: dockerdocker-composedockerfiledocker-for-macpostgis-installation

解决方案


dbPGD​​ATA 所在服务状态的环境变量,/var/lib/postgresql/data/pgdata但您在容器中安装 pgdata 卷/usr/local/var/postgres_data

我的猜测是,当 postgres 启动时,它正在查看环境变量并期待/var/lib/postgresql/data/pgdata. 由于它可能不存在,它试图将其创建为无权执行此操作的 postgres 用户。

对两个变量使用相同的路径,我很确定它会修复错误。


推荐阅读