【问题标题】:Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission deniedDocker-Compose + Postgres:/docker-entrypoint-initdb.d/init.sql:权限被拒绝
【发布时间】:2020-06-12 22:03:02
【问题描述】:

我有以下 docker compose 文件:

version: "3"
services:
  postgres:
    image: postgres:11.2-alpine
    environment:
      POSTGRES_PASSWORD: root
      POSTGRES_USER: root
    ports:
      - "5432:5432"
    volumes:
      - ./init-db/init-db.sql:/docker-entrypoint-initdb.d/init.sql

这是 init-db.sql:

CREATE TABLE users (
    email VARCHAR(355) UNIQUE NOT NULL,
    password VARCHAR(256) NOT NULL
);

CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    title VARCHAR(100) NOT NULL,
    price NUMERIC(6, 2) NOT NULL,
    category INT NOT NULL
);

INSERT INTO users VALUES ('test@test.com', 'Test*123');
INSERT INTO products (title, price, category) VALUES ('Truco', 9.90, 13);

当我运行 docker-compose up 时,我收到了这个错误:

server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
/docker-entrypoint-initdb.d/init.sql: Permission denied

我已经尝试过:

  • sql文件上的chmod 777
  • sql文件上的chmod -x
  • 使用sudo 运行 docker 和 docker-compose

有什么想法吗?

【问题讨论】:

    标签: postgresql docker-compose init permission-denied


    【解决方案1】:

    我找到了解决这个问题的简单方法...
    您应该使用“构建”方式来创建 postgres 服务
    并且不要为init.sql设置音量,会导致权限问题。

        postgres:
            build: ./postgres
    

    像这样为 postgres 创建一个 Dockerfile

    FROM postgres:12
    COPY ./init.sql /docker-entrypoint-initdb.d/init.sql
    CMD ["docker-entrypoint.sh", "postgres"]
    

    那么它应该可以解决。 希望我的回答能帮到你!

    【讨论】:

      【解决方案2】:

      我在使用 ADD 命令时遇到了类似的问题。

      使用 ADD(例如下载文件)时,默认 chmod 值为 711。使用 COPY 命令时,chmod 将匹配您从中复制文件的主机 chmod。解决方案是在复制之前设置权限,或者在复制之后在 Dockerfile 中更改它们。

      看起来在即将推出的 docker 20 中终于会有一个“COPY --chmod 775”标志,这将使这更容易。

      https://github.com/moby/moby/issues/34819

      当/docker-entrypoint-initdb.d/中的sql文件权限为775时,该文件运行正常。

      您可以使用以下方法在映像中进行测试(覆盖 /bin/bash 的入口点): docker-entrypoint.sh postgres

      【讨论】:

        【解决方案3】:

        我尝试使用以下撰写文件,它似乎按预期工作。你确定形成你使用的文件的路径吗?

        version: "3"
        services:
          postgres:
            image: postgres:11.2-alpine
            environment:
              POSTGRES_PASSWORD: root
              POSTGRES_USER: root
            ports:
              - "5432:5432"
            volumes:
              - ./init-db.sql:/docker-entrypoint-initdb.d/init.sql
        

        文件结构

        drwxr-xr-x   4 shihadeh  502596769   128B Feb 28 22:37 .
        drwxr-xr-x  12 shihadeh  502596769   384B Feb 28 22:36 ..
        -rw-r--r--   1 shihadeh  502596769   244B Feb 28 22:37 docker-compose.yml
        -rw-r--r--   1 shihadeh  502596769   380B Feb 28 22:37 init-db.sql
        

        docker-compose up 的输出

        postgres_1  | The files belonging to this database system will be owned by user "postgres".
        postgres_1  | This user must also own the server process.
        postgres_1  |
        postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
        postgres_1  | The default database encoding has accordingly been set to "UTF8".
        postgres_1  | The default text search configuration will be set to "english".
        postgres_1  |
        postgres_1  | Data page checksums are disabled.
        postgres_1  |
        postgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
        postgres_1  | creating subdirectories ... ok
        postgres_1  | selecting default max_connections ... 100
        postgres_1  | selecting default shared_buffers ... 128MB
        postgres_1  | selecting dynamic shared memory implementation ... posix
        postgres_1  | creating configuration files ... ok
        postgres_1  | running bootstrap script ... ok
        postgres_1  | performing post-bootstrap initialization ... sh: locale: not found
        postgres_1  | 2020-02-28 21:45:01.363 UTC [26] WARNING:  no usable system locales were found
        postgres_1  | ok
        postgres_1  | syncing data to disk ... ok
        postgres_1  |
        postgres_1  | Success. You can now start the database server using:
        postgres_1  |
        postgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
        postgres_1  |
        postgres_1  |
        postgres_1  | WARNING: enabling "trust" authentication for local connections
        postgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
        postgres_1  | --auth-local and --auth-host, the next time you run initdb.
        postgres_1  | waiting for server to start....2020-02-28 21:45:02.272 UTC [30] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
        postgres_1  | 2020-02-28 21:45:02.294 UTC [31] LOG:  database system was shut down at 2020-02-28 21:45:01 UTC
        postgres_1  | 2020-02-28 21:45:02.299 UTC [30] LOG:  database system is ready to accept connections
        postgres_1  |  done
        postgres_1  | server started
        postgres_1  | CREATE DATABASE
        postgres_1  |
        postgres_1  |
        postgres_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
        postgres_1  | CREATE TABLE
        postgres_1  | CREATE TABLE
        postgres_1  | INSERT 0 1
        postgres_1  | INSERT 0 1
        postgres_1  |
        postgres_1  |
        postgres_1  | waiting for server to shut down....2020-02-28 21:45:02.776 UTC [30] LOG:  received fast shutdown request
        postgres_1  | 2020-02-28 21:45:02.779 UTC [30] LOG:  aborting any active transactions
        postgres_1  | 2020-02-28 21:45:02.781 UTC [30] LOG:  background worker "logical replication launcher" (PID 37) exited with exit code 1
        postgres_1  | 2020-02-28 21:45:02.781 UTC [32] LOG:  shutting down
        postgres_1  | 2020-02-28 21:45:02.826 UTC [30] LOG:  database system is shut down
        postgres_1  |  done
        postgres_1  | server stopped
        postgres_1  |
        postgres_1  | PostgreSQL init process complete; ready for start up.
        postgres_1  |
        postgres_1  | 2020-02-28 21:45:02.890 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
        postgres_1  | 2020-02-28 21:45:02.890 UTC [1] LOG:  listening on IPv6 address "::", port 5432
        postgres_1  | 2020-02-28 21:45:02.895 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
        postgres_1  | 2020-02-28 21:45:02.915 UTC [43] LOG:  database system was shut down at 2020-02-28 21:45:02 UTC
        postgres_1  | 2020-02-28 21:45:02.921 UTC [1] LOG:  database system is ready to accept connections
        
        

        【讨论】:

          【解决方案4】:

          对我来说,问题出在我的机器上。启用 SELinux 访问控制,不允许容器扩展文件。

          解决办法,禁用 SELinux:

          echo SELINUX=disabled > /etc/selinux/config
          SELINUXTYPE=targeted >> /etc/selinux/config
          setenforce 0
          

          来自this

          【讨论】:

            【解决方案5】:

            我在我的 macOS 上遇到了同样的问题,但在我的 ubuntu 笔记本电脑上没问题。 我发现问题是 MacOS 不能让 docker 访问该文件夹。 以下链接可能会解决您的问题。 https://stackoverflow.com/a/58482702/10752354

            此外,在我的情况下,我应该在我的 init.sql 文件中添加一行代码,因为默认数据库是“root”,我应该将“root”数据库更改为“postgres”数据库,或者在你的情况下更改为另一个自定义数据库而不是 root。

            > docker-compose exec db psql -U root
            psql (13.0 (Debian 13.0-1.pgdg100+1))
            Type "help" for help.
            
            root=# \l
                                         List of databases
               Name    | Owner | Encoding |  Collate   |   Ctype    | Access privilege
            s 
            -----------+-------+----------+------------+------------+-----------------
            --
             postgres  | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
             root      | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
             template0 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root         
             +
                       |       |          |            |            | root=CTc/root
             template1 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root         
             +
                       |       |          |            |            | root=CTc/root
            (4 rows)
            

            所以我需要在我的 init.sql 文件中添加\c postgres

            \c postgres    // for creating table in the database named 'postgres'
            
            create table sample_table. ( ... )
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-08-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-08-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多