如何使用Postgres Docker映像持久化数据? [英] How to persist data using postgres docker image?

查看:130
本文介绍了如何使用Postgres Docker映像持久化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据保留在/ home / me / redmine中,以便在容器停止时不会丢失任何数据。我使用的是官方的postgres docker hub镜像。

I'm trying to get my data to persist in /home/me/redmine so that when my container stops, no data loss. I'm using the official postgres docker hub image.

问题是,每次我启动postgres容器时,它都会立即退出。当我将数据附加到/ var / lib / postgresql时,此问题就开始了,否则,如果不添加数据,它就开始了,但是我的数据没有被保存。另外,我在主机上的路径是/var/lib/postgresql/9.4/data,但是图像页面显示使用/ var / lib / postgresql / data。

Problem is, every time I start the postgres container, it immediately exits. This problem started when I appended 'data' to /var/lib/postgresql, otherwise, without adding 'data', it starts, but my data isn't being saved. Also, the path I have on my host machine is /var/lib/postgresql/9.4/data, but the image page says to use /var/lib/postgresql/data.

这是我尝试从主机启动/装入卷的方式:

Here's how I'm trying to start/mount a volume from the host:

docker run --name postgres -d   \
    --env='DB_NAME=redmine_production' \
    --env='DB_USER=redmine' \
    --env='DB_PASS=secret' \
    --volume=/home/me/redmine/postgresql:/var/lib/postgresql/data \
    postgres

...,然后按如下所示链接redmine ...

... and then after I would link redmine as follows ...

docker run -d -p 3000:3000 --name redmine \
    -v /home/me/redmine/files:/usr/src/redmine/files \
    --link postgres:postgres redmine

在redmine容器中,/ home / me / redmine / files卷是一个单独的目录, m尝试保留Postgres数据库中文件之外的文件。

In the redmine container, the /home/me/redmine/files volume is a seperate directory where I'm trying to persist files aside from the ones in the Postgres database.

编辑:

当我进入CD时在我的主机/ var / lib / postgresql目录中,我在其中找到9.4 / data,然后看到所有者和组未在我的/ etc / passwd或/ etc / groups中列出,并且想知道是否没有内容

When I cd into my host machines /var/lib/postgresql directory, I find within it 9.4/data and then see that the owner and group are not listed in my /etc/passwd or /etc/groups and wonder if this doesn't have something to do with the problem.

/var/lib/postgresql/9.4/data # ls -la
total 88
drwx------ 18   70   70 4096 Aug 13  2015 .
drwxr-xr-x  3 root root 4096 Mar 28  2015 ..
drwx------  8   70   70 4096 Jul  2  2015 base
drwx------  2   70   70 4096 Jul  2  2015 global
drwx------  2   70   70 4096 Mar 28  2015 pg_clog
drwx------  2   70   70 4096 Mar 28  2015 pg_dynshmem
lrwxrwxrwx  1 root root   31 Mar 28  2015 pg_hba.conf -> /etc/postgresql-    9.4/pg_hba.conf
lrwxrwxrwx  1 root root   33 Mar 28  2015 pg_ident.conf -> /etc/postgresql-9.4/pg_ident.conf
drwx------  4   70   70 4096 Mar 28  2015 pg_logical
drwx------  4   70   70 4096 Mar 28  2015 pg_multixact
drwx------  2   70   70 4096 Jul  2  2015 pg_notify
drwx------  2   70   70 4096 Mar 28  2015 pg_replslot
drwx------  2   70   70 4096 Mar 28  2015 pg_serial
drwx------  2   70   70 4096 Mar 28  2015 pg_snapshots
drwx------  2   70   70 4096 Aug 13  2015 pg_stat
drwx------  2   70   70 4096 Aug 13  2015 pg_stat_tmp
drwx------  2   70   70 4096 Mar 28  2015 pg_subtrans
drwx------  2   70   70 4096 Mar 28  2015 pg_tblspc
drwx------  2   70   70 4096 Mar 28  2015 pg_twophase
-rw-------  1   70   70    4 Mar 28  2015 PG_VERSION
drwx------  3   70   70 4096 Mar 28  2015 pg_xlog
-rw-------  1   70   70   88 Mar 28  2015 postgresql.auto.conf
lrwxrwxrwx  1 root root   35 Mar 28  2015 postgresql.conf -> /etc/postgresql-9.4/postgresql.conf
-rw-------  1   70   70 1626 Aug 13  2015 postmaster.log
-rw-------  1   70   70  114 Jul  2  2015 postmaster.opts

思路?

推荐答案

首先,这些环境变量看起来可疑。查看官方Docker映像的文档,请注意,您需要 POSTGRES_DB POSTGRES_USER POSTGRES_PASSWORD ,而不是 DB_NAME DB_USER DB_PASS

First, those environment variables look suspicious. Take a look at the documentation for the official Docker image, and note that you need POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD, rather than DB_NAME, DB_USER, and DB_PASS.

除此之外,您似乎基本上处在正确的轨道上。这是一个完整的示例:

Other than that, you seem to mostly be on the right track. Here's a complete example:

首先,我启动一个Postgres容器。我正在将持久性存储放置在主目录之外的某个位置,因为您已经注意到文件将不归您所有,这可能会使您的主目录混乱(尽管不一定有问题):

First, I start a Postgres container. I'm locating the persistent storage somewhere outside of my home directory, because as you have already noted that files won't be owned by you, which can be confusing in your home directory (though not necessarily problematic):

docker run --rm --name postgres \
  -v /tmp/postgres:/var/lib/postgresql/data \
  -e POSTGRES_DB=larstest \
  -e POSTGRES_USER=lars \
  -e POSTGRES_PASSWORD=secret postgres

由于这是我第一次启动postgres指向该数据目录,因此我们将看到它初始化数据库:

Since this is the first time I've started postgres pointing at that data directory, we will see it initialize the database:

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

现在,从另一个窗口中,我可以连接到它。 ..

Now, from another window, I can connect to it...

$ psql -h 172.17.0.4 -U lars larstest
Password for user lars: 
psql (9.5.4, server 9.6.0)
WARNING: psql major version 9.5, server major version 9.6.
         Some psql features might not work.
Type "help" for help.

...并创建一些数据:

...and create some data:

larstest=# create table testtable (id integer);
CREATE TABLE
larstest=# insert into testtable values (1);
INSERT 0 1
larstest=# select * from testtable;
 id 
----
  1
(1 row)

现在,我退出容器:

^CLOG:  received fast shutdown request
LOG:  aborting any active transactions
FATAL:  terminating connection due to administrator command
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down

我们可以验证它不再运行:

We can verify that it's no longer running:

$ docker ps | grep postgres

但是,如果我们使用相同的命令行参数重新启动它,

But if we start it up again with the same command line arguments;

docker run --rm --name postgres \
  -v /tmp/postgres:/var/lib/postgresql/data \
  -e POSTGRES_DB=larstest \
  -e POSTGRES_USER=lars \
  -e POSTGRES_PASSWORD=secret postgres

我们看到它没有初始化数据库,因为它已经存在,直接跳转到:

We see that it doesn't initialize the database, since it already exists, and skip straight to:

LOG:  database system was shut down at 2016-10-21 03:13:50 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

至此,我们可以重新连接到数据库,并发现我们的数据仍然存在:

At this point, we can re-connect to the database and find that our data still exists:

$ psql -h 172.17.0.2 -U lars larstest
Password for user lars: 
psql (9.5.4, server 9.6.0)
WARNING: psql major version 9.5, server major version 9.6.
         Some psql features might not work.
Type "help" for help.

larstest=# select * from testtable;
 id 
----
  1
(1 row)

这几乎就是它的全部。

这篇关于如何使用Postgres Docker映像持久化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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