简体   繁体   English

在 docker-compose 中运行的 Postgres 无法将数据写入已安装的卷

[英]Postgres running in docker-compose unable to write data to mounted volume

Description描述

I am running a Postgres container in docker-compose.我在 docker-compose 中运行 Postgres 容器。 I am mounting the /data directory needed by Postgres into the container using a volume in the docker-compose.yml below.我正在使用下面docker-compose.yml中的卷将 Postgres 所需的/data目录安装到容器中。

Qualifications资格

  1. The Postgres user must be called graph-node and create a database called graph-node Postgres 用户必须被称为graph-node并创建一个名为graph-node的数据库
  2. I delete the data/postgres/ folder before each docker-compose up using the boot.sh script below for application-specific reasons.出于特定于应用程序的原因,我使用下面的boot.sh脚本删除了每个docker-compose up之前的data/postgres/文件夹。 Just know that /data/postgres is re-created on each run of docker-compose up .只要知道/data/postgres在每次运行docker-compose up时都会重新创建。

Expected Behavior Postgres boots and writes all files it needs to the mounted /data/postgres volume.预期行为Postgres 启动并将它需要的所有文件写入已挂载的/data/postgres卷。

Actual Behavior Postgres boots fine, but writes nothing to the volume.实际行为Postgres 可以正常启动,但不会向卷中写入任何内容。

Possible Reasons可能的原因

This feels like a read/write permissions problem?这感觉像是读/写权限问题? I've added :rw in the third column of the volume as suggested, still no cigar.我按照建议在卷的第三栏中添加了:rw ,仍然没有雪茄。 I run chmod -R a+rwx./data on the data dir to get access to all files recursively.我在数据目录上运行chmod -R a+rwx./data以递归方式访问所有文件。

The oddest thing to is that if I manually run chmod -R a+rwx./data after booting, Postgres suddenly IS able to write to the directory all needed files.最奇怪的是,如果我在启动后手动运行chmod -R a+rwx./data ,Postgres 突然能够将所有需要的文件写入目录。 But if I run this before it's created as seen below (recursively for all things in /data ) it does not work.但是,如果我在创建它之前运行它,如下所示(递归地用于/data中的所有事物)它不起作用。

Files文件

boot.sh引导程序

# Check for data/ dir. If found, make it recursively rwx for all users. Otherwise, create it and make it recursively rwx for all users.

if [ -d "./data" ]
then
    chmod -R a+rwx ./data
else
    mkdir data
    chmod -R a+rwx ./data
fi

if [ -d "./data/postgres" ]
then
    rm -rf data/postgres
else
    echo "No data/postgres dir found. Proceeding"
fi

docker-compose -f docker-compose.yml up

docker-compose.yml docker-compose.yml

version: "3"
services:
  postgres:
    image: postgres
    ports:
      - '5432:5432'
    command:
      [
        "postgres",
        "-cshared_preload_libraries=pg_stat_statements"
      ]
    environment:
      POSTGRES_USER: graph-node
      POSTGRES_PASSWORD: let-me-in
      POSTGRES_DB: graph-node
    volumes:
      - ./data/postgres:/var/lib/postgresql/data:rw

Machine + Software Specs机器 + 软件规格

Operating System: Windows 10, WSL2, Ubuntu操作系统: Windows 10、WSL2、Ubuntu

Docker Version: 20.10.7 (running directly on the machine since it's Ubuntu, NOT in Docker Desktop like on a Mac) Docker 版本: 20.10.7(直接在机器上运行,因为它是 Ubuntu,不像 Mac 上的 Docker 桌面)

have you run this script as an elevated user?您是否以提升用户身份运行此脚本? specifically sudo?特别是须藤?

Well, not exactly an answer, but because I only needed one-run ephemeral storage for Postgres (I was deleting the data/ dir between runs anyways) I solved the problem by just removing the external volume and letting Postgres write data into the container itself, where it surely had privileges.好吧,不完全是一个答案,但是因为我只需要 Postgres 的一次运行临时存储(无论如何我都在删除运行之间的数据/目录),所以我通过删除外部卷并让 Postgres 将数据写入容器本身来解决了这个问题,它肯定有特权。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM