在Docker中启动并填充Postgres容器

我有一个包含我的Postgres数据库的Docker容器。 它使用的官方Postgres图像有一个CMD条目,在主线程上启动服务器。

我想通过在开始监听查询之前运行RUN psql –U postgres postgres < /dump/dump.sql来填充数据库。

我不明白这是如何与Docker可能的。 如果我在CMD之后放置RUN命令,它当然永远不会到达,因为Docker已经完成了对Dockerfile的读取。 但是,如果我把它放在CMD之前,它将在psql甚至存在的过程之前运行。

我如何预先填充Docker中的Postgres数据库?

经过很多的战斗,我find了一个解决scheme;-)

对我来说非常有用的评论发表在这里: https ://registry.hub.docker.com/_/postgres/从“justfalter”

无论如何,我已经这样做了:

 # Dockerfile FROM postgres:9.4 RUN mkdir -p /tmp/psql_data/ COPY db/structure.sql /tmp/psql_data/ COPY scripts/init_docker_postgres.sh /docker-entrypoint-initdb.d/ 

db/structure.sql是一个sql转储,用于初始化第一个表空间。

然后, init_docker_postgres.sh

 #!/bin/bash # this script is runned when the docker container is built # it imports the base database structure and create the database for the tests DATABASE_NAME="db_name" DB_DUMP_LOCATION="/tmp/psql_data/structure.sql" echo "*** CREATING DATABASE ***" # create default database gosu postgres postgres --single <<EOSQL CREATE DATABASE "$DATABASE_NAME"; GRANT ALL PRIVILEGES ON DATABASE "$DATABASE_NAME" TO postgres; EOSQL # clean sql_dump - because I want to have a one-line command # remove indentation sed "s/^[ \t]*//" -i "$DB_DUMP_LOCATION" # remove comments sed '/^--/ d' -i "$DB_DUMP_LOCATION" # remove new lines sed ':a;N;$!ba;s/\n/ /g' -i "$DB_DUMP_LOCATION" # remove other spaces sed 's/ */ /g' -i "$DB_DUMP_LOCATION" # remove firsts line spaces sed 's/^ *//' -i "$DB_DUMP_LOCATION" # append new line at the end (suggested by @Nicola Ferraro) sed -e '$a\' -i "$DB_DUMP_LOCATION" # import sql_dump gosu postgres postgres --single "$DATABASE_NAME" < "$DB_DUMP_LOCATION"; echo "*** DATABASE CREATED! ***" 

最后:

 # no postgres is running [myserver]# psql -h 127.0.0.1 -U postgres psql: could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? [myserver]# docker build -t custom_psql . [myserver]# docker run -d --name custom_psql_running -p 5432:5432 custom_psql [myserver]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce4212697372 custom_psql:latest "/docker-entrypoint. 9 minutes ago Up 9 minutes 0.0.0.0:5432->5432/tcp custom_psql_running [myserver]# psql -h 127.0.0.1 -U postgres psql (9.2.10, server 9.4.1) WARNING: psql version 9.2, server version 9.4. Some psql features might not work. Type "help" for help. postgres=# 

希望能帮助到你!

或者,您可以将卷挂载到包含所有DDL脚本的/docker-entrypoint-initdb.d/。 您可以放入* .sh,* .sql或* .sql.gz文件,并在启动时执行这些文件。

例如(假设你的脚本在/ tmp / my_scripts中)

 docker run -v /tmp/my_scripts:/docker-entrypoint-initdb.d postgres 

还有另外一个可以使用Flocker的选项:

Flocker是一个容器数据卷pipe理器,可以让PostgreSQL等数据库在生产环境中轻松运行。 在生产环境中运行数据库时,必须考虑从主机故障中恢复等。 Flocker提供的工具可以像在生产环境中一样pipe理整个机器集群中的数据卷。 例如,由于Postgres容器是在主机之间调度以响应服务器故障,Flocker可以自动在主机之间移动其关联的数据卷。 这意味着当你的Postgres容器在一个新的主机上启动时,它有它的数据。 此操作可以使用Flocker API或CLI手动完成,也可以由Flocker集成的容器编排工具自动完成,例如Docker Swarm,Kubernetes或Mesos。

我可以通过在/etc/init.d/postgresql预处理docker文件中的run命令来加载数据。 我的docker文件有以下线正在为我工​​作:

 RUN /etc/init.d/postgresql start && /usr/bin/psql -a < /tmp/dump.sql