在Docker中启动并填充Postgres容器 [英] Starting and populating a Postgres container in Docker

查看:1552
本文介绍了在Docker中启动并填充Postgres容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想通过运行运行psql -U postgres postgres来填充数据库。 /dump/dump.sql 开始侦听查询之前。



我不明白Docker如何做到这一点。如果我在CMD之后放置 RUN 命令,它当然不会被访问,因为Docker已经读完了Dockerfile。但是如果我把它放在 CMD 之前,它会在psql之前运行,甚至作为一个进程存在。



如何在Docker中预填充Postgres数据库?

解决方案

经过大量的战斗,我找到了一个解决方案; - )



对我来说非常有用的发布在这里的注释:https://registry.hub.docker.com/_/postgres/



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

 #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 ,用于初始化第一个表空间。



然后, init_docker_postgres.sh



<$ c $ p> #!/ bin / bash

#这个脚本在构建docker容器时运行
#数据库结构并为测试创建数据库

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;
授予数据库$ DATABASE_NAME上的所有权限TO postgres;
EOSQL

#clean sql_dump - 因为我想要一个单行命令

#删除缩进
seds / ^ [\ t] * //-i$ DB_DUMP_LOCATION

#remove comment
sed'/ ^ - / d'-i$ DB_DUMP_LOCATION

#remove new lines
sed':a; N; $!ba; s / \\\
/ / g'-i$ DB_DUMP_LOCATION

#删除其他空格
sed's / * / / g'-i$ DB_DUMP_LOCATION

#remove firsts行空格
sed's / ^ * //'-i$ DB_DUMP_LOCATION

#在末尾追加一行(由@Nicola Ferraro建议)
sed -e'$ a\'-i$ DB_DUMP_LOCATION

#import sql_dump
gosu postgres postgres --single$ DATABASE_NAME< $ DB_DUMP_LOCATION;


echo*** DATABASE CREATED!***

最后:

 #没有postgres正在运行
[myserver]#psql -h 127.0.0.1 - U postgres
psql:无法连接到服务器:拒绝连接
服务器是否在主机127.0.0.1上运行,并且在端口5432上接受
TCP / IP连接?

[myserver]#docker build -t custom_psql。
[myserver]#docker run -d --name custom_psql_running -p 5432:5432 custom_psql

[myserver]#docker ps -a
容器ID映像命令创建状态端口名
ce4212697372 custom_psql:latest/ docker-entrypoint。9分钟前上传9分钟0.0.0.0:5432->5432/tcp custom_psql_running

[myserver]#psql -h 127.0.0.1 -U postgres
psql(9.2.10,服务器9.4.1)
警告:psql版本9.2,服务器版本9.4。
某些psql功能可能无法正常工作
键入help 帮助。

postgres =#

希望它有帮助! p>

I have a Docker container that contains my Postgres database. It's using the official Postgres image which has a CMD entry that starts the server on the main thread.

I want to populate the database by running RUN psql –U postgres postgres < /dump/dump.sql before it starts listening to queries.

I don't understand how this is possible with Docker. If I place the RUN command after CMD, it will of course never be reached because Docker has finished reading the Dockerfile. But if I place it before the CMD, it will run before psql even exists as a process.

How can I prepopulate a Postgres database in Docker?

解决方案

After a lot of fighting, I have found a solution ;-)

For me was very useful a comment posted here: https://registry.hub.docker.com/_/postgres/ from "justfalter"

Anyway, I have done in this way:

# 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 is a sql dump, useful to initialize the first tablespace.

Then, the 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! ***"

So finally:

# 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=# 

Hope it helps!

这篇关于在Docker中启动并填充Postgres容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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