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

时间:2020-02-09 10:15:25

标签: postgresql docker docker-compose dockerfile psql

在创建映像时,是否可以创建自定义数据库(docker image build --tag mydb:latest)? 如果我将sql文件放入“ /docker-entrypoint-initdb.d/”数据库,则每次从该映像启动容器时都会执行该数据库。我不想每次启动都运行数据库脚本,因为它很大数据库。

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

这是我的Dockerfile

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上面,我可以看到我在创建映像时执行了数据库。 但是,当我用该映像启动容器时,看不到seed.sql创建的数据库。 那么如何用自己的数据库创建自己的“ postgres docker image”?

1 个答案:

答案 0 :(得分:0)

有时候,我也对此想法感到惊讶。我认为它与您正在使用的postgres基本图像有关,但我不记得详细信息。但是,我确实仍然有来自试用版的dockerfile,我知道我实现了您想要的。

首先,我基于一些示例创建了自己的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文件。

相关问题