更正postgres docker容器中tcp_keepalives设置的方法

我想改变下面的参数postgresconfiguration, tcp_keepalives_counttcp_keepalives_idletcp_keepalives_interval

推荐的方法是什么?

以下是我docker-compose.yml

 postgres: restart: always image: postgres:latest volumes: - /data:/var/lib/postgresql ports: - "5432:5432" environment: - POSTGRES_USER=admin - POSTGRES_PASSWORD=postgres - POSTGRES_DB=postgres 

UPDATE

我最终修改了我的docker-compose file

 postgres: restart: always image: postgres:latest container_name: postgres volumes: - /data:/var/lib/postgresql ports: - "5432:5432" environment: - POSTGRES_USER=admin - POSTGRES_PASSWORD=postgres - POSTGRES_DB=postgres command: postgres -c tcp_keepalives_idle=60 -c tcp_keepalives_interval=60 -c tcp_keepalives_count=60 

您可以将您的选项放在postgresql.conf文件中,并将postgres命令更改为指向自定义configuration。 如果您想为所有数据库执行此操作,则可以使用简单的Dockerfile从基本映像构build自定义映像:

 FROM postgres COPY ./postgresql.conf /etc/postgresql/postgresql.conf CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"] 

conf文件可以有任何你需要的设置:

 tcp_keepalives_count=10 tcp_keepalives_idle=60 tcp_keepalives_interval=60 

构build该映像并运行它,然后您将获得默认映像的所有行为(初始化数据库,设置密码等),但使用自定义configuration:

 docker exec -it 0a bash root@0a3e2cc18b76:/# psql -U postgres psql (9.5.4) Type "help" for help. postgres=# SHOW config_file; config_file --------------------------------- /etc/postgresql/postgresql.conf (1 row) 

如果您不想构build映像,可以在Compose文件中做类似的操作:添加一个将本地conf文件装载到容器中的卷,并在自定义命令中指定位置。