Docker Community Forums

Share and learn in the Docker community.

Postgres user password change not working from Dockerfile

docker

(Deepanshree89) #1

Hi,
I am new to Docker. I have created a docker file for postgres9.3 image creation as given below.

FROM centos

#Setting the proxy
ENV http_proxy=http://xxxxxxxx:yyyy https_proxy=http://xxxxxxxxx:yyyy

RUN mkdir -p /home/postgres
#explicitly set user/group IDs
RUN groupadd -r postgres --gid=123 && useradd -r -g postgres -d /home/postgres --uid=123 postgres
RUN chown postgres:postgres /home/postgres

#make the “en_US.UTF-8” locale so postgres will be utf-8 enabled by default
RUN localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8

Install EPEL6 for additional packages

#RUN yum -y install http://mirror.pnl.gov/epel/6/i386/epel-release-6-8.noarch.rpm

Install Development Tools

RUN yum -y groupinstall “Development Tools”

install pg repo

RUN rpm -i http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm

#Install server
RUN yum install -y postgresql93-server postgresql93-contrib

#RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql

RUN mkdir -p /pg_home && chown -R postgres:postgres /pg_home

RUN chmod 700 /pg_home

ENV PGDATA=/pg_home

RUN mkdir -p /pg_log && chown -R postgres:postgres /pg_log
RUN chmod 755 /pg_log

RUN su - postgres -c ‘/usr/pgsql-9.3/bin/initdb -D /pg_home’

RUN su - postgres -c 'echo “local all all trust” >> /pg_home/pg_hba.conf’
RUN su - postgres -c 'echo “local all all md5” >> /pg_home/pg_hba.conf’
RUN su - postgres -c 'echo “host all all 127.0.0.1/32 md5” >> /pg_home/pg_hba.conf’
RUN su - postgres -c 'echo “host all all ::1/128 md5” >> /pg_home/pg_hba.conf’
RUN su - postgres -c 'echo “host all all 0.0.0.0/0 md5” >> /pg_home/pg_hba.conf’
RUN su - postgres -c ‘echo “host all all 0.0.0.0/0 trust” >> /pg_home/pg_hba.conf’

RUN su - postgres -c "echo “listen_addresses=’*’” >> /pg_home/postgresql.conf"
RUN sed -i ‘s/max_connections=100/max_connections=150/g’ /pg_home/postgresql.conf
RUN sed -i ‘s/pg_log//pg_log/g’ /pg_home/postgresql.conf

VOLUME /pg_home
VOLUME /pg_log

ADD ./start_postgres.sh /start_postgres.sh

RUN chmod 755 /start_postgres.sh

EXPOSE 5432

CMD ["/start_postgres.sh"]

The Shell script mentioned in the dockerfile “start_postgres.sh” contains the start command for postgres and changing the password for postgres user as below. But for some reason, its working till the postgres startup and the password change is not happening. Need help on this.

#!/bin/bash

su - postgres -c '/usr/pgsql-9.3/bin/postgres -D /pg_home’
su - postgres -c “psql -U postgres -c “alter user postgres with password ‘xxxxxxx’;””


(Peterducai) #2

use postgres image or look at their Dockerfile (which is long and nasty) how they do it… Dont reinvent the wheel!