links_container_together

有两种方式来连接容器中的服务
1、端口映射
2、docker的links系统

1、端口映射
通过-p标识运行容器
docker run -d -P training/webapp python app.py
docker run -d -p 5000:5000 training/webapp python app.py
docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py
docker run -d -p 127.0.0.1::5000 training/webapp python app.py
docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
查看容器端口对于docker host映射端口
docker port nostalgic_morse 5000

2、docker的links系统
links系统依赖容器的name,因此我们创建容器时,自定义容器的name。
docker run -d -P --name web training/webapp python app.py
命名创建的容器为web
容器name必须是唯一的,如果向reuse容器name,需先删除原有容器

当创建link,就在source container和target container之间建立了一个管道
target container可以从source container中访问选定的数据

docker run -d --name db training/postgres
docker rm -f web
docker run -d -P --name web --link db:db training/webapp python app.py
上面指定在db作为source container,web作为target container建立link,link的别名为db,别名有很大的作用
--link <name or id>:alias

与端口映射相比,我们不需要暴露source container的port给docker host。
docker通过下面两种方式建立source container和target container之间的connect info
(1)Environment variables(环境变量)
(2)Updating the /etc/hosts file

(1)Environment variables:在target container中创建环境变量(名称与link的别名相关),变量的只为source container的值;
这些变量值包括
在source container运行ENV命令获取的变量值

    the ENV commands in the source container's Dockerfile
    the -e, --env and --env-file options on the docker run command when the source container is started
这对source端口的变量格式
<name>_PORT_<port>_<protocol>

The components in this prefix are:

    the alias <name> specified in the --link parameter (for example, webdb)
    the <port> number exposed
    a <protocol> which is either TCP or UDP
例如8080端口在使用
WEBDB_PORT_8080_TCP_ADDR=172.17.0.82.
WEBDB_PORT_8080_TCP_PORT=8080.
WEBDB_PORT_8080_TCP_PROTO=tcp.
可以创建一个容器web2去查阅相关信息
docker run --rm --name web2 --link db:db training/webapp env
    . . .
    DB_NAME=/web2/db
    DB_PORT=tcp://172.17.0.5:5432
    DB_PORT_5432_TCP=tcp://172.17.0.5:5432
    DB_PORT_5432_TCP_PROTO=tcp
    DB_PORT_5432_TCP_PORT=5432
    DB_PORT_5432_TCP_ADDR=172.17.0.5
注:与Updating the /etc/hosts file这种方式相比,环境变量的值不会自动更新,因此,若source container变换了IP等值(容器restart),会出现问题的。
(2)、在target container生成/etc/hosts文件
使用命令进入target contaner中查看文件
docker run -t -i --rm --link db:webdb training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7  aed84ee21bde
. . .
172.17.0.5  webdb 6e5cdeb2d300 db
第一行为local IP和container id
第二行为source container ip和link 别名 source container id以及source container name(如果是自定义的)
可以向source contianer ip进行ping
root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
root@aed84ee21bde:/opt/webapp# ping webdb
PING webdb (172.17.0.5): 48 data bytes
56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms
56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms
56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms

你可以应用/etc/hosts文件中的信息来配置在target contianer中的应用
如果source container restart的话,target container中的/etc/hosts会更新信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值