[英]Initialize postgres database (without using /docker-entrypoint-initdb.d)
I'm trying to initialize a database without using the entry point directory . 我试图在不使用入口点目录的情况下初始化数据库。
Here is a minimal Dockerfile: 这是最小的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
: 和script.sh
:
psql -d db -U user < /directory/db.sql
This does not work because postgres isn't up when the script is run. 这不起作用,因为运行脚本时postgres没有启动。
How can I run db.sql
without using /docker-entrypoint-initdb.d
? 如何在不使用/docker-entrypoint-initdb.d
情况下运行db.sql
?
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. 如果查看标准的postgres
映像的入口点脚本 ,支持/docker-entrypoint-initdb.d
目录的机制非常复杂:它需要引导数据库目录以及初始用户和数据库,然后它将在后台启动数据库服务器并运行该目录中的所有内容,最后运行数据库。 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 如果您有一个通常在运行psql
客户端工具的数据库上运行的SQL文件,则可以使用Docker执行完全相同的操作
docker run -d -p 5432:5432 --name postgres postgres:12
psql -h localhost < db.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.