Stack Exchange
Correct way to change tcp_keepalives settings in postgres docker container
I want to change the following parameters of postgres config, tcp_keepalives_count,  tcp_keepalives_idle, tcp_keepalives_interval.
What is the recommended way to change them?
Following is my 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
I ended up modifying my 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


You can put your options in a postgresql.conf file and change the postgres command to point to the custom config. If you want to do this for all your databases, you can build a custom image from the base image with a simple Dockerfile:
FROM postgres
COPY ./postgresql.conf /etc/postgresql/postgresql.conf
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]

Where the conf file can have whatever settings you need:
tcp_keepalives_count=10
tcp_keepalives_idle=60
tcp_keepalives_interval=60

Build that image and run it, and you'll get all the behaviour of the default image (initialising the DB, setting passwords etc.) but with your custom config:
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) 

If you don't want to build an image, you can do something similar in your Compose file: add a volume which mounts a local conf file into the container, and specify the location in a custom command.

Thanks, @Elton, although I ended up changing my docker-compose file, with a command param. I believe it will have the same effect, right?
