2019年8月20日 星期二

.yml 指定內部網址出錯處理

在我編輯好 .yml 檔案 docker-compose up 後,遇上了一項錯誤訊息。
User specified IP address is supported only when connecting to
networks with user configured subnets
我的 .yml 原始檔案如下:
version: '3.3'

networks:
  web_subnet:
    external: true

services:

##################
# database 工具
##################

  adminer:
    image: adminer
    container_name: adminer
    restart: always
    networks:
      web_subnet:
        ipv4_address: 192.168.80.53
    ports:
      - "18080:8080"
################################################################################
說明處理方法前,先來講解一下 Docker Network 一些概念。
在執行 docker run 指令時,有一個參數是 --net,它可以設定在執行 Docker Container 是要使用哪一種的網路模式,
目前所知道的 網路模式(DRIVER) 
有 none、container、host、bridge、overlay… 等等的模式,
它們是透過使用 Linux 的 libnetwork 所建立出來的網路模式,以下分別說明這些網路模式:

1. none: 在執行 container 時,網路功能是關閉的,所以無法與此 container 連線
2. container: 使用相同的 Network Namespace,
   所以 container1 的 IP 是 172.17.0.2 那 container2 的 IP 也會是 172.17.0.2
3. host: container 的網路設定和實體主機使用相同的網路設定,所以 container 裡面也就可以修改實體機器的網路設定,
   因此使用此模式需要考慮網路安全性上的問題
4. bridge: Docker 預設就是使用此網路模式,這種網路模式就像是 NAT 的網路模式,
   例如實體主機的 IP 是 192.168.1.10 它會對應到 Container 裡面的 172.17.0.2,
   在啟動 Docker 的 service 時會有一個 docker0 的網路卡就是在做此網路的橋接。
5. overlay: container 之間可以在不同的實體機器上做連線,
   例如 Host1 有一個 container1,
   然後 Host2 有一個 container2,container1 就可以使用 overlay 的網路模式和 container2 做網路的連線。
我的解決方法如下:
## 1. 首先檢查 Docker Network
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
9311de66b224        bridge              bridge              local
10f55487f21f        host                host                local
4382b6e4ee03        none                null                local

## ########################################################################## ##

## 2. 當指向網絡時,我們應該使用標誌【外部】告訴撰寫網絡已經創建並且應該被帶到外面( 否則將使用項目前綴動態創建 )

```
networks:
  app_subnet:
    external: true
```

## ########################################################################## ##

## 3. 所以在此之後, docker-compose 會將容器附加到現有的 web_subnet 在此之前必須創建子網:
$ docker network create --gateway 192.168.80.1 --subnet 192.168.80.0/24 web_subnet
f7e2ddd461b19a6021283cc6d198698ed910d297c6f797fec938f41a999bdf2d

## ########################################################################## ##

## 4. 在檢查 Docker Network,確認是否已經成功創建子網 web_subnet
$ docker network ls

```
NETWORK ID          NAME                DRIVER              SCOPE
9311de66b224        bridge              bridge              local
10f55487f21f        host                host                local
4382b6e4ee03        none                null                local
f7e2ddd461b1        web_subnet          bridge              local
```
## ########################################################################## ##

## 5. 詳列子網 web_subnet 信息,加以確認
$ docker network inspect web_subnet

```
[
    {
        "Name": "web_subnet",
        "Id": "f7e2ddd461b19a6021283cc6d198698ed910d297c6f797fec938f41a999bdf2d",
        "Created": "2019-08-20T03:05:43.057798831Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.80.0/24",
                    "Gateway": "192.168.80.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
```

## ########################################################################## ##

## 6. 重新啟動 docker-compose
$ docker-compose up

沒有留言:

張貼留言