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

2024-01-15

我有以下 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 ('[email protected] /cdn-cgi/l/email-protection', '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

我已经尝试过:

  • chmod 777在sql文件上
  • chmod -x在sql文件上
  • 使用以下命令运行 docker 和 docker-composesudo

任何想法?


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

    postgres:
        build: ./postgres

像这样为 postgres 创建一个 Dockerfile

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

然后它应该会成功。 希望我的回答对您有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker-Compose + Postgres:/docker-entrypoint-initdb.d/init.sql:权限被拒绝 的相关文章

随机推荐