在docker-compose之后加载Postgres转储 [英] Load Postgres dump after docker-compose up

查看:152
本文介绍了在docker-compose之后加载Postgres转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

有什么想法吗?

推荐答案

阅读< a href = https://hub.docker.com/_/postgres/ rel = noreferrer> https://hub.docker.com/_/postgres/ ,扩展此图片部分'解释说/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之后加载Postgres转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆