Learn how to create docker container with host network

The default network of newly created container is “bridge”, use the below command to list all available docker networks.

$ docker network ls

O/p looks like below.

NETWORK ID          NAME                DRIVER              SCOPE
c8135dce2b2e        bridge              bridge              local
a9c2cac718b4        host                host                local
0657bb14cd1c        none                null                local

After creating container, we can check its network details by issuing the below command.

$ docker network inspect <name of network>

Generally bridge network subnet is in 172 series, which can be witnessed with the below command.

$ docker network inspect bridge

And its corresponding output looks like this.

[
    {
        "Name": "bridge",
        "Id": "d9135dae2b2e892b30a16ec932e7f71e067ab1b18a3abd32c1a0b508cc85a57d",
        "Created": "2020-11-04T09:57:19.280868049+05:30"
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.1/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },

Sometimes, we will end up with issues which makes us unable to connect to the docker containers while being connected to VPN.

Hence host network comes in rescue, so follow the below step to create container with host network instead of default bridge network.

In below example, I’m trying to create container out of postgres image.

docker run -p 5432:5432 --network host --name mydb -e POSTGRES_PASSWORD=postgres postgres

PS:- host network is advisable when working with localhost as both host machine and host network are aligned and may not be recommended in production environment due to security issues.

Leave a Reply

Your email address will not be published. Required fields are marked *