Configure dockerfile with postgres Configure dockerfile with postgres postgresql postgresql

Configure dockerfile with postgres


When building your docker image postgres is not running. Database is started when container is starting, any sql files can be executed after that. Easiest solution is to put your sql files into special directory:

FROM postgres:9.4COPY *.sql /docker-entrypoint-initdb.d/

When booting startup script will execute all files from this dir. You can read about this in docs https://hub.docker.com/_/postgres/ in section How to extend this image.

Also, if you need different user you should set environment variables POSTGRES_USER and POSTGRES_PASSWORD. It's easier then using custom scripts for creating user.


As the comment above says during the image build you don't get a running instance of Postgres.

You could take slightly different approach. Instead of trying to execute SQL scripts yourself you could copy them to /docker-entrypoint-initdb.d/ directory. They will be executed when the container starts up.

Have a look how postgres:9.4 image is build:

Also in your Dockerfile use variables to set database details:

  • POSTGRES_DB
  • POSTGRES_USER
  • POSTGRES_PASSWORD