45
47

More than 5 years have passed since last update.

dockerのPostgreSQLコンテナのデータを永続化する

Posted at

色々なサイトの方法を読んだけど自分の言葉で説明するほうがしっくりくる。

前提としてはPostgreSQLはDockerHub公式リポジトリのpostgresを使っている。

PostgreSQLコンテナのデータを永続化するだけの最もシンプルなパターン

とにかく永続化したいときホスト側にデータを保存する。

DockerHub公式リポジトリのpostgresのPostgreSQLデータ領域は/var/lib/postgresql/dataなので、このデータ領域をホスト側から共有できればPostgreSQLのコンテナを消してもデータは残る。

docker runの-vでホスト側のパスを指定し、コンテナのデータ領域を指定する。

$ docker run -d --name postgres-container -v ~/docker/postgres:/var/lib/postgresql/data postgres:9.4.1

docker runのオプションは下記

  • d: デーモンとして起動
  • name: コンテナ名を指定
  • v: ホスト側のパスとコンテナ側のデータ領域

ホスト側のパス~/docker/postgresを調べるとPostgreSQLのデータが有る

$ ls -la ~/docker/postgres
合計 124
drwx------ 18  999 root  4096  7月 27 14:39 2015 .
drwxr-xr-x  8 root root  4096  7月 27 14:39 2015 ..
-rw-------  1  999  999     4  7月 27 14:39 2015 PG_VERSION
drwx------  7  999  999  4096  7月 27 14:42 2015 base
drwx------  2  999  999  4096  7月 27 14:42 2015 global
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_clog
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_dynshmem
-rw-------  1  999  999  4496  7月 27 14:39 2015 pg_hba.conf
-rw-------  1  999  999  1636  7月 27 14:39 2015 pg_ident.conf
drwx------  4  999  999  4096  7月 27 14:39 2015 pg_logical
drwx------  4  999  999  4096  7月 27 14:39 2015 pg_multixact
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_notify
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_replslot
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_serial
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_snapshots
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_stat
drwx------  2  999  999  4096  7月 27 14:43 2015 pg_stat_tmp
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_subtrans
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_tblspc
drwx------  2  999  999  4096  7月 27 14:39 2015 pg_twophase
drwx------  3  999  999  4096  7月 27 14:39 2015 pg_xlog
-rw-------  1  999  999    88  7月 27 14:39 2015 postgresql.auto.conf
-rw-------  1  999  999 21251  7月 27 14:39 2015 postgresql.conf
-rw-------  1  999  999    37  7月 27 14:39 2015 postmaster.opts
-rw-------  1  999  999    85  7月 27 14:39 2015 postmaster.pid

このデータはdocker rmしても消えない

45
47
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
47