问题
I have created Docker file which looks like this
db:
image: postgres:10.3
ports:
- "5432:5432"
environment:
POSTGRES_DB: test
POSTGRES_USER: test
POSTGRES_PASSWORD: changeme
restart: always
volumes:
- ./srv/postgres:/var/lib/postgresql/data
- ./init/:/docker-entrypoint-initdb.d/
I have a sql script in ./init path which have sql commands to create database and user. I see the sql file created in the postgres container but somehow it is not executing. I have followed the documentation on the docker hub. I am not sure what is wrong here?
回答1:
I have experienced this as well. The problem is that the scripts inside of /docker-entrypoint-initdb.d/
will only be ran if the database has not already been initialized.
If you stop the container, delete the contents of ./srv/postgres/
and then recreate the container your *.sql
scripts inside of ./init
will be ran.
However doing this will cause your data to be lost.
See here for a bit more info.
来源:https://stackoverflow.com/questions/50703184/issue-with-docker-compose-initial-db-setup-once