Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker PostgreSQL - Scripts in /docker-entrypoint-initdb.d doesn't run

So, I have a docker-compose project with this structure:

DockerDev
- docker-compose.yaml
- d-php
  - Dockerfile
  - scripts-apache
- d-postgresql
  - Dockerfile
  - scripts
    - dev_data_setup.sql
- logs
- pgdata
- www

PHP, Redis, ElasticSearch is OK. But Postgresql doesn't run dev_data_setup.sql, with any diferent solutions to /dockes-entrypoint-initdb.d that I found (volume, ADD, COPY, etc). I tried to run and sh script and nothing.

Could you see this docker-compose and Dockerfile and help me? Thanks

Dockerfile:

FROM postgres:latest
ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d

docker-compose.yaml:

version: '2'
services:
  php:
    build: ./d-php/
    hostname: www.domain.com
    ports:
      - "80:80"
    volumes:
      - ./www:/var/www/html
      - ./d-php/scripts-apache2/apache2.conf:/etc/apache2/apache2.conf
      - ./d-php/scripts-apache2/web.conf:/etc/apache2/sites-enabled/web.conf
      - ./d-php/scripts-apache2/webservice.conf:/etc/apache2/sites-enabled/webservice.conf
      - ./logs:/var/log/apache2
    links:
      - db
      - redis
      - elasticsearch
  db:
    build: ./d-postgresql/
    volumes:
      - ./pgdata:/pgdata
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PGDATA=/pgdata
  redis:
    image: redis:latest
  elasticsearch:
    image: elasticsearch:2.4.1
like image 769
Rodrigo Saraiva Avatar asked Nov 02 '16 17:11

Rodrigo Saraiva


2 Answers

So I found the problem.

  • First: my sql script was trying to recreate postgres user. then, the dockedev_db exited.
  • Second: I needed to remove all images related to db to docker-compose run the script again.

Thanks for your help.

like image 156
Rodrigo Saraiva Avatar answered Oct 01 '22 10:10

Rodrigo Saraiva


Your problem is caused by the way you use ADD in your Dockerfile:

FROM postgres:latest
ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d

This creates a file called /docker-entrypoint-initdb.d with the content of the dev_data_setup.sql file. What you want is to treat /docker-entrypoint-initdb.d as a directory.

You should change your ADD command to one of the following:

ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d/

The trailing slash will treat the dest parameter as a directory. Or use

ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d/dev_data_setup.sql

Which will specifically spell out the file name.

Reference: https://docs.docker.com/engine/reference/builder/#/add

If <dest> does not end with a trailing slash, it will be considered a regular file and the contents of <src> will be written at <dest>.

like image 41
nwinkler Avatar answered Oct 01 '22 09:10

nwinkler