如何在构建Docker映像时还原Postgresdump? [英] How to restore a Postgresdump while building a Docker image?

查看:67
本文介绍了如何在构建Docker映像时还原Postgresdump?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免在我的工作流程中使用共享的开发数据库;为了简化操作,我想在磁盘上有需要的架构的Docker映像定义。但是,我坚持制作一个Dockerfile,该文件将创建具有已恢复的转储的Postgres映像。我的问题是,在构建Docker映像时,Postgres服务器未运行。

I'm trying to avoid touching a shared dev database in my workflow; to make this easier, I want to have Docker image definitions on my disk for the schemas I need. I'm stuck however at making a Dockerfile that will create a Postgres image with the dump already restored. My problem is that while the Docker image is being built, the Postgres server isn't running.

当在shell中的容器中乱七八糟时,我尝试启动该容器手动操作,但我不确定执行此操作的正确方法。 /docker-entrypoint.sh 似乎没有任何作用,而且我不知道如何正确启动服务器。

While messing around in the container in a shell, I tried starting the container manually, but I'm not sure what the proper way to do so. /docker-entrypoint.sh doesn't seem to do anything, and I can't figure out how to "correctly" start the server.

所以我需要做的是:


  • 以 FROM postgres开头

  • 将转储文件复制到容器中

  • 启动PG服务器

  • 运行 psql 恢复转储文件

  • 杀死PG服务器

  • start with "FROM postgres"
  • copy the dump file into the container
  • start the PG server
  • run psql to restore the dump file
  • kill the PG server

(我不知道的步骤是斜体,其余的很容易。)

(Steps I don't know are in italics, the rest is easy.)

我想避免的是:


  • 手动将还原运行到现有容器中,整个想法是能够在不同数据库之间切换而无需接触应用程序配置。

  • 保存还原的映像,我希望能够使用其他转储轻松地为数据库重建映像。 (而且,对于具有不可重复的映像构建的Docker来说,感觉并不十分。)

推荐答案

此可以通过提供example.pg转储文件来完成以下Dockerfile:

This can be done with the following Dockerfile by providing an example.pg dump file:

FROM postgres:9.6.16-alpine

LABEL maintainer="lu@cobrainer.com"
LABEL org="Cobrainer GmbH"

ARG PG_POSTGRES_PWD=postgres
ARG DBUSER=someuser
ARG DBUSER_PWD=P@ssw0rd
ARG DBNAME=sampledb
ARG DB_DUMP_FILE=example.pg

ENV POSTGRES_DB launchpad
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD ${PG_POSTGRES_PWD}
ENV PGDATA /pgdata

COPY wait-for-pg-isready.sh /tmp/wait-for-pg-isready.sh
COPY ${DB_DUMP_FILE} /tmp/pgdump.pg

RUN set -e && \
    nohup bash -c "docker-entrypoint.sh postgres &" && \
    /tmp/wait-for-pg-isready.sh && \
    psql -U postgres -c "CREATE USER ${DBUSER} WITH SUPERUSER CREATEDB CREATEROLE ENCRYPTED PASSWORD '${DBUSER_PWD}';" && \
    psql -U ${DBUSER} -d ${POSTGRES_DB} -c "CREATE DATABASE ${DBNAME} TEMPLATE template0;" && \
    pg_restore -v --no-owner --role=${DBUSER} --exit-on-error -U ${DBUSER} -d ${DBNAME} /tmp/pgdump.pg && \
    psql -U postgres -c "ALTER USER ${DBUSER} WITH NOSUPERUSER;" && \
    rm -rf /tmp/pgdump.pg

HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
  CMD pg_isready -U postgres -d launchpad

其中等待-pg-isready.sh 是:

#!/bin/bash
set -e

get_non_lo_ip() {
  local _ip _non_lo_ip _line _nl=$'\n'
  while IFS=$': \t' read -a _line ;do
    [ -z "${_line%inet}" ] &&
        _ip=${_line[${#_line[1]}>4?1:2]} &&
        [ "${_ip#127.0.0.1}" ] && _non_lo_ip=$_ip
    done< <(LANG=C /sbin/ifconfig)
  printf ${1+-v} $1 "%s${_nl:0:$[${#1}>0?0:1]}" $_non_lo_ip
}

get_non_lo_ip NON_LO_IP
until pg_isready -h $NON_LO_IP -U "postgres" -d "launchpad"; do
  >&2 echo "Postgres is not ready - sleeping..."
  sleep 4
done

>&2 echo "Postgres is up - you can execute commands now"

对于两个不确定步骤:


启动PG服务器

nohup bash -c docker-entrypoint.sh postgres& 可以照顾它


杀死PG服务器

这不是必须的

上述脚本以及更详细的自述文件可在 https://github.com/cobrainer/pg-docker-with-restored-db

The above scripts together with a more detailed README are available at https://github.com/cobrainer/pg-docker-with-restored-db

这篇关于如何在构建Docker映像时还原Postgresdump?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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