问题描述
我有一个postgres:9.5.6-alpine容器,另一个容器是web,必须链接到它。
我想在启动并执行docker-entrypoint.sh之后在postgres容器中运行名为 create_db.sh
的脚本。 db和一个用户并还原备份。
我的 docker-compose.yml
(postgres部分):
postgres:
构建:./postgres
container_name:postgres
卷:
-/ shared_folder / postgresql:/ var / lib / postgresql
端口:
- 5432:5432
命令:sh /home/create_db.sh
create_db.sh
的内容是:
#!/ bin / sh
psql -d template1 -U postgres
psql --command使用密码'userpassword'创建用户用户;
psql-命令 CREATE DATABASE userdb;
psql-命令将数据库userdb上的所有特权授予用户;
psql-命令 \q;
psql -U用户-d userdb -f /var/lib/postgresql/backup.sql
退出
当我运行 docker-compose build
,然后 docker-compose up
我明白了:
附加到postgres,网络
postgres | psql:无法连接到服务器:没有这样的文件或目录
服务器是否在本地运行并接受
postgres | Unix域套接字 /var/run/postgresql/.s.PGSQL.5432上连接?
我了解这是因为当我启动 create_db.sh
postgres服务器尚未准备就绪,但是如何在容器的 docker-entrypoint.sh
之后运行它?
您正在覆盖原始的命令
,并且您没有在此脚本中启动postgres,这就是您的数据库的原因
您可以将数据库初始化放入容器的入口点目录: /docker-entrypoint-initdb.d
。这将执行此目录中的所有 *。sh
和 *。sql
文件,并且不会触及原始的命令
。
创建容器时,该目录中的所有文件将按字母顺序自动执行。因此,创建一个卷以将您的脚本/ sql文件添加到入口点,并让容器执行它们。在正式的postgres文档的中对此进行了描述。。 p>
您的撰写文件然后更改为如下内容:
postgres:
构建:./postgres
卷:
-/ shared_folder / postgresql:/ var / lib / postgresql
-./db-init-scripts:/docker-entrypoint-initdb。 d
端口:
- 5432:5432
而本地目录,例如 db-init-scripts
,包含您的初始化脚本(如果需要,可以重命名)。将 create_db.sh
复制到此文件夹,当您创建新容器时,它将自动执行。
几个数据库-images监视此入口点目录,这非常方便。
您的 container_name:postgres
似乎是多余的。
I have a postgres:9.5.6-alpine container, and another container, named web, which has to be linked to it.I want to run a script named create_db.sh
in postgres container after it has been started and docker-entrypoint.sh has been executed, in order to create a db and a user and restore a backup.My docker-compose.yml
(postgres part):
postgres:
build: ./postgres
container_name: postgres
volumes:
- /shared_folder/postgresql:/var/lib/postgresql
ports:
- "5432:5432"
command: sh /home/create_db.sh
The content of create_db.sh
is:
#!/bin/sh
psql -d template1 -U postgres
psql --command "CREATE USER user WITH PASSWORD 'userpassword';"
psql --command "CREATE DATABASE userdb;"
psql --command "GRANT ALL PRIVILEGES ON DATABASE userdb to user;"
psql --command "\q;"
psql -U user -d userdb -f /var/lib/postgresql/backup.sql
exit
When i run docker-compose build
and then docker-compose up
i get this:
Attaching to postgres, web
postgres | psql: could not connect to server: No such file or directory
postgres | Is the server running locally and accepting
postgres | connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I've understood this is because when I launch create_db.sh
postgres server is not ready, but so how can i run it after the docker-entrypoint.sh
of the container?
You are overriding the original command
and you do not start postgres in this script which is why your database is not available.
You can put your database initialization into the container's entrypoint directory: /docker-entrypoint-initdb.d
. This executes all *.sh
and *.sql
files in this directory and does not touch the original command
.
All files in this directory are automatically executed in the alphabetical order on container creation. Therefore, create a volume to add your scripts / sql files to the entrypoint and let the container execute them. This is described in the official postgres documentation, section "How to extend this image".
Your compose file then changes to something like this:
postgres:
build: ./postgres
volumes:
- /shared_folder/postgresql:/var/lib/postgresql
- ./db-init-scripts:/docker-entrypoint-initdb.d
ports:
- "5432:5432"
whereas a local directory, e.g. db-init-scripts
, contains your initialization scripts (rename it if you want). Copy create_db.sh
to this folder and it will be automatically executed when you create a new container.
Several database-images watch this entrypoint-directory, which is very convenient.
Your container_name: postgres
seems redundant.
这篇关于在docker-compose中的容器入口点之后运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!