Sitemap

Creating Isolated Network in Docker

3 min readMar 6, 2024

--

In this guide we will create docker containers on a host and utilize a Custom Bridge Network in order to create isolation between them.

Prerequisites: Docker CLI

The build begins:

First we will create two docker containers. They will be named login and logout respectively. My advice when creating the containers would be to create a new tab in the terminal for each.
To do create a container run the below command:

docker run -d --name <name of the container> nginx:latest
Press enter or click to view image in full size
Creation of the login container
Press enter or click to view image in full size
creation of the logout container

In order to confirm that these two containers can communicate with each other we will login to the login container, installing ping and ping the logout container.

To do this login to the login container by running the command:

docker exec -it login /bin/bash

We will also be updating the container

apt update
Press enter or click to view image in full size
logged into the login container

Next to provide this container with the capability to ping another we will install the ping command:

 apt install iputils-ping -y
Press enter or click to view image in full size
Installing ping

To ping the logout container from the login container we will need to the ip-address of the logout container.
To attain this go to another tab and type:

docker inspect logout
Press enter or click to view image in full size
Results from docker inspect

In the above we can see that the ip address is 172.17.0.2.
Using this, return to the login command and send a ping.

Press enter or click to view image in full size
Ping!

Here we see that the login container is able to ping the logout container.

Get Akil Blanchard’s stories in your inbox

Join Medium for free to get updates from this writer.

Now we will create our Custom Bridge Network called ‘secure-network’ and then the finance container.
It is necessary to create the network before the container as we will have to pass in the network name as an attribute when creating the container.

docker network create secure-network

To then create the container run:

docker run -d --name finance --network=secure-network nginx:latest
Press enter or click to view image in full size
creating the network and container

Let us take a look at the ip address of the finance container:

docker inspect finance
Press enter or click to view image in full size
Finance container inspect

Here we can see two main differences: The Network name is secure-network as opposed to bridge and the container has an ip address of 172.20.0.2.

Let us attempt to ping this container from the logout container.

Press enter or click to view image in full size
Results of pinging the finance container from the logout container

We have no communication between the two as they are isolated from each other.

If you want to see all the networks that are currently running you can do this by running:

docker network ls
Press enter or click to view image in full size
running networks on my system

To see the running containers:

docker ps
Press enter or click to view image in full size
running containers on my system

--

--