Docker容器的网络链接

在docker的容器中默认是没有 ifconfig,ping 命令,需要手动添加

apt-get update
apt install net-tools       # ifconfig 
apt install iputils-ping     # ping

安装 brctl 命令

sudo apt install bridge-utils

通过 brctl 查看 容器 web的网络

hejing@learning:~$ sudo brctl show
bridge name     bridge id               STP enabled     interfaces
docker0         8000.0242ddc1a384       no              veth46297f1
hejing@learning:~$

ifconfig 可以查看到 docker0 网桥

hejing@learning:~$ docker attach web
root@e6bb5696e362:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:02
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2939 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1775 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4194086 (4.1 MB)  TX bytes:102463 (102.4 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

root@e6bb5696e362:/#

新加并设置网桥

hejing@learning:~$ sudo brctl addbr br0
hejing@learning:~$ sudo ifconfig br0 192.168.100.1 network 255.255.255.0
network: No address associated with name
ifconfig: `--help' gives usage information.
hejing@learning:~$ ifconfig
br0       Link encap:Ethernet  HWaddr 1a:83:d5:ba:65:d0
          inet addr:192.168.100.1  Bcast:192.168.100.255  Mask:255.255.255.0
          inet6 addr: fe80::1883:d5ff:feba:65d0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:5152 (5.1 KB)

docker0   Link encap:Ethernet  HWaddr 02:42:dd:c1:a3:84
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:ddff:fec1:a384/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:22190 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25633 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1365895 (1.3 MB)  TX bytes:37833202 (37.8 MB)

在docker 启动配置文件 /etc/default/docker中添加配置 -b=br0

hejing@learning:~$ cat /etc/default/docker
# Here in Debian, this file is sourced by:
#   - /etc/init.d/docker (sysvinit)
#   - /etc/init/docker (upstart)
#   - systemd's docker.service

# Use of this file for configuring your Docker daemon is discouraged.

# The recommended alternative is "/etc/docker/daemon.json", as described in:
#   https://docs.docker.com/v1.11/engine/reference/commandline/daemon/#daemon-configuration-file

# If that does not suit your needs, try a systemd drop-in file, as described in:
#   https://docs.docker.com/v1.11/engine/admin/systemd/#custom-docker-daemon-options

DOCKER_OPTS='--registry-mirror=http://8fadea85.m.daocloud.io --label name=docker_server1 -b=br0'
hejing@learning:~$

新建容器查看网桥

hejing@learning:~$ docker run -it --name nwt3 2010jing/web:20171224 /bin/bash
root@f89fa63c56b7:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:c0:a8:64:04
          inet addr:192.168.100.4  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::42:c0ff:fea8:6404/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1976 (1.9 KB)  TX bytes:516 (516.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

检验设置成功。


利用 Dockerfile创建镜像 ,情回顾上篇 Docker创建镜像和推送到hub.docker.com

Dockerfile

FROM 2010jing/web:20171224
RUN apt install -y iputils-ping
RUN apt install -y net-tools
RUN apt update
RUN apt install -y nginx
RUN apt install -y curl
EXPOSE 80
cmd /bin/bash

构建镜像 docker build

hejing@learning:/dockerfile/cct$ sudo docker build -t 2010jing/cct .

同一宿主机内容器互联

启动第一个容器 ,并且启动 nginx服务,查看 ip地址

hejing@learning:~$ docker run -it --name cct1 2010jing/cct
root@361c52e5f2a1:/# nginx
root@361c52e5f2a1:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:c0:a8:64:02
          inet addr:192.168.100.2  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::42:c0ff:fea8:6402/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:27 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3213 (3.2 KB)  TX bytes:796 (796.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

启动第二个容器,并且查看地址

hejing@learning:~$ docker run -it --name cct2 2010jing/cct
root@8eb87baab309:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:c0:a8:64:03
          inet addr:192.168.100.3  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::42:c0ff:fea8:6403/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1774 (1.7 KB)  TX bytes:516 (516.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

root@8eb87baab309:/#

测试从 cct2 访问 cct1

root@8eb87baab309:/# curl http://192.168.100.2
<!DOCTYPE html>
<html>
<head>
        <title></title>
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js
"></script>

<style type="text/css">

        .hello{
                color:red;
        }

</style>
</head>
<body>
        <h3> welcome to <b class="hello">uic</b></h3>


        <select name="a1" id="a1" >
                <option value="a"> A</option>
                <option value="b"> B</option>
                <option value="c"> C</option>
        </select>

        <select name="a2" id="a2" >
                <option value="a"> A</option>
                <option value="b"> B</option>
                <option value="c"> C</option>
        </select>

        <select name="a3" id="a3" >
                <option value="a"> A</option>
                <option value="b"> B</option>
                <option value="c"> C</option>
        </select>

</body>

<script type="text/javascript">
        $("#a1").change(function(){
                //code...

                var selectedVal=$("#a1 ").val();  //获取Select选择的索引值
                var temp = "#a2 option[value='"+selectedVal+"']";
                alert(temp);
                $(temp).remove();  //删除Select中Value='3'的Option

        });

</script>
</html>
root@8eb87baab309:/#

当容器cct1停止再启动,cct2尝试去链接cct1

hejing@learning:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8eb87baab309        2010jing/cct        "/bin/sh -c /bin/bash"   5 minutes ago       Up 5 minutes        80/tcp              cct2
361c52e5f2a1        2010jing/cct        "/bin/sh -c /bin/bash"   8 minutes ago       Up 8 minutes        80/tcp              cct1
hejing@learning:~$ docker stop cct1
cct1
hejing@learning:~$ docker start -i cct1
root@361c52e5f2a1:/# hejing@learning:~$
hejing@learning:~$ docker attach cct2
root@8eb87baab309:/# curl http://192.168.100.2
curl: (7) Failed to connect to 192.168.100.2 port 80: Connection refused

会发现,以链接方式访问,在容器重启时候失效。

docker 为了避免这种情况,提供另一种方式
使用 --link 参数可以让容器之间安全的进行交互。

格式: docker run --link=[CONTAINER_NAME]:[ALIAS]  [IMAGE] [COMMAND]

演示:
新建一个容器,当它链接到cct1时候,给它起个别名 webtest

hejing@learning:~$ docker run -it --name cct3 --link=cct1:webtest 2010jing/cct

ping webtest, 也就是 cct1

root@f1ccf8527981:/# ping webtest
PING webtest (192.168.100.2) 56(84) bytes of data.
64 bytes from webtest (192.168.100.2): icmp_seq=1 ttl=64 time=0.200 ms
64 bytes from webtest (192.168.100.2): icmp_seq=2 ttl=64 time=0.121 ms
64 bytes from webtest (192.168.100.2): icmp_seq=3 ttl=64 time=0.119 ms
64 bytes from webtest (192.168.100.2): icmp_seq=4 ttl=64 time=0.117 ms

查看 env

...
WEBTEST_PORT_80_TCP_ADDR=192.168.100.2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
WEBTEST_PORT_80_TCP=tcp://192.168.100.2:80
WEBTEST_PORT=tcp://192.168.100.2:80
WEBTEST_PORT_80_TCP_PROTO=tcp
SHLVL=1
HOME=/root
WEBTEST_PORT_80_TCP_PORT=80
_=/usr/bin/env
...

/etc/hosts

root@f1ccf8527981:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.100.2   webtest 361c52e5f2a1 cct1
192.168.100.4   f1ccf8527981
root@f1ccf8527981:/#

测试,停止容器,重启启动,再链接

hejing@learning:~$ sudo service docker restart
[sudo] password for hejing:
hejing@learning:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
hejing@learning:~$ docker restart cct2 cct1 cct3
cct1
cct2
cct3
hejing@learning:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
f1ccf8527981        2010jing/cct        "/bin/sh -c /bin/bash"   8 minutes ago       Up 24 seconds       80/tcp              cct3
8eb87baab309        2010jing/cct        "/bin/sh -c /bin/bash"   23 minutes ago      Up 25 seconds       80/tcp              cct2
361c52e5f2a1        2010jing/cct        "/bin/sh -c /bin/bash"   27 minutes ago      Up 26 seconds       80/tcp              cct1
hejing@learning:~$
hejing@learning:~$ docker attach cct3
root@f1ccf8527981:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.100.3   webtest 361c52e5f2a1 cct1 
192.168.100.4   f1ccf8527981
root@f1ccf8527981:/#

可以对比前后,webtest的ip 重启之后 由192.168.100.2 变成 192.168.100.3。


拒绝所有容器间的访问

Docker 守护进程的启动选项 --icc = false
修改配置文件 /etc/default/docker

DOCKER_OPTS=' --icc=false --registry-mirror=http://8fadea85.m.daocloud.io --label name=docker_server1 -b=br0'

重启docker和容器

hejing@learning:~$ sudo service docker restart
hejing@learning:~$ docker restart cct1 cct2 cct3
cct1
cct2
cct3
hejing@learning:~$ docker attach cct3
root@f1ccf8527981:/# ping webtest
PING webtest (192.168.100.2) 56(84) bytes of data.

发现ping不成功。

允许特定容器间的链接

Docker 守护进程的启动选项
--icc = false
--iptables = true
-- link

添加 --iptables=true 到配置文件 /etc/default/docker

root@f1ccf8527981:/# ps -ef | grep docker
root        14     6  0 08:38 ?        00:00:00 grep --color=auto docker
root@f1ccf8527981:/# hejing@learning:~$ ps -ef | grep docker
root     13097     1  0 16:35 ?        00:00:01 /usr/bin/dockerd -H fd:// --icc=false --iptables=true --registry-mirror=http://8fadea85.m.daocloud.io --label name=docker_server1 -b=br0

重启docker和容器

root@f1ccf8527981:/docker attach cct3
hejing@learning:~$ sudo vim /etc/default/docker
hejing@learning:~$ sudo service docker restart
hejing@learning:~$ docker restart cct1 cct2 cct3
cct1
cct2
cct3

回到 cct1容器 并且启动 nginx服务

hejing@learning:~$ docker attach cct1
root@361c52e5f2a1:/# nginx
root@361c52e5f2a1:/# hejing@learning:~$
hejing@learning:~$

切回 cct3 访问 cct1

hejing@learning:~$
hejing@learning:~$ docker attach cct3
root@f1ccf8527981:/# curl webtest
<!DOCTYPE html>
<html>
<head>
        <title></title>
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js
"></script>

<style type="text/css">

        .hello{
                color:red;
        }

</style>
</head>
<body>
        <h3> welcome to <b class="hello">uic</b></h3>


        <select name="a1" id="a1" >
                <option value="a"> A</option>
                <option value="b"> B</option>
                <option value="c"> C</option>
        </select>

        <select name="a2" id="a2" >
                <option value="a"> A</option>
                <option value="b"> B</option>
                <option value="c"> C</option>
        </select>

        <select name="a3" id="a3" >
                <option value="a"> A</option>
                <option value="b"> B</option>
                <option value="c"> C</option>
        </select>

</body>

<script type="text/javascript">
        $("#a1").change(function(){
                //code...

                var selectedVal=$("#a1 ").val();  //获取Select选择的索引值
                var temp = "#a2 option[value='"+selectedVal+"']";
                alert(temp);
                $(temp).remove();  //删除Select中Value='3'的Option

        });

</script>
</html>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342

推荐阅读更多精彩内容