Docker networking

Containers run in isolation and don’t know anything about other processes or containers on the same machine. You have to options to make them communicate: Via Container Linking (aka legacy linking) or via Container Networks. Linking containers is simple to setup, but also limited and should only be used during development.

Container Linking (Legacy Linking)

Let’s assume you have to Containers: The first one is running MongoDB, the second is running NodeJS and you want to connect them via Container Linking. First you give MongoDb container a name:

# Run container with name
docker run -d --name my-mongodb mongo

Then your second container (in this example node) can be linked to it:

# Link node container to my-mongo and choose alias 'mongodb'
docker run -d --link my-mongo:mongodb node

The alias mongodb that you chose is now available as a host within the node container:

  "databaseConfig" : {
    "host" : "mongodb",
    "database" : "myDatabase"

You can repeat this process if you want to join more containers.

Creating a network

If two containers are on the same network, they can talk to each other. If they aren’t, they can’t. You can group containers into their own isolated network.

# list networks and their drivers
docker network ls

# list containers attached to the network, subnet, gateway and others
docker network inspect my-network

First you have to create a custom bridge network. After that there are two ways to put a container on a network: Assign the network when starting the Container or add an existing container to the network.

# create bridged network with name 'isolated-network'
docker network create --driver bridge isolated-network

Now, start the first container and attach it to the network:

# run detached container and assign network
docker run -d \
     --network isolated-network \
     --network-alias mongodb \

# --network is same as --net
# --network-alias is same as --net-alias

Having network-alias mongodb allows us to connect to this container using mongodb as host name.

Run the second container and attach it to the same network:

# run detached container and assign network
docker run -d \
     --network isolated-network \
     --network-alias node-app \

Linking multiple containers

If you have many containers it can be tedious to create networks and attach them to it via command line every time. Instead you should use Docker Compose.

About Author

Mathias Bothe To my job profile

I am Mathias, born 39 years ago in Heidelberg, Germany. Today I am living in Munich and Stockholm. I am a passionate IT freelancer with more than 15 years experience in programming, especially in developing web based applications for companies that range from small startups to the big players out there. I am founder of, creator of the security service platform BosyProtect© and initiator of several other software projects.