how to network your containers.

Launch a container on the default network

Docker includes support for networking containers through the use of network drivers.

drivers. By default, Docker provides two network drivers for you, the bridge and the overlay drivers.

You can also write a network driver plugin so that you can create your own drivers but that is an advanced task.

Every installation of the Docker Engine automatically includes three default networks. You can list them:

$ docker network ls

NETWORK ID          NAME                DRIVER
18a2866682b8 none null
c288470c46f6 host host
7b369448dccb bridge bridge

The network named bridge is a special network. Unless you tell it otherwise, Docker always launches your containers in this network.

$ docker run -itd --name=networktest ubuntu

74695c9cea6d9810718fddadc01a727a5dd3ce6a69d09752239736c030599741

Inspecting the network is an easy way to find out the container’s IP address.

$ docker network inspect bridge

[
{
"Name": "bridge",
"Id": "f7ab26d71dbd6f557852c7156ae0574bbf62c42f539b50c8ebde0f728a253b6f",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.1/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Containers": {
"3386a527aa08b37ea9232cbcace2d2458d49f44bb05a6b775fba7ddd40d8f92c": {
"Name": "networktest",
"EndpointID": "647c12443e91faf0fd508b6edfe59c30b642abb60dfab890b4bdccee38750bc1",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "9001"
},
"Labels": {}
}
]

  

You can remove a container from a network by disconnecting the container.

To do this, you supply both the network name and the container name.

You can also use the container ID.

In this example, though, the name is faster.

$ docker network disconnect bridge networktest

While you can disconnect a container from a network, you cannot remove the builtin bridge network named bridge.

Networks are natural ways to isolate containers from other containers or other networks.

So, as you get more experienced with Docker, you’ll want to create your own networks. 

Create your own bridge network

Docker Engine natively supports both bridge networks and overlay networks.

A bridge network is limited to a single host running Docker Engine.

An overlay network can include multiple hosts and is a more advanced topic.

For this example, you’ll create a bridge network:

$ docker network create -d bridge my_bridge

The -d flag tells Docker to use the bridge driver for the new network.

You could have left this flag off as bridge is the default value for this flag.

Go ahead and list the networks on your machine:

$ docker network ls

NETWORK ID          NAME                DRIVER
7b369448dccb bridge bridge
615d565d498c my_bridge bridge
18a2866682b8 none null
c288470c46f6 host host

If you inspect the network, you’ll find that it has nothing in it.

$ docker network inspect my_bridge

[
{
"Name": "my_bridge",
"Id": "5a8afc6364bccb199540e133e63adb76a557906dd9ff82b94183fc48c40857ac",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Driver": "default",
"Config": [
{
"Subnet": "10.0.0.0/24",
"Gateway": "10.0.0.1"
}
]
},
"Containers": {},
"Options": {},
"Labels": {}
}
]

  

Add containers to a network

To build web applications that act in concert but do so securely, create a network.

Networks, by definition, provide complete isolation for containers.

You can add containers to a network when you first run a container.

Launch a container running a PostgreSQL database and pass it the --net=my_bridge flag to connect it to your new network:

$ docker run -d --net=my_bridge --name db training/postgres

If you inspect your my_bridge you’ll see it has a container attached. You can also inspect your container to see where it is connected:

$ docker inspect --format='{{json .NetworkSettings.Networks}}'  db

{"my_bridge":{"NetworkID":"7d86d31b1478e7cca9ebed7e73aa0fdeec46c5ca29497431d3007d2d9e15ed99",
"EndpointID":"508b170d56b2ac9e4ef86694b0a76a22dd3df1983404f7321da5649645bf7043","Gateway":"10.0.0.1","IPAddress":"10.0.0.254","IPPrefixLen":24,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}

  

Now, go ahead and start your by now familiar web application. This time don’t specify a network.

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

Which network is your web application running under?

Inspect the application and you’ll find it is running in the default bridgenetwork.

$ docker inspect --format='{{json .NetworkSettings.Networks}}'  web

{"bridge":{"NetworkID":"7ea29fc1412292a2d7bba362f9253545fecdfa8ce9a6e37dd10ba8bee7129812",
"EndpointID":"508b170d56b2ac9e4ef86694b0a76a22dd3df1983404f7321da5649645bf7043","Gateway":"172.17.0.1","IPAddress":"10.0.0.2","IPPrefixLen":24,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}

Then, get the IP address of your web

$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

172.17.0.2

Now, open a shell to your running db container:

$ docker exec -it db bash

root@a205f0dd33b2:/# ping 172.17.0.2
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 ---
44 packets transmitted, 0 received, 100% packet loss, time 43185ms

After a bit, use CTRL-C to end the ping and you’ll find the ping failed.

That is because the two containers are running on different networks.You can fix that.

Then, use the exit command to close the container.

Docker networking allows you to attach a container to as many networks as you like. You can also attach an already running container.

Go ahead and attach your running web app to the my_bridge.

$ docker network connect my_bridge web

Open a shell into the db application again and try the ping command. This time just use the container name web rather than the IP address.

$ docker exec -it db bash

root@a205f0dd33b2:/# ping web
PING web (10.0.0.2) 56(84) bytes of data.
64 bytes from web (10.0.0.2): icmp_seq=1 ttl=64 time=0.095 ms
64 bytes from web (10.0.0.2): icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from web (10.0.0.2): icmp_seq=3 ttl=64 time=0.066 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.060/0.073/0.095/0.018 ms 

The ping shows it is contacting a different IP address, the address on the my_bridge which is different from its address on the bridge network.

Docker6之Network containers的更多相关文章

  1. Docker Network containers

    Network containers Estimated reading time: 5 minutes If you are working your way through the user gu ...

  2. Docker-2:network containers

    docker run -d -P --name web training/webapp python app.py # -name means give the to-be-run container ...

  3. (转)Docker - 创建 Docker overlay network (containers 通信)

    原文链接: http://www.cnblogs.com/AlanWalkOn/p/6101875.html --- 创建基于Key-Value的Docker overlay network. 这样运 ...

  4. Docker - 运行 containers 使用在 swarm 模式下创建的 overlay 模式的 network

    前言 在Docker engine v1.12, 使用Swarm可以方便的创建overlay模式的网络,但是它只能被swarm下面的service所使用的,相对于container,这个网络是完全隔离 ...

  5. [Docker] Driver Bridge network for linking containers

    In previous postwe have seen how to link two container together by using `--link`: # docker run -d - ...

  6. 【Network】Calico, Flannel, Weave and Docker Overlay Network 各种网络模型之间的区别

    From the previous posts, I have analysed 4 different Docker multi-host network solutions - Calico, F ...

  7. docker offical docs:Working with Containers

    enough ---------------------------------------------------------------------------------- Working wi ...

  8. LXC-Linux Containers介绍

    Linux Containers,Linux的容器,容器嘛,可以想象成一个大的装东西的罐子,罐子口很大,里面可以装很多同样形状,只不过大小不同的小罐子.专业的话,叫做基于容器的操作系统层面的虚拟化技术 ...

  9. 逻辑网络(Logical Network)

    Introduction The VMM documentation indicates that “A logical network is used to organize and simplif ...

随机推荐

  1. restful的特点

    1. 资源(Resources) REST的名称”表现层状态转化”中,省略了主语.”表现层”其实指的是”资源”(Resources)的”表现层”.                所谓”资源”,就是网络 ...

  2. react 页面存在多 input 时

    this.setState({ [e.target.name]:e.target.value }) let o = {} o[e.target.name] = e.target.value this. ...

  3. maven 入门 (一)

    纠结了好久,到底要不要写一份maven入门的所谓“教程”,有好几次想写一下,但是都放弃了,因为网上的太多了,而且都很好,但是现在决定了,还是要写出来,因为者有我自己的理解.所以我想写一份教程出来. 首 ...

  4. Radio中REG

    Auto REG/REG OFF在广播接收质量不好时,收音机首先仅调整到该广播电台当前发射的可选频率.但是,如果接收质量差到“该发射电台濒临消失”的程度,则收音机也会接收德国NDR1(北德意志广播电台 ...

  5. Linux系统管理和维护常用命令

    Linux系统管理和维护常用命令 ls 命令 功能说明 ls 命令显示指定工作目录下的内容,列出工作目录所包含的文件及子目录. 语法结构: ls [选项] [路径或文件] ls 选项及说明 -a 显示 ...

  6. 使用Wisdom RESTClient自动化测试REST API,如何取消对返回的body内容的校验?

    使用Wisdom RESTClient V1.1 自动化测试API,默认是对返回HTTP状态码和body内容进行校验的. 如果您的API返回body内容是变化的,可以通过设置来取消对body内容的校验 ...

  7. java中的关键字、保留字、标识符

    Java关键字(KeyWord):  对Java的编译器有特殊的意义,他们用来表示一种数据类型或者表示程序的结构. Java保留字(Reserved Word):为Java预留的关键字,现在还没有用到 ...

  8. Docker学习笔记之使用 Docker Hub 中的镜像

    0x00 概述 自己编写 Dockerfile 能够很好的实现我们想要的程序运行环境,不过如果装有我们想要环境的镜像已经由热心的开发者构建好并共享在 Docker Hub 上,直接使用它们就会远比自己 ...

  9. TCP/IP编程——基于TCP的半关闭

    在TCP服务端和客户端建立连接之后服务端和客户端会分别有两个独立的输入流和输出流,而且相互对应.服务端的输出流对应于客户端的输入流,服务端的输入流对应于客户端的输出流.这是在建立连接之后的状态. 当我 ...

  10. Unity3D之主菜单

    1.新建一个名为MainMenu的C#脚本,修改编码后拖动到主摄像机,并给主摄像机添加一个AudioSource声音源作为背景音乐.将音乐文件赋值给Audio Clip属性. 2.创建一个Common ...