gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:03:35 44 4
gpt4 key购买 nike

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

我已经尝试过:
  • chmod 777关于 sql 文件
  • chmod -x在 sql 文件
  • 使用 sudo 运行 docker 和 docker-compose

  • 任何的想法?

    最佳答案

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

        postgres:
    build: ./postgres

    像这样为 postgres 创建一个 Dockerfile
    FROM postgres:12
    COPY ./init.sql /docker-entrypoint-initdb.d/init.sql
    CMD ["docker-entrypoint.sh", "postgres"]

    那么它应该工作。
    希望我的回答能帮到你!

    关于postgresql - Docker-Compose + Postgres :/docker-entrypoint-initdb. d/init.sql:权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60457838/

    44 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com