Docker-Compose + Postgres:/docker-entrypoint-initdb.d/init.sql:权限被拒绝

时间:2020-02-28 19:06:58

标签: postgresql docker-compose init permission-denied

我有以下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

有什么主意吗?

5 个答案:

答案 0 :(得分: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"]

然后应该可以解决。 希望我的回答对您有帮助!

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

解决方案,禁用SELinux:

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

来自this

答案 3 :(得分:0)

我在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. ( ... )

答案 4 :(得分:0)

使用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