github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/docs/userguide/networkingcontainers.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Networking containers" 4 description = "How to manage data inside your Docker containers." 5 keywords = ["Examples, Usage, volume, docker, documentation, user guide, data, volumes"] 6 [menu.main] 7 parent = "smn_containers" 8 weight = -3 9 +++ 10 <![end-metadata]--> 11 12 13 # Networking containers 14 15 If you are working your way through the user guide, you just built and ran a 16 simple application. You've also built in your own images. This section teaches 17 you how to network your containers. 18 19 ## Name a container 20 21 You've already seen that each container you create has an automatically 22 created name; indeed you've become familiar with our old friend 23 `nostalgic_morse` during this guide. You can also name containers 24 yourself. This naming provides two useful functions: 25 26 * You can name containers that do specific functions in a way 27 that makes it easier for you to remember them, for example naming a 28 container containing a web application `web`. 29 30 * Names provide Docker with a reference point that allows it to refer to other 31 containers. There are several commands that support this and you'll use one in a exercise later. 32 33 You name your container by using the `--name` flag, for example launch a new container called web: 34 35 $ docker run -d -P --name web training/webapp python app.py 36 37 Use the `docker ps` command to see check the name: 38 39 $ docker ps -l 40 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 41 aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web 42 43 You can also use `docker inspect` with the container's name. 44 45 $ docker inspect web 46 [ 47 { 48 "Id": "3ce51710b34f5d6da95e0a340d32aa2e6cf64857fb8cdb2a6c38f7c56f448143", 49 "Created": "2015-10-25T22:44:17.854367116Z", 50 "Path": "python", 51 "Args": [ 52 "app.py" 53 ], 54 "State": { 55 "Status": "running", 56 "Running": true, 57 "Paused": false, 58 "Restarting": false, 59 "OOMKilled": false, 60 ... 61 62 Container names must be unique. That means you can only call one container 63 `web`. If you want to re-use a container name you must delete the old container 64 (with `docker rm`) before you can reuse the name with a new container. Go ahead and stop and remove your old `web` container. 65 66 $ docker stop web 67 web 68 $ docker rm web 69 web 70 71 72 ## Launch a container on the default network 73 74 Docker includes support for networking containers through the use of **network 75 drivers**. By default, Docker provides two network drivers for you, the 76 `bridge` and the `overlay` drivers. You can also write a network driver plugin so 77 that you can create your own drivers but that is an advanced task. 78 79 Every installation of the Docker Engine automatically includes three default networks. You can list them: 80 81 $ docker network ls 82 NETWORK ID NAME DRIVER 83 18a2866682b8 none null 84 c288470c46f6 host host 85 7b369448dccb bridge bridge 86 87 The network named `bridge` is a special network. Unless you tell it otherwise, Docker always launches your containers in this network. Try this now: 88 89 $ docker run -itd --name=networktest ubuntu 90 74695c9cea6d9810718fddadc01a727a5dd3ce6a69d09752239736c030599741 91 92 Inspecting the network is an easy way to find out the container's IP address. 93 94 ```bash 95 $ docker network inspect bridge 96 [ 97 { 98 "Name": "bridge", 99 "Id": "f7ab26d71dbd6f557852c7156ae0574bbf62c42f539b50c8ebde0f728a253b6f", 100 "Scope": "local", 101 "Driver": "bridge", 102 "IPAM": { 103 "Driver": "default", 104 "Config": [ 105 { 106 "Subnet": "172.17.0.1/16", 107 "Gateway": "172.17.0.1" 108 } 109 ] 110 }, 111 "Containers": { 112 "3386a527aa08b37ea9232cbcace2d2458d49f44bb05a6b775fba7ddd40d8f92c": { 113 "EndpointID": "647c12443e91faf0fd508b6edfe59c30b642abb60dfab890b4bdccee38750bc1", 114 "MacAddress": "02:42:ac:11:00:02", 115 "IPv4Address": "172.17.0.2/16", 116 "IPv6Address": "" 117 }, 118 "94447ca479852d29aeddca75c28f7104df3c3196d7b6d83061879e339946805c": { 119 "EndpointID": "b047d090f446ac49747d3c37d63e4307be745876db7f0ceef7b311cbba615f48", 120 "MacAddress": "02:42:ac:11:00:03", 121 "IPv4Address": "172.17.0.3/16", 122 "IPv6Address": "" 123 } 124 }, 125 "Options": { 126 "com.docker.network.bridge.default_bridge": "true", 127 "com.docker.network.bridge.enable_icc": "true", 128 "com.docker.network.bridge.enable_ip_masquerade": "true", 129 "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", 130 "com.docker.network.bridge.name": "docker0", 131 "com.docker.network.driver.mtu": "9001" 132 } 133 } 134 ] 135 ``` 136 137 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. 138 139 $ docker network disconnect bridge networktest 140 141 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. 142 143 ## Create your own bridge network 144 145 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: 146 147 $ docker network create -d bridge my-bridge-network 148 149 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: 150 151 $ docker network ls 152 NETWORK ID NAME DRIVER 153 7b369448dccb bridge bridge 154 615d565d498c my-bridge-network bridge 155 18a2866682b8 none null 156 c288470c46f6 host host 157 158 If you inspect the network, you'll find that it has nothing in it. 159 160 $ docker network inspect my-bridge-network 161 [ 162 { 163 "Name": "my-bridge-network", 164 "Id": "5a8afc6364bccb199540e133e63adb76a557906dd9ff82b94183fc48c40857ac", 165 "Scope": "local", 166 "Driver": "bridge", 167 "IPAM": { 168 "Driver": "default", 169 "Config": [ 170 {} 171 ] 172 }, 173 "Containers": {}, 174 "Options": {} 175 } 176 ] 177 178 ## Add containers to a network 179 180 To build web applications that act in concert but do so securely, create a 181 network. Networks, by definition, provide complete isolation for containers. You 182 can add containers to a network when you first run a container. 183 184 Launch a container running a PostgreSQL database and pass it the `--net=my-bridge-network` flag to connect it to your new network: 185 186 $ docker run -d --net=my-bridge-network --name db training/postgres 187 188 If you inspect your `my-bridge-network` you'll see it has a container attached. 189 You can also inspect your container to see where it is connected: 190 191 $ docker inspect --format='{{json .NetworkSettings.Networks}}' db 192 {"bridge":{"EndpointID":"508b170d56b2ac9e4ef86694b0a76a22dd3df1983404f7321da5649645bf7043","Gateway":"172.17.0.1","IPAddress":"172.17.0.3","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}} 193 194 Now, go ahead and start your by now familiar web application. This time leave off the `-P` flag and also don't specify a network. 195 196 $ docker run -d --name web training/webapp python app.py 197 198 Which network is your `web` application running under? Inspect the application and you'll find it is running in the default `bridge` network. 199 200 $ docker inspect --format='{{json .NetworkSettings.Networks}}' web 201 {"bridge":{"EndpointID":"508b170d56b2ac9e4ef86694b0a76a22dd3df1983404f7321da5649645bf7043","Gateway":"172.17.0.1","IPAddress":"172.17.0.3","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}} 202 203 Then, get the IP address of your `web` 204 205 $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web 206 172.17.0.2 207 208 Now, open a shell to your running `db` container: 209 210 $ docker exec -it db bash 211 root@a205f0dd33b2:/# ping 172.17.0.2 212 ping 172.17.0.2 213 PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data. 214 ^C 215 --- 172.17.0.2 ping statistics --- 216 44 packets transmitted, 0 received, 100% packet loss, time 43185ms 217 218 After a bit, use CTRL-C to end the `ping` and you'll find the ping failed. That is because the two container are running on different networks. You can fix that. Then, use CTRL-C to exit the container. 219 220 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-network`. 221 222 $ docker network connect my-bridge-network web 223 224 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. 225 226 $ docker exec -it db bash 227 root@a205f0dd33b2:/# ping web 228 PING web (172.19.0.3) 56(84) bytes of data. 229 64 bytes from web (172.19.0.3): icmp_seq=1 ttl=64 time=0.095 ms 230 64 bytes from web (172.19.0.3): icmp_seq=2 ttl=64 time=0.060 ms 231 64 bytes from web (172.19.0.3): icmp_seq=3 ttl=64 time=0.066 ms 232 ^C 233 --- web ping statistics --- 234 3 packets transmitted, 3 received, 0% packet loss, time 2000ms 235 rtt min/avg/max/mdev = 0.060/0.073/0.095/0.018 ms 236 237 The `ping` shows it is contacting a different IP address, the address on the `my-bridge-network` which is different from its address on the `bridge` network. 238 239 ## Next steps 240 241 Now that you know how to network containers, see [how to manage data in containers](dockervolumes.md).