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

时间:2018-02-19 09:09:37

标签: postgresql docker

目前,我想为postgres挂载日志记录文件夹,这样即使在主机重启后,仍然会保留日志记录信息。

我使用以下docker-compose.yml

搬运工-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:

但是,我收到以下权限被拒绝错误。

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

任何人都知道如何解决此类错误?

2 个答案:

答案 0 :(得分:1)

第二次修改

经过一些阅读后,似乎无法以您需要的方式执行此操作。在使用docker-compose声明卷时,您需要能够定义文件所有权,这是docker引擎不支持的。但是您可以考虑一些解决方法,请查看here以获取更多详细信息。

作为一种解决方法,您可以创建一个扩展postgres的Dockerfile并添加:

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

修改

经过一些实验,这是有效的:

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:

原始回答

我不小心通过这样做了,首先使用这个docker-compose文件运行docker-compose:

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:

这失败了:

  

postgres_1 | "根"执行PostgreSQL服务器不是   允许的。 postgres_1 |服务器必须在a下启动   无特权的用户ID,以防止postgres_1 |可能的系统安全   妥协。请参阅postgres_1的文档更多信息   关于如何正确启动服务器。

将更改恢复为命令并再次运行后工作,但显然这不是一个解决方案,所以坚持上面编辑的答案。

答案 1 :(得分:1)

问题在于主机中的许可权,而不是容器中的许可权。

所以在主机上,

   chmod -R 777 postgres_logs