Diferente da sub rede padrão disponibilizada na instalação:
Criação de container nginx com a definição do nome do container:
Criação de container ubuntu em modo interativo com a instalação do curl e a tentativa de acessar o container nginx:
Não foi possível resolver o nome do host. Pois a rede não foi definida no momento da criação do container
Lista dos containers em execução e a inspeção do container nginx sendo executado na rede padrão da instalação Docker:
Conectar o container nginx na rede custom:
Desconectar a rede bridge:
Para que o container do ubuntu consiga acessar o container do nginx ele precisa estar na mesma sub rede:
Com isso será possível ao container ubuntu acessar o container nginx:
Ao criar a rede custo o Docker sugeriu a utilização da sub rede 172.18.0.0/16. Porém se já existir essa sub rede fora da estrutura Docker haverá conflito:
Especificação manual da sub rede:
Utilizar as especificações --subnet e --gateway:
Conexão dos containers existentes:
O acesso ao container nginx a partir do ubuntu continua disponível:
Criação de container já especificando a rede a ser conectada. O container nginx continua sendo acessível agora a partir do novo container:
Criação de container conversao-temperatura na mesma sub rede:
Bridges distintas
Existe a possibilidade de criar os container com isolamento de rede, conforme imagem abaixo:
Criação das redes:
Criação dos containers nginx1 e ubuntu1 para a rede custom1, e demonstração do acesso ao container nginx1 e a falta de acesso ao container nginx2:
Criação dos containers nginx2 e ubuntu2 para a rede custom2, e demonstração do acesso ao container nginx2 e a falta de acesso ao container nginx1:
Containers criados:
Inspeção dos endereços IP dos containers da rede custom1:
Inspeção dos endereços IP dos containers da rede custom2:
Alterar endereço IP da rede padrão "bridge"
Para alterar os endereços IP faz-se necessário alterar o arquivo /etc/docker/daemon.json conforme documentação existente no link abaixo:
$ docker network create custom
02c206fcb9c9ad9f850c5de72758d80649ee27e9780e72235238f38be126f953
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
29f83204d89d bridge bridge local
02c206fcb9c9 custom bridge local
73f2d51a39de host host local
9198ba9fc6fd none null local
root@70b6fbcf8952:/# curl http://nginx
<!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>
~$ docker network create --help
Usage: docker network create [OPTIONS] NETWORK
Create a network
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
--config-from string The network from which to copy the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--ingress Create swarm routing-mesh network
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enable or disable IPv6 networking
--label list Set metadata on a network
-o, --opt map Set driver specific options (default map[])
--scope string Control the network's scope
--subnet strings Subnet in CIDR format that represents a network segment
$ docker network create --subnet=10.0.0.0/16 --gateway=10.0.0.1 custom2
5460187d15b4d94021a160f231ab4f8e0aa4ca04bc8915eeaf34cd3cebd05e4e
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
29f83204d89d bridge bridge local
02c206fcb9c9 custom bridge local
5460187d15b4 custom2 bridge local
73f2d51a39de host host local
9198ba9fc6fd none null local
$ docker network inspect custom2 | grep -Ei "subnet|gateway|ipaddress"
"Subnet": "10.0.0.0/16",
"Gateway": "10.0.0.1"
root@70b6fbcf8952:/# curl http://nginx
<!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>
$ docker container run -it --network custom2 ubuntu /bin/bash
root@00775d9f7e89:/# apt update && apt install curl --yes && curl
curl: try 'curl --help' or 'curl --manual' for more information
root@00775d9f7e89:/# curl http://nginx
<!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>
$ docker container run -d --network custom2 marcelodpbarbieri/conversao-temperatura:v1
aebe591402c8dc9cad3528db88f61cfe195a8d0b00df8c0ed5dff63b322f77b1
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aebe591402c8 marcelodpbarbieri/conversao-temperatura:v1 "node server.js" 47 seconds ago Up 46 seconds xenodochial_kirch
00775d9f7e89 ubuntu "/bin/bash" 7 minutes ago Up 7 minutes serene_goldberg
70b6fbcf8952 ubuntu "/bin/bash" 57 minutes ago Up 57 minutes infallible_clarke
dadeac1f8244 nginx "/docker-entrypoint.…" 58 minutes ago Up 58 minutes 80/tcp nginx
$ docker container inspect xenodochial_kirch | grep -Ei "network|ipaddress|gateway"
"NetworkMode": "custom2",
"NetworkSettings": {
"SecondaryIPAddresses": null,
"Gateway": "",
"IPAddress": "",
"IPv6Gateway": "",
"Networks": {
"NetworkID": "5460187d15b4d94021a160f231ab4f8e0aa4ca04bc8915eeaf34cd3cebd05e4e",
"Gateway": "10.0.0.1",
"IPAddress": "10.0.0.5",
"IPv6Gateway": "",
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
29f83204d89d bridge bridge local
73f2d51a39de host host local
9198ba9fc6fd none null local
$ docker network create --subnet=10.0.0.0/16 --gateway=10.0.0.1 custom1
9eb5a4ec5d86c38761768055767a8a077f3a290e6ec63791df921d07ed574050
$ docker network create --subnet=20.0.0.0/16 --gateway=20.0.0.1 custom2
aeac0dbaf41fab8360b2a81e50a0ee3d239fd8404c0a6ccc21d6608cf5aa1235
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
29f83204d89d bridge bridge local
9eb5a4ec5d86 custom1 bridge local
aeac0dbaf41f custom2 bridge local
73f2d51a39de host host local
9198ba9fc6fd none null local
$ docker container run -d --network custom1 --name nginx1 nginx
911750d6bec5ada5658d76273558bfef99ab50deadbbb9d8dbe86a5f19a65897
$ docker container run -it --network custom1 --name ubuntu1 ubuntu /bin/bash
root@91fa3d41367b:/# apt update && apt install curl --yes
root@91fa3d41367b:/# curl
curl: try 'curl --help' or 'curl --manual' for more information
root@91fa3d41367b:/# curl nginx1
<!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>
root@91fa3d41367b:/# curl nginx2
curl: (6) Could not resolve host: nginx2
$ docker container run -d --network custom2 --name nginx2 nginx
885e750713e13991c0ba77dbfcbfbff9d6e8d69af38864910feef8154946d571
$ docker container run -it --network custom2 --name ubuntu2 ubuntu /bin/bash
root@874701ee0b12:/# apt update && apt install curl --yes
root@874701ee0b12:/# curl
curl: try 'curl --help' or 'curl --manual' for more information
root@874701ee0b12:/# curl nginx2
<!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>
root@874701ee0b12:/# curl nginx1
curl: (6) Could not resolve host: nginx1
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
874701ee0b12 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes ubuntu2
91fa3d41367b ubuntu "/bin/bash" 2 minutes ago Up 2 minutes ubuntu1
885e750713e1 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp nginx2
911750d6bec5 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp nginx1
$ docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Networks}}"
CONTAINER ID NAMES IMAGE NETWORKS
874701ee0b12 ubuntu2 ubuntu custom2
91fa3d41367b ubuntu1 ubuntu custom1
885e750713e1 nginx2 nginx custom2
911750d6bec5 nginx1 nginx custom1