Docker: Networking Basics - Configure static and Dynamic IP Address to Container

Hello Everyone, 

        In this post, I will show you how to create a network in the docker and how to spin containers with static IP and Dynamic IP Address. 

 

Step1: Create a new network for docker

You can create a new network in the docker using below command

 

docker network create --subnet=172.18.0.0/16 mydockernetwork

As network created just do docker inspect to make sure all good with it

~ docker network create --subnet=172.18.0.0/16 mydockernetwork
a4d11712754b7d5bf3ba25e3c831c8512065ce77a66099948effc59d5bae968f
~ docker inspect mydockernetwork
[
    {
        "Name": "mydockernetwork",
        "Id": "a4d11712754b7d5bf3ba25e3c831c8512065ce77a66099948effc59d5bae968f",
        "Created": "2021-08-07T11:27:38.515202386+05:30",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
] 

Step2: Lets spin a new container with dynamic IP

~ docker run --net mydockernetwork -it --name ubuntu_dynamic_ip3 ubuntu

By using docker inspect command lets extract IP Address of the above container.

 

~ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ubuntu_dynamic_ip3
172.18.0.2

 Step 3: Lets spin a new container with static IP

~ docker run --net mydockernetwork --ip 172.18.0.4 -it --name ubuntu_static_ip ubuntu
root@71f58fb557c1:/# %                                                                        ~ docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
71f58fb557c1   ubuntu    "bash"    6 seconds ago   Up 5 seconds             ubuntu_static_ip
ab563564d964   ubuntu    "bash"    3 minutes ago   Up 3 minutes             ubuntu_dynamic_ip3
f4bc181d89af   ubuntu    "bash"    2 hours ago     Up 2 hours               ubuntu_container
~ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ubuntu_static_ip
172.18.0.4

 

Update #1: A Shell Function To Return Container IP Address.

As I have to get IP Address very often for different containers, I wrote a small function in bash which return IP Address of given container.

test_playbook git:(master)  function docker_ip
function> docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $1
test_playbook git:(master)  docker_ip webserver
172.18.0.5
test_playbook git:(master)  docker_ip mysql_db_ubuntu
172.18.0.4
test_playbook git:(master)  

 

You can add the function to your bashrc file and call the function any time. I use ZSH as my default shell, so I did as below

test_playbook git:(master)  vim ~/.zshrc       
test_playbook git:(master)  exec $SHELL
test_playbook git:(master)  docker_ip webserver
172.18.0.5
test_playbook git:(master)  tail -n 3 ~/.zshrc 

function docker_ip
  docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $1
test_playbook git:(master)  


Hope it helps.

Thank you.

0 comments:

Post a Comment