0

I am pulling the postgres:12.0-alpine docker image to build my database. My intention is to replace the postgresql.conf file in the container to reflect the changes I want (changing data directory, modify backup options etc). I am trying with the following docker file

FROM postgres:12.0-alpine

# create the custom user 
RUN addgroup -S custom && adduser -S custom_admin -G custom

# create the appropriate directories 
ENV APP_HOME=/home/data 
ENV APP_SETTINGS=/var/lib/postgresql/data 
WORKDIR $APP_HOME

# copy entrypoint.sh 
COPY ./entrypoint.sh $APP_HOME/entrypoint.sh

# copy postgresql.conf 
COPY ./postgresql.conf $APP_HOME/postgresql.conf

RUN chmod +x /home/data/entrypoint.sh

# chown all the files to the app user 
RUN chown -R custom_admin:custom $APP_HOME 
RUN chown -R custom_admin:custom $APP_SETTINGS

# change to the app user 
USER custom_admin

# run entrypoint.sh 
ENTRYPOINT ["/home/data/entrypoint.sh"]

CMD ["custom_admin"]

my entrypoint.sh looks like

#!/bin/sh

rm /var/lib/postgresql/data/postgresql.conf
cp ./postgresql.conf /var/lib/postgresql/data/postgresql.conf

echo "replaced .conf file"

exec "$@"

But I get an exec error saying 'custom_admin: not found on the 'exec "$@"' line. What am I missing here?

neoceph
  • 75
  • 11

2 Answers2

2

In order to provide a custom configuration. Please use the below command:

docker run -d --name some-postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf postgres -c 'config_file=/etc/postgresql/postgresql.conf'

Here my-postgres.conf is your custom configuration file.

Refer the docker hub page for more information about the postgres image.

Thilak
  • 935
  • 8
  • 12
  • This does not work for changing directory. The directory needs to have appropriate privileges which are not working according to the #2 and #3 of the "Arbitrary --user Notes" on the docker hub page. Did you have any success running the postgres container as a different user? – neoceph Dec 30 '19 at 00:00
  • @neoceph Why do you want to run it as different user? I haven't faced any permission denied issue while using a different config file. – Thilak Jan 02 '20 at 05:42
1

Better to use the suggested answer by @Thilak, you do not need custom image to just use the custom config.

Now the problem with the CMD ["custom_admin"] in the Dockerfile, any command that is passed to CMD in the Dockerfile, you are executing that command in the end of the entrypoint, normally such command refers to main or long-running process of the container. Where custom_admin seems like a user, not a command. you need to replace this with the process which would run as a main process of the container.

Change CMD to

CMD ["postgres"]

I would suggest modifying the offical entrypoint which do many tasks out of the box, as you own entrypoint just starting the container no DB initialization etc.

Adiii
  • 54,482
  • 7
  • 145
  • 148