Geo*_*los 4 postgresql docker dockerfile docker-compose
这个问题与此相关:Docker - How can run the psql command in the postgres container?
但是上面提供的解决方案仅在您手动执行操作时才有效,而这里人们希望使用 docker-compose 自动执行操作。
所以这个 docker 容器可以工作:
FROM postgres
COPY init.sql /docker-entrypoint-initdb.d/
Run Code Online (Sandbox Code Playgroud)
但是这个 docker 容器失败了:
FROM postgres
RUN psql -U postgres -c "CREATE USER user WITH PASSWORD 'pass';"
Run Code Online (Sandbox Code Playgroud)
因为当执行 RUN 时,postgresql 服务器还没有!
有没有办法克服这个限制?
当您将带有脚本的文件夹挂载到 docker 容器时,您可以在 posgres 中运行一些脚本,即/docker-entrypoint-initdb.d/挂载文件夹 sql 的 docker-compose.yml。postgres 图像很棒,每次 postgres 启动时都会运行脚本......
version: '3.6'
services:
my_db:
image: postgres:alpine
volumes: ["/somepath/sql/:/docker-entrypoint-initdb.d/"]
Run Code Online (Sandbox Code Playgroud)
文件结构/somepath/
- docker-compose.yml
- sql
- new_extension.sql
Run Code Online (Sandbox Code Playgroud)
和cat sql/new_extension.sql
/* create extension for cureent DB */
CREATE EXTENSION IF NOT EXISTS citext;
/* create extension over the template1 so all created databases after that also have the extension */
\c template1
CREATE EXTENSION IF NOT EXISTS citext;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4370 次 |
| 最近记录: |