Docker Virtual Networking

Manfred Lange
11 min readApr 15

A gentle introduction to virtual networks with docker compose files


In my previous article “Docker Port Mapping” I explained what port mapping is and demonstrated how to use it. Using a browser, we were able to send a request deliberately to one of two running containers.

In this article, we will expand on the concept and figure out how running containers can communicate with each other. We will cover:

  • How to configure a virtual network with Docker?
  • How to send a request from one running container to a different container?

Understanding virtual networks in Docker is an important building block for more advanced setups. Examples of these advanced scenarios are provided in this article’s summary.

The complete source code for this article is available at

Why do we need a Virtual Network?

Last time, we ran up two different instances of the same container image. For each we configured a different port mapping so that depending on what we used in a browser we would make a request to either one or the other instance. Let us take a simple example.

Let us run up two instances of the nginx as follows:

docker run -it -d -p 8080:80 — name thisservice nginx
docker run -it -d -p 8090:80 — name otherservice nginx

Then in Docker Desktop you should see the following:

Or if you prefer the command line interface (CLI):

One way to test if one container can communicate with the other container is by opening a terminal window for one of the running containers and then use “curl” to make a request to the other container. Let us try that.

docker exec -it thisservice bash

This should result in something like this:

Next, we use curl to make a request to “otherservice”. We may be tempted to use “curl http://localhost”. It will return…

Manfred Lange

CTO at MacroActive I write about improving flow of value in software engineering. LinkedIn