Docker - Assign an IP address to a container using the docker run --ip command

When creating or starting a container using the docker run command, the --ip option can be used to declare the IP address that will be assigned to the container. In this example, a container is created using the foo:latest image, and the container will have IP address 172.16.0.100.

docker run --ip 172.16.0.100 foo:latest

 

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, you will need to use an IP address in the 172.17.0.0/16 subnet range.

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

 




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 94331 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |