After some lecture & exercises I managed to successfully run such a Dockerfile
:
FROM postgres:9
ENV PG_DATA=/usr/data
RUN mkdir -p $PG_DATA
COPY schema.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/schema.sh
COPY .env /usr/local/bin/
COPY source.sh $PG_DATA
RUN chmod +x $PG_DATA/source.sh
RUN $PG_DATA/source.sh
ENV PGDATA=/var/lib/postgresql/data
with contents as follow:
source.sh
#!/bin/bash -x
TEXT_INN="set -a && . .env && set +a"
sed -i "2 a $TEXT_INN " /usr/local/bin/docker-entrypoint.sh
.env
#POSTGRES_USER=adminn
POSTGRES_PASSWORD=verysecret
#POSTGRES_DB=somedata
POSTGRE_ROOT_USER=adminn
POSTGRE_ROOT_PASSWORD=lesssecret
POSTGRE_TABLE=sometable
POSTGRE_DATABASE=somedata
schema.sh
#!/bin/bash
set -a && . .env && set +a
psql -v ON_ERROR_STOP=1 -d template1 -U postgres <<-EOSQL
CREATE USER $POSTGRE_ROOT_USER WITH PASSWORD '$POSTGRE_ROOT_PASSWORD';
CREATE DATABASE $POSTGRE_DATABASE WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8';
ALTER DATABASE $POSTGRE_DATABASE OWNER TO $POSTGRE_ROOT_USER;
SELECT usename, usesuper, usecreatedb FROM pg_catalog.pg_user ORDER BY usename;
SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema NOT LIKE ALL (ARRAY['pg_catalog','information_schema']) ORDER BY table_schema,table_name;
EOSQL
Container (with funny name) is running but I can not connect to it out the docker. Try at default port localhost:5432/somedata
or even localhost:5432/postgres
but it does not connect. How can I find useful url to reach out of the docker?