本示例使用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