Docker Community Forums

Share and learn in the Docker community.

Initializing postgres database from DockerFile on docker Windows Host


(Pcouas) #1

Hi,

I have made an DockerFile from Centos7 with Postgres 9.5.
I could see in screen my CREATE DATABASE
but aftter dockerfile finish when i connect to postgres with psql, my database is not created ?
Why ?

Regards


(Fiberoptic) #2

Hello,

Give your Dockerfile and how you launched tour container.
Did you use the official postgres image? You would not have to create the database.

Nicolas


(Pcouas) #3

“ported” by Adam Miller maxamillion@fedoraproject.org from

https://github.com/fedora-cloud/Fedora-Dockerfiles

Originally written for Fedora-Dockerfiles by

scollier scollier@redhat.com

FROM centos:centos7
MAINTAINER The CentOS Project cloud-ops@centos.org

RUN yum -y update; yum clean all
RUN yum -y install sudo epel-release; yum clean all
RUN yum -y install postgresql-server postgresql postgresql-contrib supervisor pwgen; yum clean all

RUN mkdir -p /home/pgdata
ADD ./postgresql-setup /usr/bin/postgresql-setup
ADD ./supervisord.conf /etc/supervisord.conf
ADD ./start_postgres.sh /start_postgres.sh

#RUN mkdir /var/lib/pgsql/data
#Sudo requires a tty. fix that.
RUN sed -i ‘s/.*requiretty$/#Defaults requiretty/’ /etc/sudoers
RUN chmod +x /usr/bin/postgresql-setup
RUN chmod +x /start_postgres.sh

RUN /usr/bin/postgresql-setup initdb

ADD ./postgresql.conf /home/pgdata/data/postgresql.conf

RUN chown -v postgres.postgres /home/pgdata/data/postgresql.conf

RUN echo “host all all 0.0.0.0/0 md5” >> /home/pgdata/data/pg_hba.conf

run /start_postgres.sh postgres postgres my_database &

VOLUME ["/home/pgdata"]

EXPOSE 5432

CMD ["/bin/bash", “/start_postgres.sh”]

##for testing
#docker build -t v17 .

#docker run --rm -it --entrypoint=/bin/bash v17
#/start_postgres.sh &


#!/bin/bash

DB_NAME=${POSTGRES_DB:-}
DB_USER=${POSTGRES_USER:-}
DB_PASS=${POSTGRES_PASSWORD:-}
#PG_CONFDIR="/var/lib/pgsql/data"
PG_CONFDIR="/home/pgdata/data"

__create_user() {
echo "aaa"
echo "bbbb “${DB_USER}”…"
echo “ccc “${DB_PASS}”…”
#Grant rights
usermod -G wheel postgres

Check to see if we have pre-defined credentials to use

if [ -n “${DB_USER}” ]; then
if [ -z “${DB_PASS}” ]; then
echo ""
echo "WARNING: "
echo "No password specified for “${DB_USER}”. Generating one"
echo ""
DB_PASS=$(pwgen -c -n -1 12)
echo "Password for “${DB_USER}” created as: “${DB_PASS}”"
fi
echo "Creating user “${DB_USER}”…"
echo “CREATE ROLE ${DB_USER} with CREATEROLE login superuser PASSWORD ‘${DB_PASS}’;” |
sudo -u postgres -H postgres --single
-c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}

fi

if [ -n “${DB_NAME}” ]; then
echo "Creating database “${DB_NAME}”…"
echo “CREATE DATABASE ${DB_NAME};” |
sudo -u postgres -H postgres --single
-c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}

if [ -n “${DB_USER}” ]; then
echo "Granting access to database “${DB_NAME}” for user “${DB_USER}”…"
echo “GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} to ${DB_USER};” |
sudo -u postgres -H postgres --single
-c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}
fi
fi
}

__run_supervisor() {
supervisord -n
}

export DB_USER=$1
export DB_PASS=$2
export DB_NAME=$3

Call all functions

__create_user
__run_supervisor


(Somashekar) #4

Hi,

Please provide me the dockerfile to create postgres on docker windows i am searching but i am unable to get the proper dockerfile.