2018年12月8日 星期六

docker create net and container with statistic ip

env: CentOS 7
        Docker 18.09.0-ce-beta1

1.在docker的環境上建立名稱"my_net"的docker network
command:
docker network create --driver bridge my_net

[root@localhost mssql-tools]# docker network create --driver bridge my_net
87859b976e937fd1bed90ae23cdd02f0501e114ddb7933e5b326ed9a28e3e30a
[root@localhost mssql-tools]#

2.檢查我們建立的docker network "my_net"
command:
docker network ls 

[root@localhost mssql-tools]# docker network ls 
NETWORK ID          NAME                DRIVER              SCOPE
b67efb59762a        bridge              bridge              local
d87c295d4f6a        host                host                local
87859b976e93        my_net              bridge              local
7097737b04b0        nictest             bridge              local
69dd1233c5fa        none                null                local
[root@localhost mssql-tools]#

3.檢查docker network "my_net"設定資訊
command:
docker network inspect my_net

從這個指令可以看到"my_net"在預設狀況下使用bridge,subnet, getway都是docker內部的網路
資訊包含了名稱,ID,建立時間...等訊息

[root@localhost mssql-tools]# docker network inspect my_net
[
    {
        "Name": "my_net",
        "Id": "87859b976e937fd1bed90ae23cdd02f0501e114ddb7933e5b326ed9a28e3e30a",
        "Created": "2018-09-10T17:44:20.859416799+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

4.再建立docker network "my_net2",並且指定subnet, gateway
command:
docker network create --driver=bridge --subnet=172.22.16.0/24 --gateway 172.22.16.1 my_net2

[root@localhost mssql-tools]# docker network create --driver=bridge --subnet=172.22.16.0/24 --gateway 172.22.16.1 my_net2
39edd02299de93533d881ec6f6fe17b06b5d11cb7a08a86f60063d062bc0f533
[root@localhost mssql-tools]#

5.再次檢查docker network "my_net2"是否存在
command:
docker network ls

[root@localhost mssql-tools]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
b67efb59762a        bridge              bridge              local
d87c295d4f6a        host                host                local
87859b976e93        my_net              bridge              local
39edd02299de        my_net2             bridge              local
7097737b04b0        nictest             bridge              local
69dd1233c5fa        none                null                local

6.使用Linux指令"brctl"顯示bridge網路資訊
command:
brctl show

docker network "my_net2" id:39edd02299de93533d881ec6f6fe17b06b5d11cb7a08a86f60063d062bc0f533
所以"br-39edd02299de"是我們要找的

[root@localhost mssql-tools]# brctl show
bridge name bridge id  STP enabled interfaces
br-39edd02299de  8000.0242f072ef60 no 
br-7097737b04b0  8000.0242080373e8 no 
br-87859b976e93  8000.0242396adaf6 no 
docker0  8000.024268aae95c no 
virbr0  8000.525400267917 yes  virbr0-nic

7.查看bridge網路"br-39edd02299de"內容
command:
ifconfig br-39edd02299de

[root@localhost mssql-tools]# ifconfig br-39edd02299de
br-39edd02299de: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.22.16.1  netmask 255.255.255.0  broadcast 172.22.16.255
        ether 02:42:f0:72:ef:60  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8.使用image: SQL Server 2017建立docker container "sql1",並且指定docker network "my_net2",指定ip:172..22.16.8
command:
sudo docker run --ip 172.22.16.8 --network my_net2 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Passw0rd@1234' \
   -p 1433:1433 --name sql1 \
   -d microsoft/mssql-server-linux:2017-latest


[root@localhost ~]# sudo docker run --ip 172.22.16.8 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Passw0rd@1234' \
>    -p 1433:1433 --name sql1 \
>    -d microsoft/mssql-server-linux:2017-latest
9328a7bc6678952d4d33490e9b911c07a22d4b6fcbc85fa79b0034f3fbc66f6c

9.檢查docker container "sql1"是否建立成功
command:
docker container ps -a

[root@localhost ~]# docker container ps -a
CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
7324e572224a        microsoft/mssql-server-linux:2017-latest   "/opt/mssql/bin/sqls…"   7 minutes ago       Up 7 minutes        0.0.0.0:1433->1433/tcp   sql1


10.檢查container "sql1"的ip
command:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

[root@localhost ~]# docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' sql1
172.22.16.8
[root@localhost ~]#

11.同理,建立docker network "nicforsql",並且指定subnet, gateway到主機ip網段:192.168.220.x
command:
docker network create --driver=bridge --subnet=192.168.220.0/24 --gateway 192.168.220.1 nicforsql

[root@localhost ~]# docker network create --driver=bridge --subnet=192.168.220.0/24 --gateway 192.168.220.1 nicforsql
af015e482eba3b58cda8fc463c655363510288df5e848733135b89dc4bbacbc7
[root@localhost ~]#

12.檢查docker network "nicforsql"與bridge網路資訊
command:
docker network ls
brctl show

[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
9a9544204357        bridge              bridge              local
d87c295d4f6a        host                host                local
20b3681875d2        my_net2             bridge              local
af015e482eba        nicforsql           bridge              local
69dd1233c5fa        none                null                local
[root@localhost ~]# brctl show
bridge name bridge id  STP enabled interfaces
br-20b3681875d2  8000.0242bc8d9a2d no  veth02df188
br-af015e482eba  8000.024297a091f2 no 
docker0  8000.02428d26c03e no 
virbr0  8000.525400267917 yes  virbr0-nic
[root@localhost ~]#

13.檢查docker network "nicforsql" 設定資訊
command:
docker network inspect af015e482eba

[root@localhost ~]# docker network inspect af015e482eba
[
    {
        "Name": "nicforsql",
        "Id": "af015e482eba3b58cda8fc463c655363510288df5e848733135b89dc4bbacbc7",
        "Created": "2018-09-11T15:52:58.597646578+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.220.0/24",
                    "Gateway": "192.168.220.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
[root@localhost ~]#

14.使用image: SQL Server 2017建立docker container "sqltest",並且指定docker network "nicforsql",指定ip:192.168.220.8
command:
docker run -it --ip 192.168.220.8 --network nicforsql --name "sqltest" -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Passw0rd@1234' -p 1433:1433 -v /dockervol:/var/opt/mssql -d microsoft/mssql-server-linux:2017-latest 

[root@localhost dockervol2]# docker run -it --ip 192.168.220.8 --network nicforsql --name "sqltest" -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Passw0rd@1234' -p 1433:1433 -v /dockervol:/var/opt/mssql -d microsoft/mssql-server-linux:2017-latest
9b68c732bc5626539b5e1a9a959ef70a0dd2cacc6bf0f405108a9f37195f8b3d
[root@localhost dockervol2]#

15.檢查docker container "sqltest"是否建立成功 
command:
docker container ps -a

[root@localhost dockervol2]# docker container ps -a
CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
9b68c732bc56        microsoft/mssql-server-linux:2017-latest   "/opt/mssql/bin/sqls…"   13 seconds ago      Up 11 seconds       0.0.0.0:1433->1433/tcp   sqltest
16.檢查container "sqltest"的ip
command:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' sqltest

[root@localhost dockervol2]# docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' sqltest
192.168.220.8
[root@localhost dockervol2]#




 

沒有留言:

張貼留言