Docker - Choose the network that the container will use using the docker run -n or --network command

When creating or starting a container using the docker run command, the --network option can be used to choose the network that the container will use. 

By default, if the --network option is not used, the container will use the default bridge network.

~]# docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
7e6e54bde697   bridge            bridge    local
8ba129869d06   host              host      local
1143f39a14ae   none              null      local

 

The docker network inspect command can be used to see that the default bridge network uses IP addresses in the 172.17.0.0/16 subnet. Thus, if you do not include the --network option, DHCP will assign an IP address in the 172.17.0.0/16 subnet range to the container. Or, the --ip option could be used to assign a specific IP address to the container.

~]# docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "55b61b1a57f58bf3dc43b5262d6ed312ecd1ecc43a32e356d2f10edc1536a035",
        "Created": "2021-10-26T21:42:00.410817105-05:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        }

 

In this example, a container is created using the foo:latest image, and the container will use the network named "foo-network".

docker run --network foo-network foo:latest

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 17178 in the box below so that we can be sure you are a human.