Postgres Docker does not run initialization file in docker-entrypoint-initdb.d

Based on the documentation of Docker Postgres, I can create any *.sql file inside /docker-entrypoint-initdb.d and run it automatically.

I have init.sql which contains CREATE DATABASE ronda;

In my docker-compose.yaml I have

 web: restart: always build: ./web expose: - "8000" links: - postgres:postgres volumes: - /usr/src/app/static env_file: .env command: /usr/local/bin/gunicorn ronda.wsgi:application -w 2 -b :8000 nginx: restart: always build: ./nginx/ ports: - "80:80" volumes: - /www/static volumes_from: - web links: - web:web postgres: restart: always build: ./postgres/ volumes_from: - data ports: - "5432:5432" data: restart: always build: ./postgres/ volumes: - /var/lib/postgresql command: "true" 

and my postgres dockerfile,

 FROM library/postgres RUN mkdir -p /docker-entrypoint-initdb.d COPY init.sql /docker-entrypoint-initdb.d/ 

Running docker-compose build and docker-compose up works fine, but the ronda database ronda not been created.

+5
docker postgresql docker-compose
source share
3 answers

If your initialization requirements are simply for creating a ronda schema, then you can simply use the POSTGRES_DB environment POSTGRES_DB as described in the documentation .

The bit of your docker-compose.yml file for the postgres service will be:

 postgres: restart: always build: ./postgres/ volumes_from: - data ports: - "5432:5432" environment: POSTGRES_DB: ronda 

Note: do not use restart: always for your data container as this container does not start any service (only the true command). By doing this, you basically tell Docker that you need to execute the true command in an infinite loop.

+2
source share

This is how I use postgres for my projects and preload the database.

file: docker-compose.yml

  db: container_name: db_service build: context: . dockerfile: ./Dockerfile.postgres ports: - "5432:5432" volumes: - /var/lib/postgresql/data/ 

This Dockerfile uploads a file called pg_dump.backup (binary dump) or psql_dump.sql (empty text dump) if it exists in the project root folder.

file: Dockerfile.postgres

 FROM postgres:9.6-alpine ENV POSTGRES_DB DatabaseName COPY pg_dump.backup . COPY pg_dump.sql . RUN [[ -e "pg_dump.backup" ]] && pg_restore pg_dump.backup > pg_dump.sql # Preload database on init RUN [[ -e "pg_dump.sql" ]] && cp pg_dump.sql /docker-entrypoint-initdb.d/ 

If necessary, repeat loading the dump, you can delete the current database using the command:

 docker-compose rm db 

Then you can run docker-compose up to reload the database.

0
source share

All you need to do is Add save the file as follows:

 FROM library/postgres ADD init.sql /docker-entrypoint-initdb.d/ 

note that the file must be in the same folder as your Docker file, and that you do not need to create a directory using the mkdir command.

-one
source share

All Articles