docker容器互连

容器别名

root@virtual-machine:/etc/docker# docker run -it -d --name myos1 ubuntu:net /bin/bash
de03145b828e19e73a727f2321a40d09c1a42fcad07327ae989cbe443f96c47c
root@virtual-machine:/etc/docker#
root@virtual-machine:/etc/docker# docker run -it -d --name myos2 --link=myos1:os1  ubuntu:net /bin/bash
b1aef9bc5f7f809e88bb9541c16423555cec4a573e2e4471909c0c5b612f194d
root@virtual-machine:/etc/docker# docker exec -it myos2 /bin/bash
root@b1aef9bc5f7f:/# ping os1
PING os1 (172.17.0.2) 56(84) bytes of data.
64 bytes from os1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.171 ms
64 bytes from os1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.155 ms
64 bytes from os1 (172.17.0.2): icmp_seq=3 ttl=64 time=0.129 ms
^C
--- os1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2018ms
rtt min/avg/max/mdev = 0.129/0.151/0.171/0.022 ms
root@b1aef9bc5f7f:/# 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
172.17.0.2      os1 de03145b828e myos1
172.17.0.3      b1aef9bc5f7
root@b1aef9bc5f7f:/# exit
exit
root@virtual-machine:/etc/docker#

阻断容器互连

root@virtual-machine:~# cat /etc/default/docker
DOCKER_OPTS="--icc=false"
root@virtual-machine:~# ps aux | grep docker
root      10159  2.4  8.2 550592 81384 ?        Ssl  11:28   0:00 /usr/bin/dockerd -H fd:// --icc=false
root      10288  0.0  0.0  21572   920 pts/8    S+   11:28   0:00 grep --color=auto docker
root@virtual-machine:~# docker rm `docker ps -aq`
9ea9dc72538a
7ba1a068cd9d
root@virtual-machine:~# docker run -it -d --name myos1 ubuntu:net /bin/bash
c2ef906327dcbb0e8ad4178d87d66383c81422823bd4ab61cc0e612cec687069
root@virtual-machine:~# docker run -it -d --name myos2 ubuntu:net /bin/bash
8ebe1df90b9e6f4181a08caeba8c8d0e14563faf0369711b88d82206551a7201
root@virtual-machine:~# docker exec -it myos1 /bin/bash
root@c2ef906327dc:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 23  bytes 2901 (2.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@c2ef906327dc:/# exit
exit
root@virtual-machine:~# docker exec -it myos2 /bin/bash
root@8ebe1df90b9e:/# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
^C
--- 172.17.0.2 ping statistics ---
7 packets transmitted, 0 received, 100% packet loss, time 6130ms

root@8ebe1df90b9e:/#
 

指定特定的容器互连

root@virtual-machine:~# ps aux | grep docker
root      11505  0.8  8.2 419264 81444 ?        Ssl  11:46   0:00 /usr/bin/dockerd -H fd:// --icc=false --iptables=true
root      11659  0.0  0.0  21572   936 pts/8    S+   11:47   0:00 grep --color=auto docker
root@virtual-machine:~# docker run -it -d --name myos1 ubuntu:nginx /bin/bash
cdce08ced79ad905afa41d36fe2b852002f652a24e1e7ea93ed3d5158a012464
root@virtual-machine:~# docker run -it -d --name myos2 ubuntu:nginx /bin/bash
c9fbaaba148c217c972bb53fcb43785b83596dd3d25da353d7fbf9bdac193d30

root@virtual-machine:~# docker exec -it myos1 /bin/bash
root@cdce08ced79a:/# nginx
root@cdce08ced79a:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 66  bytes 6055 (6.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 4776 (4.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@cdce08ced79a:/# exit
exit
root@virtual-machine:~# docker exec -it myos2 /bin/bash
root@c9fbaaba148c:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 36  bytes 3776 (3.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 274 (274.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@c9fbaaba148c:/# exit
root@virtual-machine:~# docker run -it -d --name myos4 --link=myos1:os1 ubuntu:nginx /bin/bash
1fe744247f97332dcc9d812345f04df32faaff48be2b6cce048092c05ab60a35

root@virtual-machine:~# docker exec -it myos4 /bin/bash
root@1fe744247f97:/# curl os1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

root@1fe744247f97:/# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

root@1fe744247f97:/# curl 172.17.0.3
^C

root@virtual-machine:~# iptables -S
-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
-N DOCKER
-N DOCKER-ISOLATION-STAGE-1
-N DOCKER-ISOLATION-STAGE-2
-N DOCKER-USER
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j DROP
-A DOCKER -s 172.17.0.5/32 -d 172.17.0.2/32 -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER -s 172.17.0.2/32 -d 172.17.0.5/32 -i docker0 -o docker0 -p tcp -m tcp --sport 80 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN
root@virtual-machine:~# cat /etc/docker/dockerfile
FROM ubuntu
MAINTAINER haojie

#RUN 执行以下命令
RUN apt-get update
RUN apt-get install iputils-ping -y
RUN apt-get install nginx -y
RUN apt-get install curl -y
RUN apt-get install net-tools -y
EXPOSE 80

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker容器是一种轻量级、可移植的虚拟化技术,用于在操作系统级别隔离应用程序和其依赖的运行环境。通过使用Docker容器,可以将应用程序及其所有依赖项打包到一个独立的、可移植的容器中,然后在任何支持Docker的环境中运行。 以下是一些常见的Docker容器相关概念和工具: 1. Docker镜像(Docker Image):Docker镜像是一个只读的模板,包含了运行应用程序所需的所有文件和依赖项。可以通过Docker镜像创建Docker容器。 2. Docker容器Docker Container):Docker容器Docker镜像的运行实例。每个Docker容器都是相互隔离的,具有自己的文件系统、进程空间和网络接口。 3. Docker引擎(Docker Engine):Docker引擎是Docker的核心组件,负责管理和运行Docker容器。它包括了一个守护进程(dockerd)和一组命令行工具(docker命令)。 4. Docker Compose:Docker Compose是一个用于定义和管理多个Docker容器的工具。通过编写一个YAML文件来描述应用程序的各个组件及其依赖关系,然后使用docker-compose命令来启动、停止和管理这些容器。 5. Kubernetes:Kubernetes是一个用于自动化部署、扩展和管理容器化应用程序的开源平台。它可以与Docker结合使用,提供了更高级的容器编排和管理功能。 6. Docker Hub:Docker Hub是一个公共的Docker镜像仓库,可以从中获取各种预构建的Docker镜像。同时,也可以将自己构建的镜像推送到Docker Hub上进行分享和存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值