Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 是否可以在创建映像时而不是启动容器时初始化数据库?_Postgresql_Docker_Docker Compose_Dockerfile_Psql - Fatal编程技术网

Postgresql 是否可以在创建映像时而不是启动容器时初始化数据库?

Postgresql 是否可以在创建映像时而不是启动容器时初始化数据库?,postgresql,docker,docker-compose,dockerfile,psql,Postgresql,Docker,Docker Compose,Dockerfile,Psql,在创建镜像docker镜像构建标记mydb:latest时,是否可以创建自定义数据库? 如果我将sql文件放在/docker-entrypoint-initdb.d,则每次从该映像启动容器时,都会执行/database。我不希望每次启动时都运行数据库脚本,因为它是一个巨大的数据库 这是用于自动测试的。如果绑定卷,数据库将成为持久性。我也不想那样。因为我的测试用例会失败 这是我的文件 FROM postgres:10 COPY seed.sql /tmp/ COPY init-db

在创建镜像docker镜像构建标记mydb:latest时,是否可以创建自定义数据库? 如果我将sql文件放在/docker-entrypoint-initdb.d,则每次从该映像启动容器时,都会执行/database。我不希望每次启动时都运行数据库脚本,因为它是一个巨大的数据库

这是用于自动测试的。如果绑定卷,数据库将成为持久性。我也不想那样。因为我的测试用例会失败

这是我的文件

FROM postgres:10
COPY seed.sql         /tmp/
COPY init-db.sh       /tmp/
WORKDIR /tmp/
RUN ["chmod", "+x", "init-db.sh"]
RUN ./init-db.sh
这是我的init-db.sh

#!/bin/bash
set -e
RETRIES=5
cd /usr/lib/postgresql/10/bin/
gosu postgres initdb
su postgres -c './pg_ctl start -D "/var/lib/postgresql/data"'
until psql -U postgres -d postgres -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
  echo "Waiting for postgres server, ${RETRIES}-- remaining attempts..."
  RETRIES=$((RETRIES--))
  sleep 1
done

psql -v ON_ERROR_STOP=1 -U postgres <<-EOSQL
    \i /tmp/seed.sql;
EOSQL
在Dockerfile和init-db.sh上面的Usng我可以看到在创建映像时我的数据库被执行。 但是当我用这个映像启动容器时,我看不到seed.sql创建的数据库。
那么,我怎样才能用自己的数据库创建自己的postgres docker图像呢?

不久前,我也对这个想法感到厌倦。我想你正在使用的postgres基础图片需要做些什么,但我不记得细节了。然而,我仍然有我的到期文件,我知道我实现了你想要的

首先,我创建了自己的postgres基础图像,基于一些示例,请参见注释:

#
# example Dockerfile for https://docs.docker.com/engine/examples/postgresql_service/
#

FROM ubuntu:bionic

RUN apt-get update && apt-get install -y wget gnupg2
# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
#RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8

# Add PostgreSQL's repository. It contains the most recent stable release
#     of PostgreSQL, ``9.3``.
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 
#  There are some warnings (in red) that show up during the build. You can hide
#  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y postgresql-10 postgresql-client-10 postgresql-contrib-10

# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
# after each ``apt-get``

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-10`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# Note: here we use ``&&\`` to run commands one after the other - the ``\``
#       allows the RUN command to span multiple lines.
RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker &&\
    /etc/init.d/postgresql stop

# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/10/main/pg_hba.conf

# And add ``listen_addresses`` to ``/etc/postgresql/10/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/10/main/postgresql.conf

# Expose the PostgreSQL port
EXPOSE 5432

# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/10/bin/postgres", "-D", "/var/lib/postgresql/10/main", "-c", "config_file=/etc/postgresql/10/main/postgresql.conf"]
然后,对于每个测试,我将创建一个特定的图像,如下所示:

FROM pgbase

COPY init.sql /tmp/init.sql
RUN    /etc/init.d/postgresql start &&\
    psql -f /tmp/init.sql &&\
    /etc/init.d/postgresql stop
init.sql是由pg_dump生成的sql文件