Docker - Assign an IP address to a container using the docker run --ip command
by
Jeremy Canfield |
Updated: February 13 2022
| Docker articles
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