Rede Bridge na prática
Laboratório
3 redes por padrão:
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
4fd474dd0ebe bridge bridge local
73f2d51a39de host host local
9198ba9fc6fd none null local
Inspeção da rede bridge:
$ docker network inspect 4fd474dd0ebe
[
{
"Name": "bridge",
"Id": "4fd474dd0ebe7aad9a3d19048b4544eca0dd2642bee8f6b0ceb3e1dbbead1824",
"Created": "2024-08-27T08:18:12.74665459-03:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"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": "1500"
},
"Labels": {}
}
]
Os container criados, por padrão, são inseridos na rede bridge.
Por exemplo:
$ docker container run -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e4fff0779e6d: Pull complete
2a0cb278fd9f: Pull complete
7045d6c32ae2: Pull complete
03de31afb035: Pull complete
0f17be8dcff2: Pull complete
14b7e5e8f394: Pull complete
23fa5a7b99a6: Pull complete
Digest: sha256:447a8665cc1dab95b1ca778e162215839ccbb9189104c79d7ec3a81e14577add
Status: Downloaded newer image for nginx:latest
5c428ec1a30f3e18fb0ac7433fcec9f4e562aa0410e6002aaf6796a207ac7f31
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c428ec1a30f nginx "/docker-entrypoint.…" 25 seconds ago Up 24 seconds 80/tcp jolly_albattani
$ docker container inspect 5c428ec1a30f | grep -i network
"NetworkMode": "bridge",
"NetworkSettings": {
"Networks": {
"NetworkID": "4fd474dd0ebe7aad9a3d19048b4544eca0dd2642bee8f6b0ceb3e1dbbead1824",
$ docker container inspect 5c428ec1a30f | grep -i gateway
"Gateway": "172.17.0.1",
"IPv6Gateway": "",
"Gateway": "172.17.0.1",
"IPv6Gateway": "",
$ docker container inspect 5c428ec1a30f | grep -i ipaddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
Outro exemplo:
$ docker container run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
31e907dcc94a: Already exists
Digest: sha256:8a37d68f4f73ebf3d4efafbcf66379bf3728902a8038616808f04e34a9ab63ee
Status: Downloaded newer image for ubuntu:latest
root@4b2a2198ec89:/#
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b2a2198ec89 ubuntu "/bin/bash" 33 seconds ago Up 32 seconds charming_wilbur
$ docker container inspect 4b2a2198ec89 | grep -i network
"NetworkMode": "bridge",
"NetworkSettings": {
"Networks": {
"NetworkID": "4fd474dd0ebe7aad9a3d19048b4544eca0dd2642bee8f6b0ceb3e1dbbead1824",
$ docker container inspect 4b2a2198ec89 | grep -i gateway
"Gateway": "172.17.0.1",
"IPv6Gateway": "",
"Gateway": "172.17.0.1",
"IPv6Gateway": "",
$ docker container inspect 4b2a2198ec89 | grep -i ipaddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
Os 2 containers foram inseridos na mesma rede com o modo bridge:
Instalar o curl no container em que o Ubuntu está sendo executado e acessar o NgINX que está sendo executado no outro container:
root@4b2a2198ec89:/# apt update && app install curl --yes
root@4b2a2198ec89:/# curl http://172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
O container é efêmero. Trabalhar com endereço IP no container é errado.
Simulação de cenário com a exclusão do container do NgINX e a criação de novos containeres (i.e., hello-world, conversao-temperatura, nginx etc.):
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b2a2198ec89 ubuntu "/bin/bash" 23 hours ago Up 59 seconds charming_wilbur
5c428ec1a30f nginx "/docker-entrypoint.…" 23 hours ago Up 1 second 80/tcp jolly_albattani
$ docker container rm -f 5c428ec1a30f
5c428ec1a30f
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b2a2198ec89 ubuntu "/bin/bash" 23 hours ago Up About a minute charming_wilbur
$ docker container run -d hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:53cc4d415d839c98be39331c948609b659ed725170ad2ca8eb36951288f81b75
Status: Downloaded newer image for hello-world:latest
8eea478c3a607487fb35456465dcc06473ce8c0917afd8b44f109a913e8a84f5
$ docker container run -d marcelodpbarbieri/conversao-temperatura:v1
Unable to find image 'marcelodpbarbieri/conversao-temperatura:v1' locally
v1: Pulling from marcelodpbarbieri/conversao-temperatura
31e907dcc94a: Already exists
2b84bdeb597a: Pull complete
f0392a7398c4: Pull complete
f41b53d4e76c: Pull complete
8dadaaf7622a: Pull complete
Digest: sha256:1fa5eb0aecb5e46bc7ed3e01f3ee9e3c8fb7336f9fea64b8839690825d1fa11b
Status: Downloaded newer image for marcelodpbarbieri/conversao-temperatura:v1
0682a0a0f8017ace97f71d82177a3849d978758c9d022d4da95a9b68eeb4c643
$ docker container run -d nginx
c50920d72e98bb580168c73dbd21eeffd21eccd59acb7227b2386fb483d11cd9
Inspecionar o endereço IP do container NgINX para verificar que o endereço IP não é o mesmo. Antes era 172.17.0.2 e agora é 172.17.0.4:
$ docker container inspect c50920d72e98bb580168c73dbd21eeffd21eccd59acb7227b2386fb483d11cd9 | grep
-i ipaddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.4",
"IPAddress": "172.17.0.4",
O acesso ao container antes parametrizado com o endereço IP 172.17.0.2 não está mais acessível:
$ docker container exec -it 4b2a /bin/bash
root@4b2a2198ec89:/# curl http://172.17.0.2
curl: (7) Failed to connect to 172.17.0.2 port 80 after 0 ms: Couldn't connect to server
Teria que ser utilizado o novo endereço IP 172.17.0.4:
root@4b2a2198ec89:/# curl http://172.17.0.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
Apesar da rede no modo bridge ser disponibilizada pelo Docker, não deve ser utilizada. Ela não tem um DNS vinculada a ela. Ela somente trabalha com endereço IP.
Last updated