Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker postgres does not run init file in docker-entrypoint-initdb.d

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

I have init.sql that 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 work fine, but the database ronda is not created.

like image 781
Shulhi Sapli Avatar asked Jan 09 '16 08:01

Shulhi Sapli


3 Answers

If your initialisation requirements are just to create the ronda schema, then you could just make use of the POSTGRES_DB environment variable as described in the documentation.

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

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

On a side note, do not use restart: always for your data container as this container does not run any service (just the true command). Doing this you are basically telling Docker to run the true command in an infinite loop.

like image 135
Thomasleveil Avatar answered Nov 19 '22 18:11

Thomasleveil


This is how I use postgres on 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 load the file named pg_dump.backup(binary dump) or psql_dump.sql(plain text dump) if exist on root folder of the project.

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/

In case of need retry the loading of the dump, you can remove the current database with the command:

docker-compose rm db

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

like image 31
nsantana Avatar answered Nov 19 '22 19:11

nsantana


Had the same problem with postgres 11.

Some points that helped me:

  • run:
    • docker-compose rm
    • docker-compose build
    • docker-compose up
  • The obvious: don't run compose in detached mode. You want to see the logs.

After adding the step docker-compose rm to the mix it worked, finally.

like image 25
user2366975 Avatar answered Nov 19 '22 18:11

user2366975