0

I'm trying to initialize a database without using the entry point directory.

Here is a minimal Dockerfile:

FROM postgres:latest

POSTGRES_DB db
POSTGRES_USER user
POSTGRES_PASSWORD password

ADD db.sql /directory/
ADD script.sh /directory/

CMD ["sh", "/directory/script.sh"] 
# Or ENTRYPOINT ["/directory/script.sh"]?

And script.sh:

psql -d db -U user < /directory/db.sql

This does not work because postgres isn't up when the script is run.

How can I run db.sql without using /docker-entrypoint-initdb.d?

1 Answer 1

1

If you look at the standard postgres image's entrypoint script the mechanism to support the /docker-entrypoint-initdb.d directory is pretty intricate: it needs to bootstrap the database directory and initial user and database, then it starts the database server in the background and runs everything in that directory, and finally runs the database for real. If you're trying to replicate this setup, you have to do all of these steps yourself.

There are other ways to set up a database, though. You can create an empty database and then run your application's migrations normally to create the initial schema. If you have an SQL file that normally you'd run against a database running the psql client tool, you can do the exact same thing with Docker

docker run -d -p 5432:5432 --name postgres postgres:12
psql -h localhost < db.sql
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.