Docker-容器互联访问之数据容器操作过程
要通过一个容器A来访问另一个容器B,其中容器B部署数据库,容器A部署应用程序。在Docker生态中我们称之为容器互联。
下面通过一步一步执行,来实现此需求。
-
获取postgres镜像
|
1
2
3
4
5
6
7
|
ubuntu@ip-10-23-28-180:~$
sudo
docker pull daocloud.io
/library/postgres
:latest
Pulling repository daocloud.io
/library/postgres
a7d662bede59: Download complete
2c49f83e0b13: Download complete
4a5e6db8c069: Download complete
...
Status: Downloaded newer image
for
daocloud.io
/library/postgres
:latest
|
注:a.为了加快下载速度使用daocloud.io提供的服务;b. ... 表示输出内容部分省略
这里使用了postgres的最新版本,建议在开发,测试,生产过程中最好指定明确的版本,这样就可以在任一时刻清楚此时使用的镜像(image)版本,比如:ubuntu:14.04
2. 查看本地镜像
|
1
2
3
|
ubuntu@ip-10-23-28-180:~$
sudo
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
daocloud.io
/library/postgres
latest a7d662bede59 6 days ago 265.3 MB
|
镜像库:daocloud.io/library/postgres是来自daocloud registry的,对应docker hub的官方postgres镜像库,tag是latest 。
由于没有其它tag的镜像了,下面使用镜像
daocloud.io/library/postgres
daocloud.io/postgres,
daocloud.io/postgres:latest
等同daocloud.io/library/postgres:latest镜像。
3.通过镜像daocloud.io/postgres创建一个容器查看环境变量
|
1
2
3
4
5
6
7
8
9
10
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run --name pg --
rm
daocloud.io
/library/postgres
env
HOSTNAME=a741a4625fe8
PG_MAJOR=9.4
PATH=
/usr/lib/postgresql/9
.4
/bin
:
/usr/local/sbin
:
/usr/local/bin
:
/usr/sbin
:
/usr/bin
:
/sbin
:
/bin
PWD=/
LANG=en_US.utf8
SHLVL=0
HOME=
/root
PG_VERSION=9.4.4-1.pgdg80+1
PGDATA=
/var/lib/postgresql/data
|
容器名称(--name): pg, 运行完后删除。
4.创建一个容器命名命名为pg,并后台运行
|
1
2
3
4
5
6
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run --name pg -d daocloud.io
/postgres
3e27983005a9872da5e60fa84aef5a00897585b55754baff26b756eb16ed7674
ubuntu@ip-10-23-28-180:~$
sudo
docker
ps
-a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e27983005a9 daocloud.io
/postgres
"
/docker-entrypoint
. 13 seconds ago Up 12 seconds 5432
/tcp
pg
ubuntu@ip-10-23-28-180:~$
|
5.创建一个容器,连接pg容器,运行env命令,查看环境变量信息
|
1
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run -it --link pg:pglink --
rm
daocloud.io
/library/postgres
:latest
env
|
输出:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
PGLINK_PORT_5432_TCP_ADDR=172.17.0.3
HOSTNAME=c4c66c74ee25
PGLINK_ENV_PGDATA=
/var/lib/postgresql/data
PGLINK_ENV_LANG=en_US.utf8
TERM=xterm
PG_MAJOR=9.4
PGLINK_ENV_PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_ENV_PG_MAJOR=9.4
PGLINK_PORT=tcp:
//172
.17.0.3:5432
PGLINK_PORT_5432_TCP_PROTO=tcp
PATH=
/usr/lib/postgresql/9
.4
/bin
:
/usr/local/sbin
:
/usr/local/bin
:
/usr/sbin
:
/usr/bin
:
/sbin
:
/bin
PGLINK_PORT_5432_TCP_PORT=5432
PWD=/
PGLINK_NAME=
/hungry_sammet/pglink
LANG=en_US.utf8
SHLVL=0
HOME=
/root
PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_PORT_5432_TCP=tcp:
//172
.17.0.3:5432
PGDATA=
/var/lib/postgresql/data
|
通过输出内容可以得到连接pg容器中的postgres数据库信息
注:参数 --link CONTAINER_NAME:alias CONTAINER_NAME是要连接的容器名(这个是创建容器时自动生成的或者是通过--name指定的),alias该连接的别名
6.创建一个容器,连接容器pg,执行psql连接postgres数据库,要用到5中输出的环境变量
|
1
2
3
4
5
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run -it --link pg:pglink --name pgclient daocloud.io
/library/postgres
:latest sh -c
'exec psql -h "$PGLINK_PORT_5432_TCP_ADDR" -p "$PGLINK_PORT_5432_TCP_PORT" -U postgres'
psql (9.4.4)
Type
"help"
for
help.
postgres=
# \q
|
注:5中创建postgres数据库容器没有指定密码和用户名,6中使用默认用户名postgres
7.查看容器
|
1
2
3
4
|
ubuntu@ip-10-23-28-180:~$
sudo
docker
ps
-a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10ff07bac1f3 daocloud.io
/library/postgres
:latest "
/docker-entrypoint
. 2 minutes ago Exited (0) About a minute ago pgclient
3e27983005a9 daocloud.io
/postgres
"
/docker-entrypoint
. 15 minutes ago Up 15 minutes 5432
/tcp
pg
|
现在有两个容器,pg容器还在运行中;pgclient容器处理退出状态,可以启动。
8. 下面重新操作4,5,6个步骤,为postgres容器指定用户名和密码,客户端容器要通过认证才能连接到数据库
|
1
2
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run --name pg2 -d -e POSTGRES_PASSWORD=123 -e POSTGRES_USER=admin daocloud.io
/postgres
1373bae75b802587bbbe4a71e37ec5d69a1a8d28e715c95574e154c10bddce5d
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run -it --link pg2:pglink --name pgclient2 daocloud.io
/library/postgres
:latest
env
PGLINK_PORT_5432_TCP_ADDR=172.17.0.9
HOSTNAME=9dc0b31dee7d
PGLINK_ENV_PGDATA=
/var/lib/postgresql/data
PGLINK_ENV_LANG=en_US.utf8
TERM=xterm
PG_MAJOR=9.4
PGLINK_ENV_PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_ENV_PG_MAJOR=9.4
PGLINK_PORT=tcp:
//172
.17.0.9:5432
PGLINK_PORT_5432_TCP_PROTO=tcp
PATH=
/usr/lib/postgresql/9
.4
/bin
:
/usr/local/sbin
:
/usr/local/bin
:
/usr/sbin
:
/usr/bin
:
/sbin
:
/bin
PGLINK_PORT_5432_TCP_PORT=5432
PWD=/
PGLINK_NAME=
/pgclient2/pglink
LANG=en_US.utf8
PGLINK_ENV_POSTGRES_USER=admin
SHLVL=0
HOME=
/root
PGLINK_ENV_POSTGRES_PASSWORD=123
PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_PORT_5432_TCP=tcp:
//172
.17.0.9:5432
PGDATA=
/var/lib/postgresql/data
|
注意:PGLINK_ENV_POSTGRES_USER, PGLINK_ENV_POSTGRES_PASSWORD
下面是正确的认证方式:
|
1
2
3
4
5
6
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run -it --link pg2:pglink --name pgclient2 daocloud.io
/library/postgres
:latest sh -c
'exec psql -h "$PGLINK_PORT_5432_TCP_ADDR" -p "$PGLINK_PORT_5432_TCP_PORT" -U "$PGLINK_ENV_POSTGRES_USER" '
Password
for
user admin:
psql (9.4.4)
Type
"help"
for
help.
admin=
#
|
下面是错误的认证方式:
|
1
2
3
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run -it --link pg2:pglink --name pgclient2 daocloud.io
/library/postgres
:latest sh -c
'exec psql -h "$PGLINK_PORT_5432_TCP_ADDR" -p "$PGLINK_PORT_5432_TCP_PORT" -U "$PGLINK_ENV_POSTGRES_USER" -P "$PGLINK_ENV_POSTGRES_PASSWORD"'
\pset: unknown option: 123
psql: could not
set
printing parameter
"123"
|
9.使用busybox进行创建容器,并连接容器pg
下载busybox镜像:
|
1
2
3
4
5
6
|
ubuntu@ip-10-23-28-180:~$
sudo
docker pull busybox
Pulling repository busybox
8c2e06607696: Download complete
cf2616975b4a: Download complete
6ce2e90b0bc7: Download complete
Status: Downloaded newer image
for
busybox:latest
|
创建容器,通过交互式方式ping pg
|
1
2
3
4
5
6
7
8
9
10
11
12
|
ubuntu@ip-10-23-28-180:~$
sudo
docker run -it --name bb --link pg:bblink busybox
/
# ping -c 5 pg
PING pg (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3:
seq
=0 ttl=64
time
=0.132 ms
64 bytes from 172.17.0.3:
seq
=1 ttl=64
time
=0.050 ms
64 bytes from 172.17.0.3:
seq
=2 ttl=64
time
=0.079 ms
64 bytes from 172.17.0.3:
seq
=3 ttl=64
time
=0.053 ms
64 bytes from 172.17.0.3:
seq
=4 ttl=64
time
=0.082 ms
--- pg
ping
statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min
/avg/max
= 0.050
/0
.079
/0
.132 ms
|
容器bb和容器pg可以通信,并且可以直接使用容器名来作为hostname。 多个容器可以同时连接到一个容器上。
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1690841,如需转载请自行联系原作者