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

Updated:   |  Docker articles

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"
                }
            ]
        }

 

When creating a container using the docker run command the --network option can be used to configure the container with a specific network.

sudo docker run --network foo-network --name my-container my-image:latest

 

Of if the container already exists the docker update command can be used.

sudo docker update --network foo-network my-container

 

You can use the docker inspect command to view the JSON for the container.

~]$ sudo docker inspect my-container | jq .[].NetworkSettings
{
  "Bridge": "",
  "SandboxID": "3bcbed81de9f0c6197e3b7866b875570aca0e7fea5b215dabda100dd878d1806",
  "HairpinMode": false,
  "LinkLocalIPv6Address": "",
  "LinkLocalIPv6PrefixLen": 0,
  "Ports": {
    "443/tcp": null,
    "80/tcp": [
      {
        "HostIp": "0.0.0.0",
        "HostPort": "18089"
      }
    ]
  },
  "SandboxKey": "/var/run/docker/netns/3bcbed81de9f",
  "SecondaryIPAddresses": null,
  "SecondaryIPv6Addresses": null,
  "EndpointID": "c2072a51155917302309744f69c76dfd71998b651e3eec4943a6f8e2fb706a76",
  "Gateway": "172.17.0.1",
  "GlobalIPv6Address": "",
  "GlobalIPv6PrefixLen": 0,
  "IPAddress": "172.17.0.15",
  "IPPrefixLen": 16,
  "IPv6Gateway": "",
  "MacAddress": "02:42:ac:11:00:05",
  "Networks": {
    "bridge": {
      "IPAMConfig": null,
      "Links": null,
      "Aliases": null,
      "NetworkID": "4775001eae48c1887abc1c0f1bd1887a1b85aa7b49794b94509921e64e65bafc",
      "EndpointID": "c2072a51155917302309744f69c76dfd71998b651e3eec4943a6f8e2fb706a76",
      "Gateway": "172.17.0.1",
      "IPAddress": "172.17.0.15",
      "IPPrefixLen": 16,
      "IPv6Gateway": "",
      "GlobalIPv6Address": "",
      "GlobalIPv6PrefixLen": 0,
      "MacAddress": "02:42:ac:11:00:05",
      "DriverOpts": null
    }
  }
}

 




Did you find this article helpful?

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



Comments


Add a Comment


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