1

I am using the official postgresql docker image (version 9.4). I have extended the Dockerfile, so I can alter the settings in the postgresql.conf etc, using a bash script. It successfully adds and runs the script on entrypoint, for a single sed command. But when I put 2 or more sed commands, I get the following error:

/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/config.sh
: No such file or directoryread /var/lib/postgresql/data/postgresql.conf

I am trying on Windows 10, in combination with Vagrant and VirtualBox, using NFS file system on shared folders, via the vagrant-winnfsd plugin.

Why is this happening? How can I alter my bash script in order to work with more configuration settings? Is there a better way?

Dockerfile:

FROM postgres:9.4

RUN echo "Europe/Athens" > /etc/timezone \
 && dpkg-reconfigure -f noninteractive tzdata

RUN localedef -i el_GR -c -f UTF-8 -A /usr/share/locale/locale.alias el_GR.UTF-8

ADD config.sh /docker-entrypoint-initdb.d/
RUN chmod 755 /docker-entrypoint-initdb.d/config.sh

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

config.sh:

#!/bin/bash
sed -i -e"s/^#logging_collector = off.*$/logging_collector = on/" /var/lib/postgresql/data/postgresql.conf
sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" /var/lib/postgresql/data/postgresql.conf

database.yml

postgres:
    container_name: postgres-9.4
    image: ***/postgres-9.4
    volumes_from:
      - postgres_data
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=database
      - USERMAP_UID=999
      - USERMAP_GID=999

postgres_data:
    container_name: postgres_data
    image: ***/postgres-9.4
    volumes:
      - ./services/postgres:/etc/postgresql
      - ./services/postgres:/var/lib/postgresql
      - ./services/postgres/logs:/var/log/postgresql
    command: "true"
mallix
  • 1,399
  • 1
  • 21
  • 44
  • Possible duplicate of [How to customize the configuration file of the official PostgreSQL docker image?](http://stackoverflow.com/questions/30848670/how-to-customize-the-configuration-file-of-the-official-postgresql-docker-image) – Vojtech Vitek - golang.cz Nov 14 '16 at 21:37

2 Answers2

1

You might want to try using a RUN statement to execute your bash script or just run sed directly with both commands combined with a semicolon:

RUN sed -i -e 's/^#\(logging_collector = \).*/\1on/; s/^\(max_connections = \).*/\11000/' \
    /var/lib/postgresql/data/postgresql.conf

A more scalable solution would be to put the sed program in an external file, then use these statements:

ADD postgres-edit.sed /var/local
RUN sed -i -f /var/local/postgres-edit.sed /var/lib/postgresql/data/postgresql.conf

postgres-edit.sed:

# sed script to edit postgresql configuration

s/^#\(logging_collector = \).*/\1on/
s/^\(max_connections = \).*/\11000/
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
  • If I use this inside my Dockerfile, it would not work. – mallix Mar 27 '16 at 20:07
  • You said that a single sed command works. If you're editting a single file, there's no need to call sed more than once. Just join the commands with semi colons as I've done. Or put them in a file one command per line with no semi colons and call it with: `sed -i -f postgresql-edit.sed /var/lib/postgresql/data/postgresql.conf`. – Cole Tierney Mar 27 '16 at 21:19
  • You definitely got me to the right path. I put all my configs inside the postgresql-edit.sed. But still, I used the config.sh to execute the sed -i -f command. Getting this done via the Dockerfile with ADD sed -i -f, simply does not work. Thank you. – mallix Mar 29 '16 at 22:03
-1

Seems like a duplicate of How to customize the configuration file of the official PostgreSQL docker image?.

Copy-paste of my answer at https://stackoverflow.com/a/40598124/385548.


Inject custom postgresql.conf into postgres Docker container

The default postgresql.conf file lives within the PGDATA dir (/var/lib/postgresql/data), which makes things more complicated especially when running postgres container for the first time, since the docker-entrypoint.sh wrapper invokes the initdb step for PGDATA dir initialization.

To customize PostgreSQL configuration in Docker consistently, I suggest using config_file postgres option together with Docker volumes like this:

Production database (PGDATA dir as Persistent Volume)

docker run -d \
-v $CUSTOM_CONFIG:/etc/postgresql.conf \
-v $CUSTOM_DATADIR:/var/lib/postgresql/data \
-e POSTGRES_USER=postgres \
-p 5432:5432 \
--name postgres \
postgres:9.6 postgres -c config_file=/etc/postgresql.conf

Testing database (PGDATA dir will be discarded after docker rm)

docker run -d \
-v $CUSTOM_CONFIG:/etc/postgresql.conf \
-e POSTGRES_USER=postgres \
--name postgres \
postgres:9.6 postgres -c config_file=/etc/postgresql.conf

Debugging

  1. Remove the -d (detach option) from docker run command to see the server logs directly.
  2. Connect to the postgres server with psql client and query the configuration:

    docker run -it --rm --link postgres:postgres postgres:9.6 sh -c 'exec psql -h $POSTGRES_PORT_5432_TCP_ADDR -p $POSTGRES_PORT_5432_TCP_PORT -U postgres'
    
    psql (9.6.0)
    Type "help" for help.
    
    postgres=# SHOW all;
    
Community
  • 1
  • 1
Vojtech Vitek - golang.cz
  • 25,275
  • 4
  • 34
  • 40