本示例使用Docker官方提供的镜像进行实验,基本步骤如下:
1.创建本地数据目录
ChrisdeMacBook-Pro:~ chris$ mkdir pgdata
ChrisdeMacBook-Pro:~ chris$ cd pgdata
ChrisdeMacBook-Pro:pgdata chris$ pwd
/Users/chris/pgdata
2.运行镜像
首先去官方hub拉取pg,然后运行,其中我增加了-v参数映射本地目录到pg数据目录:
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容器是否运行
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.查看本地目录下是否有数据
由于run的时候,执行了initdb操作,所以本地目录下应该会有对应文件:
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目录下也有对应的数据库目录:
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.创建数据库、表、插入测试数据
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
此时:本地文件夹下,多了另外一个数据库目录:
ChrisdeMacBook-Pro:base chris$ ls
1 12993 12994 16384
7.关闭容器并重新启动
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.连接到重启过的容器,进行查询,查到刚才插入的数据
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