简体   繁体   English

如何为 postgres 日志文件夹设置卷 - 权限被拒绝错误

[英]How to setup volume for postgres log folder - Permission denied error

Currently, I would like to mount logging folder for postgres, so that even after host machine restart, logging information is still preserved.目前,我想为 postgres 挂载日志文件夹,以便即使在主机重启后,日志信息仍然保留。

I use the following docker-compose.yml我使用以下docker-compose.yml

docker-compose.yml docker-compose.yml

version: '2'
services:
  postgres:
    image: postgres:latest
    restart: always
    environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=55F4rGFwsXXXXXX
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - postgres_logs:/logs
    # Make Postgres log to a file.
    # More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
    command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs

volumes:
  postgres_data:
  postgres_logs:

However, I'm getting the following permission denied error.但是,我收到以下权限被拒绝错误。

postgres_1             | 2018-02-19 09:03:45.359 UTC [1] LOG:  database system is shut down
postgres_1             | 2018-02-19 09:04:45.963 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1             | 2018-02-19 09:04:45.963 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1             | 2018-02-19 09:04:45.965 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1             | 2018-02-19 09:04:45.972 UTC [1] FATAL:  could not open log file "/logs/postgresql-2018-02-19_090445.log": Permission denied
postgres_1             | 2018-02-19 09:04:45.974 UTC [1] LOG:  database system is shut down
postgres_1             | 2018-02-19 09:05:46.741 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1             | 2018-02-19 09:05:46.741 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1             | 2018-02-19 09:05:46.744 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1             | 2018-02-19 09:05:46.753 UTC [1] FATAL:  could not open log file "/logs/postgresql-2018-02-19_090546.log": Permission denied
postgres_1             | 2018-02-19 09:05:46.755 UTC [1] LOG:  database system is shut down
postgres_1             | 2018-02-19 09:06:47.366 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1             | 2018-02-19 09:06:47.366 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1             | 2018-02-19 09:06:47.368 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1             | 2018-02-19 09:06:47.375 UTC [1] FATAL:  could not open log file "/logs/postgresql-2018-02-19_090647.log": Permission denied
postgres_1             | 2018-02-19 09:06:47.377 UTC [1] LOG:  database system is shut down

Anyone has idea how can I resolve such error?任何人都知道如何解决此类错误?

2nd Edit第二次编辑

After some reading it seems that is not possible to do this the way you need.经过一些阅读后,似乎无法按照您需要的方式执行此操作。 You would need to be able to define file ownership when declaring a volume with docker-compose, and this is something that is not supported by the docker engine.在使用 docker-compose 声明卷时,您需要能够定义文件所有权,这是 docker 引擎不支持的。 But there are a few workarounds that you can consider, check here for more details.但是您可以考虑一些解决方法,请在此处查看更多详细信息。

As a workaround you could create a Dockerfile extending postgres and add this:作为一种解决方法,您可以创建一个扩展 postgres 的 Dockerfile 并添加以下内容:

# ...
RUN    mkdir /logs
RUN   chown postgres:postgres /logs

Edit编辑

After some experimentation this is working:经过一些实验,这是有效的:

version: '2'
services:
postgres:
    image: postgres:latest
    restart: always
    environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=55F4rGFwsXXXXXX
    ports:
    - "5432:5432"
    volumes:
    - postgres_data:/var/lib/postgresql/data
    - ./:/logs:z
    # Make Postgres log to a file.
    # More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
    command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs

volumes:
postgres_data:

Original answer原答案

I accidentally got it working by doing this, first running docker-compose up with this docker-compose file:我不小心这样做了,首先使用这个 docker-compose 文件运行 docker-compose up:

version: '2'
services:
postgres:
    image: postgres:latest
    restart: always
    environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=55F4rGFwsXXXXXX
    ports:
    - "5432:5432"
    volumes:
    - postgres_data:/var/lib/postgresql/data
    - postgres_logs:/logs:z
    # Make Postgres log to a file.
    # More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
    command: /bin/bash -c "mkdir -p /logs && chmod -R 777 /logs && postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs"

volumes:
postgres_data:
postgres_logs:

This fails with:这失败了:

postgres_1 | postgres_1 | "root" execution of the PostgreSQL server is not permitted.不允许 PostgreSQL 服务器的“root”执行。 postgres_1 | postgres_1 | The server must be started under an unprivileged user ID to prevent postgres_1 |服务器必须在非特权用户 ID 下启动以防止 postgres_1 | possible system security compromise.可能的系统安全妥协。 See the documentation for postgres_1 |请参阅 postgres_1 的文档 | more information on how to properly start the server.有关如何正确启动服务器的更多信息。

After reverting the changes to command and running again then works, but obviously this is not a solution so stick with the edited answer above.将更改恢复为命令并再次运行后,就可以工作了,但显然这不是解决方案,因此请坚持使用上面编辑过的答案。

the problem is the permission in host, not in container.问题是主机中的权限,而不是容器中的权限。

so in host,所以在主机中,

   chmod -R 777 postgres_logs

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

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