在 docker-compose up 后加载 Postgres 转储 [英] Load Postgres dump after docker-compose up
问题描述
我有一个 dump.sql 文件,我想用 docker-compose 加载它.
I have a dump.sql file that I would like to load with docker-compose.
docker-compose.yml:
docker-compose.yml:
services:
postgres:
environment:
POSTGRES_DB: my_db_name
POSTGRES_USER: my_name
POSTGRES_PASSWORD: my_password
build:
context: .
dockerfile: ./devops/db/Dockerfile.db
我的 Dockerfile.db 目前非常简单:
My Dockerfile.db is really simple at the moment:
FROM postgres
MAINTAINER me <me@me.me>
COPY ./devops/db ./devops/db
WORKDIR ./devops/db
我想运行一个像 psql my_db_name < 这样的命令.dump.sql
在某个时候.如果我从 Dockerfile.db 运行这样的脚本,问题是脚本在构建之后但在 docker-compose up
之前运行,并且数据库尚未运行.
I would like to run a command like psql my_db_name < dump.sql
at some point. If I run a script like this from the Dockerfile.db, the issue is that the script is run after build but before docker-compose up
, and the database is not running yet.
知道怎么做吗?
推荐答案
阅读 https://hub.docker.com/_/postgres/,'Extend this image' 部分解释了/docker-entrypoint-initdb.d 中的任何 .sql 将在构建后执行.
Reading https://hub.docker.com/_/postgres/, the section 'Extend this image' explains that any .sql in /docker-entrypoint-initdb.d will be executed after build.
我只需要将我的 Dockerfile.db 更改为:
I just needed to change my Dockerfile.db to:
FROM postgres
ADD ./devops/db/dummy_dump.sql /docker-entrypoint-initdb.d
它有效!
这篇关于在 docker-compose up 后加载 Postgres 转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!