简体   繁体   English

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

[英]Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied

I have the following docker compose file:我有以下 docker 撰写文件:

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

This is the init-db.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);

When I run docker-compose up , I'm getting this error:当我运行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

I already tried to:我已经尝试过:

  • chmod 777 on the sql file sql 文件上的chmod 777
  • chmod -x on the sql file sql 文件上的chmod -x
  • Run docker and docker-compose using sudo使用sudo运行 docker 和 docker-compose

Any idea?任何想法?

I found the easy way to solve this...我找到了解决这个问题的简单方法......
You should use "build" way to create postgres service您应该使用“构建”方式来创建 postgres 服务
And DO NOT setting the volume for init.sql, it will cause the permission problem.并且不要为init.sql设置音量,这会导致权限问题。

    postgres:
        build: ./postgres

Create a Dockerfile for postgres like this像这样为 postgres 创建一个 Dockerfile

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

Then it should works out.那么它应该工作。 Hope my answer would help you!希望我的回答能帮到你!

I had a similar problem when using the ADD command.我在使用 ADD 命令时遇到了类似的问题。

When using ADD (eg to download a file) the default chmod value is 711. When using the COPY command the chmod will match the hosts chmod of the file where you copy it from.当使用 ADD(例如下载文件)时,默认的 chmod 值为 711。当使用 COPY 命令时,chmod 将匹配您从中复制文件的主机 chmod。 The solution is to set the permission before copying, or change them in your Dockerfile after they've been copied.解决方案是在复制之前设置权限,或者在复制后在 Dockerfile 中更改它们。

It looks like there will finally be a "COPY --chmod 775" flag available in the upcoming docker 20 which will make this easier.看起来在即将到来的 docker 20 中最终会有一个“COPY --chmod 775”标志可用,这将使这更容易。

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

When the sql file in /docker-entrypoint-initdb.d/ has a permission of 775 then the file is run correctly.当 /docker-entrypoint-initdb.d/ 中的 sql 文件的权限为 775 时,该文件将正确运行。

You can test this within the image (override entrypoint to /bin/bash) using: docker-entrypoint.sh postgres您可以使用以下命令在图像中进行测试(覆盖 /bin/bash 的入口点):docker-entrypoint.sh postgres

I tried with the following compose file and it seems working as expected.我尝试使用以下撰写文件,它似乎按预期工作。 are you sure form the path of the files you use?你确定形成你使用的文件的路径吗?

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

files structure文件结构

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

output of docker-compose up 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

For me problem was in my machine.对我来说,问题出在我的机器上。 enabled SELinux access control, which did not allow for containers to expand files.启用了 SELinux 访问控制,不允许容器扩展文件。

Solution, disable SELinux:解决办法,禁用SELinux:

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

From this这个

I have same problem on my macOS, but it's OK on my ubuntu laptop.我在 macOS 上有同样的问题,但在我的 ubuntu 笔记本电脑上没问题。 I found the problem is that MacOS cannot let docker access the folder.我发现问题是 MacOS 不能让 docker 访问该文件夹。 Following link may resolve your problem.以下链接可能会解决您的问题。 https://stackoverflow.com/a/58482702/10752354 https://stackoverflow.com/a/58482702/10752354

Besides, in my case, I should add one line code in my init.sql file because the default database is "root", and I should change "root" database into "postgres" database, or in your case for another custom database instead of root.此外,在我的情况下,我应该在我的 init.sql 文件中添加一行代码,因为默认数据库是“root”,我应该将“root”数据库更改为“postgres”数据库,或者在您的情况下改为另一个自定义数据库根的。

> 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)

so I need to add \\c postgres in my init.sql file:所以我需要在我的 init.sql 文件中添加\\c postgres

\c postgres    // for creating table in the database named 'postgres'

create table sample_table. ( ... )

Just adding my solution even when it is a little late:即使有点晚了,也只需添加我的解决方案:

My problem:我的问题:

I am installing the source files from an udemy course:我正在从 udemy 课程安装源文件:

https://github.com/rockthejvm/spark-essentials https://github.com/rockthejvm/spark-essentials

We have 2 important things here:我们这里有两件重要的事情:

Docker compose file: docker-compose.yml Docker 撰写文件:docker-compose.yml

Folder and sql file to do db initialization: ./sql/bd.sql用于进行数据库初始化的文件夹和 sql 文件:./sql/bd.sql

I downloaded the zip file to my Ubuntu 20.04我将 zip 文件下载到我的 Ubuntu 20.04

When doing docker-compose up, I got the error:在做 docker-compose 时,我得到了错误:

Attaching to postgres附加到 postgres

postgres |后勤 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied ls: 无法打开目录'/docker-entrypoint-initdb.d/': 权限被拒绝

postgres exited with code 2 postgres 以代码 2 退出

SOLUTION解决方案

Before running doccker-compose up do:在运行 docker-compose up 之前,请执行以下操作:

run chmod 777 ON FOLDER called sql that contains file db.sql在名为 sql 的文件夹上运行 chmod 777,其中包含文件 db.sql

chmod 777 sql chmod 777 sql

Explanation:解释:

The folder called sql contains file db.sql to init the database from inside the docker container so, we need to give the permissions to run the file inside folde called sql. The folder called sql contains file db.sql to init the database from inside the docker container so, we need to give the permissions to run the file inside folde called sql.

I think you only need to do as below.我认为您只需要执行以下操作。

Give permisions to your folder:授予文件夹权限:

chmod 777 init-db chmod 777 初始化数据库

be sure your sql file can be read by all users:确保所有用户都可以读取您的 sql 文件:

chmod 666 init-db.sql chmod 666 init-db.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker-compose postgres 在 docker-entrypoint-initdb.d 中运行脚本后重新启动 - docker-compose postgres restart after running scripts in docker-entrypoint-initdb.d docker-entrypoint-initdb.d 错误解释器:权限被拒绝 - docker-entrypoint-initdb.d bad interpreter: Permission denied docker-entrypoint-initdb.d/* 文件在 docker-compose 首次运行时被忽略 - docker-entrypoint-initdb.d/* files ignored on docker-compose first run docker alpine postgres在撰写中不执行docker-entrypoint-initdb.d脚本 - docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts Docker postgres 不在 docker-entrypoint-initdb.d 中运行 init 文件 - Docker postgres does not run init file in docker-entrypoint-initdb.d 初始化postgres数据库(不使用/docker-entrypoint-initdb.d) - Initialize postgres database (without using /docker-entrypoint-initdb.d) Docker 入口点 initdb 权限被拒绝 - Docker entrypoint initdb PERMISSION DENIED 使用 /docker-entrypoint-initdb.d 初始化 postgres docker 时不会创建表 - Tables are not created when initialize postgres docker with /docker-entrypoint-initdb.d init.sql 作为目录添加到 docker-compose 与 postgressql - init.sql is added as directory in docker-compose with postgressql 为什么 postgres 容器在 Gitlab CI 中忽略 /docker-entrypoint-initdb.d/* - Why is postgres container ignoring /docker-entrypoint-initdb.d/* in Gitlab CI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM