文档章节

【4】Linking Containers Together

Sean-x
 Sean-x
发布于 2016/02/23 19:29
字数 1293
阅读 14
收藏 0

//网络端口映射

//-P随机映射到主机的49153~49155端口

$ docker run -d -P training/webapp python app.py


//将镜像的5000端口映射到主机的5000端口

$ docker run -d -p 5000:5000 training/webapp python app.py


//将镜像的5000端口映射到IP为127.0.0.1的网卡的5000端口
$ docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py

//将镜像的5000端口映射到主机的随机端口
$ docker run -d -p 127.0.0.1::5000 training/webapp python app.py

//绑定UDP端口
$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

//查看指定容器的端口情况
$ docker port CONTAINERID[3~4] 5000
127.0.0.1:49155


//--name给镜像定义一个名称
$ docker run -d -P --name web training/webapp python app.py


//查看镜像属性
$ docker ps -l
CONTAINER ID  IMAGE                  COMMAND        CREATED       STATUS       PORTS                    NAMES
aed84ee21bde  training/webapp:latest python app.py  12 hours ago  Up 2 seconds 0.0.0.0:49154->5000/tcp  web


//用inspect查看镜像名称
$ docker inspect -f "{{ .Name }}" aed84ee21bde
/web


//Docker容器连接
//根据training/postgre创建一个名字为db的容器,使用--name为容器定义名称
$ docker run -d --name db training/postgres

//删除已经创建过的web容器
$ docker rm -f web

//创建一个新的web容器并与db容器连接,--link的使用方法--link <name or id>:alias
$ docker run -d -P --name web --link db:db training/webapp python app.py

//使用inspect查看web容器的连接情况
$ docker inspect -f "{{ .HostConfig.Links }}" web
[/db:/web/db]
//You can see that the web container is now linked to the db container web/db. 

So what does linking the containers actually do? You've learned that a link allows a source container to provide information about itself to a recipient container. In our example, the recipient, web, can access information about the source db. To do this, Docker creates a secure tunnel between the containers that doesn't need to expose any ports externally on the container; you'll note when we started the db container we did not use either the -P or -p flags. That's a big benefit of linking: we don't need to expose the source container, here the PostgreSQL database, to the network.

Docker exposes connectivity information for the source container to the recipient container in two ways:

  • Environment variables,

  • Updating the /etc/hosts file.






Environment Variables

When two containers are linked, Docker will set some environment variables in the target container to enable programmatic discovery of information related to the source container.

First, Docker will set an <alias>_NAME environment variable specifying the alias of each target container that was given in a --link parameter. So, for example, if a new container called web is being linked to a database container called db via --link db:webdb then in the web container would be WEBDB_NAME=/web/webdb.

Docker will then also define a set of environment variables for each port that is exposed by the source container. The pattern followed is:

  • <name>_PORT_<port>_<protocol> will contain a URL reference to the port. Where <name> is the alias name specified in the --link parameter (e.g. webdb), <port> is the port number being exposed, and <protocol> is either TCP or UDP. The format of the URL will be: <protocol>://<container_ip_address>:<port> (e.g. tcp://172.17.0.82:8080). This URL will then be split into the following 3 environment variables for convenience:

  • <name>_PORT_<port>_<protocol>_ADDR will contain just the IP address from the URL (e.g. WEBDB_PORT_8080_TCP_ADDR=172.17.0.82).

  • <name>_PORT_<port>_<protocol>_PORT will contain just the port number from the URL (e.g. WEBDB_PORT_8080_TCP_PORT=8080).

  • <name>_PORT_<port>_<protocol>_PROTO will contain just the protocol from the URL (e.g. WEBDB_PORT_8080_TCP_PROTO=tcp).

If there are multiple ports exposed then the above set of environment variables will be defined for each one.

Finally, there will be an environment variable called <alias>_PORT that will contain the URL of the first exposed port of the source container. For example, WEBDB_PORT=tcp://172.17.0.82:8080. In this case, 'first' is defined as the lowest numbered port that is exposed. If that port is used for both tcp and udp, then the tcp one will be specified.

Returning back to our database example, you can run the env command to list the specified container's environment variables.

    $ sudo 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
    . . .

Note: These Environment variables are only set for the first process in the container. Similarly, some daemons (such as sshd) will scrub them when spawning shells for connection.

Note: Unlike host entries in the /etc/hosts file, IP addresses stored in the environment variables are not automatically updated if the source container is restarted. We recommend using the host entries in /etc/hosts to resolve the IP address of linked containers.

You can see that Docker has created a series of environment variables with useful information about the source db container. Each variable is prefixed with DB_, which is populated from the alias you specified above. If the alias were db1, the variables would be prefixed with DB1_. You can use these environment variables to configure your applications to connect to the database on the db container. The connection will be secure and private; only the linked web container will be able to talk to the db container.

Updating the /etc/hosts file

In addition to the environment variables, Docker adds a host entry for the source container to the /etc/hosts file. Here's an entry for the web container:

$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7  aed84ee21bde
. . .
172.17.0.5  db

You can see two relevant host entries. The first is an entry for the web container that uses the Container ID as a host name. The second entry uses the link alias to reference the IP address of the db container. You can ping that host now via this host name.

root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
root@aed84ee21bde:/opt/webapp# ping db
PING db (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

Note: In the example, you'll note you had to install ping because it was not included in the container initially.

Here, you used the ping command to ping the db container using its host entry, which resolves to 172.17.0.5. You can use this host entry to configure an application to make use of your db container.

Note: You can link multiple recipient containers to a single source. For example, you could have multiple (differently named) web containers attached to your db container.

If you restart the source container, the linked containers /etc/hosts files will be automatically updated with the source container's new IP address, allowing linked communication to continue.

$ sudo docker restart db
db
$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7  aed84ee21bde
. . .
172.17.0.9  db



© 著作权归作者所有

Sean-x
粉丝 4
博文 57
码字总数 83950
作品 0
武汉
程序员
私信 提问
Docker 1.8.0 RC2 发布,Linux 容器引擎

Docker 1.8.0 RC2 发布,这是第二个 RC 版本。 下载: RPMs/DEBs: curl -sSL https://test.docker.com | sh Linux 64bit binary: https://test.docker.com/builds/Linux/x86_64/docker-1.8.0......

oschina
2015/08/04
1K
0
Docker 1.8.0 正式版发布,Linux 容器引擎

Docker 1.8.0 正式发布,更新内容如下: Distribution Trusted pull, push and build, disabled by default Make tar layers deterministic between registries Don't allow deleting the i......

oschina
2015/08/12
3.3K
14
Install log

Libraries have been installed in:/opt/bin/gcc62/lib/../lib64 If you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libt......

MtrS
2016/09/20
4
0
kubeadm 安装 Kubernetes 1.6.2 过程中的一些坑

因为一些莫可名状的原因,国内网络使用 Kubeadm 颇有难度,这里大概说一下过程中的一些坑。 主体流程遵循官网指南:https://kubernetes.io/docs/getting-started-guides/kubeadm/ 1/4 准备工...

店家小二
2018/12/14
0
0
Linux LD

MtrS
2016/10/18
56
0

没有更多内容

加载失败,请刷新页面

加载更多

分页查询

一、配置 /*** @author beth* @data 2019-10-14 20:01*/@Configurationpublic class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor(){ ......

一个yuanbeth
17分钟前
2
0
在LINQPad中使用Ignite.NET

LINQPad是进行.NET开发的一款优秀工具,非常有利于Ignite.NET API的快速入门。 入门 下载LINQPad:linqpad.net/Download.aspx,注意要选择64位操作系统的AnyCPU版本; 安装Ignite.NET的NuGet...

李玉珏
30分钟前
2
0
JS其他类型值转化为Boolean类型规则

本文转载于:专业的前端网站➤JS其他类型值转化为Boolean类型规则 由于最近在笔试的时候,发现好多关于其他类型转化为Boolean类型的题目,因此总结一下! 一、String类型转化为Boolean 1.转化...

前端老手
41分钟前
4
0
EurekaClient自动装配及启动流程解析

在上篇文章中,我们简单介绍了EurekaServer自动装配及启动流程解析,本篇文章则继续研究EurekaClient的相关代码 老规矩,先看spring.factories文件,其中引入了一个配置类EurekaDiscoveryClie...

Java学习录
47分钟前
8
0
析构函数是否必须为虚函数?为何?

p517 在C++中,基类指针可以指向一个派生类的对象。如果基类的析构函数不是虚函数,当需要delete这个指向派生类的基类指针时,就只会调用基类的析构函数,而派生类的析构函数无法被调用。容易...

天王盖地虎626
48分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部