Networking in Docker-Compose¶
One of the many confusing things for beginners is how networking works in Docker-Compose.
It's essentially you "Datacenter on a Laptop". That means you can have virtual switches, routers and vpc's, all on your laptop.
In this lecture we're having a closer look at how this exactly works!
Understanding the Basic Docker Bridge and Host Network¶
By default, docker creates an overlay network over all containers. A bridge. It's called "docker0". From the previous lectures there should be some networks left:
docker network ls
- Brings up a list of all networks
docker network prune
docker network ls
- Should now just list the necessary networks for docker to function
- A bridge
- A host
- A null
docker run --rm --name my-webserver -d httpd
- Start an apache webserver (httpd container)
- Detached (
-d
)
docker inspect my-webserver
-
Check the IP address of the container Open http://172.17.0.2 (or the IP address of your container)
-
It won't let you connect to it
- Unless you forward a port to your host with -p 8080:80 or so...
docker run --rm tomw1808/mycurl my-webserver
curl my-webserver
on any Linux
* It will end in an error
docker run --rm tomw1808/mycurl 172.17.0.2
So, we can't curl by name, we have to curl the exact IP Address
docker stop my-webserver
Hostname binding with Docker¶
Let's see if we can also get Hostname-binding to work with Docker...
docker network create simple-network
- Creates a new bridge network called "simple-network"
docker run --rm -d --name my-webserver --network simple-network httpd
- Start the webserver again attaching it to the "simple-network" we created earlier
docker run --rm --network simple-network appropriate/curl my-webserver
- Now the name binding works
- "It Works!"
docker inspect my-webserver
- Get the IP Address of your webserver
- It should be 172.22.0.2 (or so – copy the IP of your container here)
docker run --rm tomw1808/mycurl IP-OF-MY-WEBSERVER(e.g. 172.22.0.2)
- Run curl without the network, on the docker0 network
- You won't be able to connect to the webserver
- It's segregated from the other network
- Ctrl-c to stop
docker stop my-webserver
- Stop the container again
docker network rm simple-network
- Cleanup: remove the simple-network again