Skip to Content
CoursesLearn DockerUnderstand Networking in Docker

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!

View the Full Course Now 

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
  • Should delete all networks
  • Just to clean up – you don’t have to do this, but sometimes it’s good to start fresh
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
  • Should download and run an image called ā€œtomw1808/curlā€ which is just an ubuntu alpine with curl installed
  • And curl ā€œmy-webserverā€
    • Basically, the same as executing curl my-webserver on any Linux
  • It will end in an error
docker run --rm tomw1808/mycurl 172.17.0.2
  • Will output you the HTML of the Webserver
  • ā€œIt Works!ā€

So, we can’t curl by name, we have to curl the exact IP Address

docker stop my-webserver
  • Stops the webserver container

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
Last updated on