Default networks

Default networks#

There are three default networks in Docker. Let’s look at them all.

Bridge#

This network makes it possible to communicate with containers using a set of rules.


The next cell starts a container connected to the bridge network.

Note the port specification is critical here.

docker run --rm -itd -p 80:80 \
    --name bridge_example \
    --network bridge \
    nginx &> /dev/null

You can access the container from the host through the specified port. The following cell curls the nginx welcome page.

curl -s localhost:80 | head -n 10
<!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>

And you can freely access the Internet from the container. The next cell shows curling httpbin from the container.

docker exec bridge_example curl -s https://httpbin.org/anything
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.88.1", 
    "X-Amzn-Trace-Id": "Root=1-66a63219-0e94bc7504020c563550a0bb"
  }, 
  "json": null, 
  "method": "GET", 
  "origin": "37.214.79.105", 
  "url": "https://httpbin.org/anything"
}

Don’t forget to stop the container after all.

docker stop bridge_example

None#

None network is just an abstraction that closes all network interactions with the container.


In the next cell port is thrown for the purpose of checking that we still can’t access the container even if the correct port is specified.

docker run --rm -itd -p 80:80 \
    --name none_example \
    --network none \
    nginx &> /dev/null

The next cell shows that curling the container from the host results in no response.

curl localhost:80
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused

Attempting to access the internet from the container will also result in an error.

docker exec -it none_example curl https://httpbin.org/anything
curl: (6) Could not resolve host: httpbin.org

Don’t forget to stop the container after all.

docker stop none_example

Host#

Completely removes the container’s network isolation, that is, the container shares the network completely with the host.


The following cell starts a container connected to the host network.

Note that there is no port specified - we don’t need it, we can communicate with the container just as we can with porcess on the host.

docker run --rm -itd \
    --name host_example \
    --network host  \
    nginx &> /dev/null

Even though port wasn’t specified for the container, we can still access the container from host using the default nginx port.

curl -s localhost:80 | head -n 10
<!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>

So there is internet access from the container.

docker exec -it host_example curl https://httpbin.org/anything
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.88.1", 
    "X-Amzn-Trace-Id": "Root=1-66a63b50-4d0c620c69944ca507bcc11c"
  }, 
  "json": null, 
  "method": "GET", 
  "origin": "37.214.79.105", 
  "url": "https://httpbin.org/anything"
}

Don’t forget to stop the container.

docker stop host_example
host_example

Connect/disconnect host network#

You cannot connect or disconnect host network.


The following cell shows an attempt to connect a container to the host network. The result is an error message.

docker run -itd --rm --name test_container alpine &> /dev/null
docker network disconnect bridge test_container
docker network connect host test_container
docker stop test_container &> /dev/null
Error response from daemon: container cannot be disconnected from host network or connected to host network