07-Docker數(shù)據(jù)持久化示例

本示例使用Docker官方提供的鏡像進(jìn)行實(shí)驗(yàn)东羹,基本步驟如下:

1.創(chuàng)建本地?cái)?shù)據(jù)目錄

ChrisdeMacBook-Pro:~ chris$ mkdir pgdata

ChrisdeMacBook-Pro:~ chris$ cd pgdata

ChrisdeMacBook-Pro:pgdata chris$ pwd

/Users/chris/pgdata

2.運(yùn)行鏡像

首先去官方hub拉取pg看幼,然后運(yùn)行翔试,其中我增加了-v參數(shù)映射本地目錄到pg數(shù)據(jù)目錄:

ChrisdeMacBook-Pro:pgdata chris$ docker run -v/Users/chris/pgdata:/var/lib/postgresql/data --name some-postgres -ePOSTGRES_PASSWORD=mysecretpassword -d postgres

Unable to find image 'postgres:latest' locally

latest: Pulling from library/postgres

f2aa67a397c4: Already exists

8218dd41bf94: Pull complete

e9b7fa2e6bd8: Pull complete

7288a45ee17f: Pull complete

0d0f8a67376c: Pull complete

670a6cc35896: Pull complete

813e44621756: Pull complete

220a4cd75207: Pull complete

2f20067ce75a: Pull complete

4adafe999f39: Pull complete

a4e661925544: Pull complete

db5c3dc2b534: Pull complete

9f501bef0c46: Pull complete

Digest:sha256:1ec65278ff8f8bb4a3e1d3ba09f6d09d5666e69765463179ae376e91e65b17a9

Status: Downloaded newer image for postgres:latest

3.查看PG容器是否運(yùn)行

ChrisdeMacBook-Pro:pgdata chris$ docker ps

CONTAINERID????????IMAGE???????????????COMMAND??????????????????CREATED??????????????????STATUS??????????????PORTS???????????????NAMES

2c7da34251db????????postgres????????????"docker-entrypoint.s…"???Lessthan a second ago???Up 3seconds????????5432/tcp????????????some-postgres

4.查看本地目錄下是否有數(shù)據(jù)

由于run的時(shí)候磕诊,執(zhí)行了initdb操作谱俭,所以本地目錄下應(yīng)該會(huì)有對(duì)應(yīng)文件:

ChrisdeMacBook-Pro:pgdata chris$ ls

PG_VERSION????????pg_dynshmem????????pg_multixact????????pg_snapshots????????pg_tblspc????????postgresql.auto.conf

base????????????pg_hba.conf????????pg_notify????????pg_stat????????????pg_twophase????????postgresql.conf

global????????????pg_ident.conf????????pg_replslot????????pg_stat_tmp????????pg_wal????????????postmaster.opts

pg_commit_ts????????pg_logical????????pg_serial????????pg_subtrans????????pg_xact????????????postmaster.pid

base目錄下也有對(duì)應(yīng)的數(shù)據(jù)庫(kù)目錄:

ChrisdeMacBook-Pro:pgdata chris$ cd base

ChrisdeMacBook-Pro:base chris$ ls

1????12993????12994

5.連接到PG容器

ChrisdeMacBook-Pro:~ chris$ docker run -it --rm --linksome-postgres:postgres postgres psql -h postgres -U postgres

Password for user postgres:

psql (10.3 (Debian 10.3-1.pgdg90+1))

Type "help" for help.

6.創(chuàng)建數(shù)據(jù)庫(kù)亏拉、表、插入測(cè)試數(shù)據(jù)

postgres=# create database chris;

CREATE DATABASE

postgres=# \c chris

You are now connected to database "chris" as user"postgres".

chris=# create table test_per(id int);

CREATE TABLE

chris=# insert into test_per values(1);

INSERT 0 1

chris=# insert into test_per values(2);

INSERT 0 1

chris=# insert into test_per values(3);

INSERT 0 1

chris=# insert into test_per values(4);

INSERT 0 1

chris=# \q

此時(shí):本地文件夾下走孽,多了另外一個(gè)數(shù)據(jù)庫(kù)目錄:

ChrisdeMacBook-Pro:base chris$ ls

1????12993????12994????16384

7.關(guān)閉容器并重新啟動(dòng)

ChrisdeMacBook-Pro:~ chris$ docker ps

CONTAINERID????????IMAGE???????????????COMMAND??????????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES

981191a451dc????????86517cb8435e????????"/bin/sh-c 'set -ex…"???15 secondsago??????Up 37 seconds???????????????????????????wonderful_hopper

2c7da34251db????????postgres????????????"docker-entrypoint.s…"???2minutes ago???????Up 2 minutes????????5432/tcp????????????some-postgres

ChrisdeMacBook-Pro:~ chris$ docker stop 2c7da34251db

2c7da34251db

ChrisdeMacBook-Pro:~ chris$ docker ps

CONTAINERID????????IMAGE???????????????COMMAND??????????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES

981191a451dc????????86517cb8435e????????"/bin/sh-c 'set -ex…"???28 secondsago??????Up 50 seconds???????????????????????????wonderful_hopper

ChrisdeMacBook-Pro:~ chris$ docker ps -a

CONTAINERID????????IMAGE???????????????COMMAND??????????????????CREATED?????????????STATUS?????????????????????PORTS???????????????NAMES

981191a451dc????????86517cb8435e????????"/bin/sh-c 'set -ex…"???35 secondsago??????Up 57seconds??????????????????????????????????wonderful_hopper

2c7da34251db????????postgres????????????"docker-entrypoint.s…"???2minutes ago???????Exited (0) 9 secondsago???????????????????????some-postgres

ChrisdeMacBook-Pro:~ chris$ docker start 2c7da34251db

2c7da34251db

ChrisdeMacBook-Pro:~ chris$ docker ps

CONTAINERID????????IMAGE???????????????COMMAND??????????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES

981191a451dc????????86517cb8435e????????"/bin/sh-c 'set -ex…"???45 secondsago??????Up About aminute???????????????????????wonderful_hopper

2c7da34251db????????postgres????????????"docker-entrypoint.s…"???2minutes ago???????Up 2 seconds????????5432/tcp????????????some-postgres

8.連接到重啟過的容器,進(jìn)行查詢琳状,查到剛才插入的數(shù)據(jù)

ChrisdeMacBook-Pro:~ chris$ docker run -it --rm --linksome-postgres:postgres postgres psql -h postgres -U postgres

Password for user postgres:

psql (10.3 (Debian 10.3-1.pgdg90+1))

Type "help" for help.


postgres=# \c chris

You are now connected to database "chris" as user"postgres".

chris=# \d

List ofrelations

Schema |???Name???|Type??|??Owner

--------+----------+-------+----------

public | test_per | table | postgres

(1 row)


chris=# select * from test_per;

id

----

1

2

3

4

(4 rows)


chris=# \q

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末磕瓷,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子算撮,更是在濱河造成了極大的恐慌生宛,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肮柜,死亡現(xiàn)場(chǎng)離奇詭異陷舅,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)审洞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門莱睁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人芒澜,你說我怎么就攤上這事仰剿。” “怎么了痴晦?”我有些...
    開封第一講書人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵南吮,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我誊酌,道長(zhǎng)部凑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任碧浊,我火速辦了婚禮涂邀,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘箱锐。我一直安慰自己比勉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著浩聋,像睡著了一般观蜗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赡勘,一...
    開封第一講書人閱讀 49,166評(píng)論 1 284
  • 那天嫂便,我揣著相機(jī)與錄音,去河邊找鬼闸与。 笑死毙替,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的践樱。 我是一名探鬼主播厂画,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼拷邢!你這毒婦竟也來了袱院?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬榮一對(duì)情侶失蹤瞭稼,失蹤者是張志新(化名)和其女友劉穎忽洛,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體环肘,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡欲虚,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了悔雹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片复哆。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖腌零,靈堂內(nèi)的尸體忽然破棺而出梯找,到底是詐尸還是另有隱情,我是刑警寧澤益涧,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布锈锤,位于F島的核電站,受9級(jí)特大地震影響闲询,放射性物質(zhì)發(fā)生泄漏久免。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一嘹裂、第九天 我趴在偏房一處隱蔽的房頂上張望妄壶。 院中可真熱鬧摔握,春花似錦寄狼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)伊磺。三九已至,卻和暖如春删咱,著一層夾襖步出監(jiān)牢的瞬間屑埋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工痰滋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留摘能,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓敲街,卻偏偏與公主長(zhǎng)得像团搞,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子多艇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容