Docker network Commands (Docker Series - IV)
Tags: docker, devops, webdev, beginners
Published: 2022-08-16 13:53:00

Some network commands:

```
$ sudo docker network create --driver bridge my-bridge
3973cea88d3f345c60ade7414dee4ff0a09f2863d667e713acef61dcf4badfa6
```

The above code is to create a network of type bridge with default Ip address.

```
$ sudo docker network create --driver bridge --subnet=192.168.0.0/16 --ip-range=192.168.5.0/24 my-bridge-1

eb05210ca8a0c722684271373dbdb7ea2da57dd82754324c59ffac363be22cd0
```

The above code is to create bridge network with specified ip range and subnet

```
$ sudo docker network ls

NETWORK ID     NAME          DRIVER    SCOPE
a2884fc4f337   bridge        bridge    local
6bffa8afea2f   host          host      local
3973cea88d3f   my-bridge     bridge    local
eb05210ca8a0   my-bridge-1   bridge    local
4278b3734e55   none          null      local

```
apart from created network , docker provides us 3 network, host, bridge, and none is a special case where its completely isolated and lack of connnectivity.

```
$ sudo docker network ls --filter driver=bridge
NETWORK ID     NAME          DRIVER    SCOPE
a2884fc4f337   bridge        bridge    local
3973cea88d3f   my-bridge     bridge    local
eb05210ca8a0   my-bridge-1   bridge    local

```
the above code helps to filter the network which has driver as bridge.

## Connect Network:

```
$ sudo docker network connect my-bridge-1 flamboyant_wing
```
In the above code the we have established a bridge connection to running container. This wont return any id, to check we need to do 
`{% embed $ sudo docker inspect %} flamboyant_wing(container name)`

result 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcep7kshd4zi7rmjlwt2.png)
 
**Connect using network flag in run command:**

```
$ sudo docker container run -itd --network host --name cont_ngnix nginx:latest
9227518678e384232b6e34f31674e5618f17851d0f49a317c33294faecec06e5

```
Here we provided the host network. when we try to find the port 
`sudo docker container port cont_ngnix`
it returns nothing, since there is no port mapping as it's uses **host** ip address. instead try hitting the localhost 80 port which is host port.

Result on `$ sudo docker inspect cont_ngnix`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2zgrk88ybtsivoatjus.png)

## Inspect Network

```
 $ sudo docker network inspect my-bridge-1

[
    {
        "Name": "my-bridge-1",
        "Id": "eb05210ca8a0c722684271373dbdb7ea2da57dd82754324c59ffac363be22cd0",
        "Created": "2022-08-15T21:57:03.886178549+05:30",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        **"IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "IPRange": "192.168.5.0/24",
                    "Gateway": "192.168.5.0"
                }
            ]
        },**
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
       ** "Containers": {},**
        "Options": {},
        "Labels": {}
    }
]
```

the above command inspect user created network displays the related details.

**format inspected details:**

```
$sudo docker network inspect --format "{{.Scope}}" bridge
```
the above command helps to list the scope of bridge network

```
$sudo docker network inspect --format "{{.Id}}:{{.Name}}" bridge

f46032790f9dcb26f99afec265b1eea44fc96563836a85316c4346f36aa2c6fe:bridge

```
this command list the id and name of the bridge network 

## Disconnect Network

```
$sudo docker network disconnect my-bridge-1 flamboyant_wing

```