This question is related to this one: Docker - How can run the psql command in the postgres container?
But the solution provided above is only valid when you are doing things manually while here one would like to do things automatically using docker-compose.
So this docker container works:
FROM postgres
COPY init.sql /docker-entrypoint-initdb.d/
but this docker container fails:
FROM postgres
RUN psql -U postgres -c "CREATE USER user WITH PASSWORD 'pass';"
because by the time the RUN is executed the postgresql server has not started yet!
Is there a way to overcome this limitation?