4

I am using docker-compose to create docker image combining postgresql and python application using the database.

I want to run several sql init scripts when I start the container. For this I am using volumes specified in docker-compose.yml (at the last line of the file):

version: '3.6'

services:
  parking_lots_app:
    build: .
    depends_on:
      - parking_lots_db
    environment:
      STAGE: test
    networks:
      - default
    ports:
      - 5000:5000
    restart: always

  parking_lots_db:
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 123456
    image: kartoza/postgis
    networks:
      - default
    ports:
      - 5432:5432
    restart: always
    volumes:
      - ./postgresql/:/docker-entrypoint-initdb.d/

The problem is these init scripts are only run the first time I run docker-compose up. When I tried it on different machine it again worked for the first time, but second time it did not run the scripts.

I tried removing all containers, images, volumes on the machine. I tried rebooting and restarting docker. I tried changing the name of the directory with the sql scripts to something different than "postgresql". Nothing seems to help.

I am running Gentoo-5.4.38.

Does anyone have a clue, what's wrong? I've been struggling for far too long.

Thanks

4
  • 3
    This is by design. The postgres docker page says this: Warning: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup. Commented Jun 4, 2020 at 12:26
  • 1
    It's better to have your application connect to the database itself and run the scripts, if that's the challenge you're running into. You might also look to see if your application framework has a database migration system which can do this setup for you, Docker or otherwise. Commented Jun 4, 2020 at 12:35
  • @Jeremy Removed all containers, images, volumes and everything. That should remove all changes made. Commented Jul 15, 2020 at 16:15
  • @DavidMaze Thanks a lot for the advice, I guess it's the only way. Commented Jul 15, 2020 at 16:16

1 Answer 1

2

docker-compose up -d --build rebuilds the container and reruns the scripts in /docker-entrypoint-initdb.d/

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

Comments

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.