15

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
5
  • Looks right to me. If you exec into the postgres container can you see your file in /docker-entrypoint-initdb.d/. Try: docker-compose exec db ls -al /docker-entrypoint-initdb.d/. Also posting the logs may help: docker-compose logs db Commented Nov 2, 2016 at 18:17
  • 1
    Well, TIL about /docker-entrypoint-initdb.d/ in the postgres container, so thanks for that! :) Commented Nov 2, 2016 at 19:34
  • The file is there. This is the log from db: Commented Nov 3, 2016 at 10:15
  • 1
    db_1 | LOG: database system was interrupted; last known up at 2016-11-02 17:11:33 UTC db_1 | LOG: database system was not properly shut down; automatic recovery in progress db_1 | LOG: invalid record length at 0/14EE338: wanted 24, got 0 db_1 | LOG: redo is not required db_1 | LOG: MultiXact member wraparound protections are now enabled db_1 | LOG: database system is ready to accept connections db_1 | LOG: autovacuum launcher started Commented Nov 3, 2016 at 10:16
  • 1
    db_1 | FATAL: password authentication failed for user "user" db_1 | DETAIL: Role "user" does not exist. db_1 | Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5" Commented Nov 3, 2016 at 10:17

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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>.

3 Comments

Thanks man, but I tried this too. It's not working. The script is copied, but the container doesn't run the script when starts database. How I say, I tried many times with many different configs, including like a mount volume. And nothing. For a while, I am using an Ubuntu image with postgresql instalation, and it's working.
Looks like you're running into this issue: github.com/docker-library/postgres/issues/193
Stupid suggestion: chmod 755 dev_data_setup.sql before you build. docker doesn't run things that aren't executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.