iT邦幫忙

0

Docker 內沒有網路

  • 分享至 

  • xImage

各位好,我在 digitalocean 租用了一台 ubuntu,有安裝 docker 以及 docker compose,軟體以及系統版本如下:

軟體資訊

  • OS: Ubuntu 20.04.5 LTS
  • Docker: 20.10.22
  • Docker Compose 1.24.0

網卡資訊

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 04:01:3c:c8:a6:01 brd ff:ff:ff:ff:ff:ff
    inet *.*.*.*/24 brd 198.199.116.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 *::*:*:*:*/64 scope link 
       valid_lft forever preferred_lft forever
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:3f:78:bf:69 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:3fff:fe78:bf69/64 scope link 
       valid_lft forever preferred_lft forever

不知道為什麼我的 container 無法取得網路,以至於對外的服務基本上都不能動

docker run --rm busybox ping google.com
ping: bad address 'google.com'

但網路模式用 host 可以

docker run --rm --network=host busybox ping google.com
PING google.com (142.250.189.238): 56 data bytes
64 bytes from 142.250.189.238: seq=0 ttl=60 time=1.853 ms
64 bytes from 142.250.189.238: seq=1 ttl=60 time=1.368 ms
64 bytes from 142.250.189.238: seq=2 ttl=60 time=1.380 ms
64 bytes from 142.250.189.238: seq=3 ttl=60 time=1.381 ms

就算防火牆關閉也一樣(理論上應該也不會是防火牆問題),網路上搜尋了很多方法,像是:

以上全部都沒有效用,感覺是 bridge 上有狀況,使用 host 的話指定 expose port 的功能會不能用,例如我想架一個 mailhog service:

docker-compose.yml

version: "3"
services:
  mailer:
    network_mode: host
    image: mailhog/mailhog
    ports:
      - "1025:1025"
      - "8025:8025"
      Name         Command   State   Ports
------------------------------------------
mailhog_mailer_1   MailHog   Up           

network mode 為 host 的話有網路沒錯但 port 就會不見了,這樣沒什麼意義,不知道有沒有什麼其他線索可以解決這件事,感謝。

看更多先前的討論...收起先前的討論...
Ray iT邦大神 1 級 ‧ 2023-01-09 09:25:53 檢舉
剛裝完 Docker 之後, 有重開機過嗎?
Ray iT邦大神 1 級 ‧ 2023-01-09 09:29:35 檢舉
有試過直接 ping 127.0.01 和 8.8.8.8 這兩個嗎?
chan15 iT邦新手 2 級 ‧ 2023-01-09 09:33:45 檢舉
重啟過、重開過也重裝過都一樣

```
docker run --rm busybox ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.060 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.069 ms
64 bytes from 127.0.0.1: seq=2 ttl=64 time=0.079 ms

docker run --rm busybox ping 8.8.8.8

// pending 住
```
johncoc iT邦新手 3 級 ‧ 2023-01-09 11:24:21 檢舉
這篇參考看看
https://stackoverflow.com/questions/35519615/docker-container-can-only-access-internet-with-net-host
chan15 iT邦新手 2 級 ‧ 2023-01-09 13:29:09 檢舉
上面的都試過了,一樣不行
obarisk iT邦研究生 2 級 ‧ 2023-01-10 08:46:52 檢舉
先看 ipv4.forward 有沒有開
再看 iptables
chan15 iT邦新手 2 級 ‧ 2023-01-10 08:59:26 檢舉
sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

iptables -nL | grep -i "Chain DOCKER" -A 3
Chain DOCKER (2 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 172.18.0.2 tcp dpt:8025
ACCEPT tcp -- 0.0.0.0/0 172.18.0.2 tcp dpt:1025
--
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- 0.0.0.0/0 0.0.0.0/0
DOCKER-ISOLATION-STAGE-2 all -- 0.0.0.0/0 0.0.0.0/0
--
Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
DROP all -- 0.0.0.0/0 0.0.0.0/0
--
Chain DOCKER-USER (1 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 0.0.0.0/0
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
西撒
iT邦新手 5 級 ‧ 2023-01-13 22:20:59

感覺你是這個問題
把後面寫的文字 放到前面了

第二個猜測
你容器內 會不會安裝過於簡單
無法解析 google.com
改成 google 的 ip ?
ping 172.217.160.110

你容器 base 是什麼?
和這篇感覺很像, 所以我不喜歡 用 musl 系列的東西
常常有詭異的 bug, 雖然我只看文章 沒太多經驗
https://stackoverflow.com/questions/48477570/unable-to-resolve-domain-names-inside-docker-container


第一個猜測

我學這門課 老師很強 devops社群的強者
Linux 工程師維運大全|從容器幫你重新打造網路基底

提到 docker 在安裝的時候
有寫規則到 iptable 的 forward chain 中

原本系統預設是 ACCEPT
安裝 docker 被修改為 DROP

所以你簡單執行這個命令試試, 選 ACCEPT 的指令

sudo iptables -t filter -P FORWARD ACCEPT
sudo iptables -t filter -P FORWARD DROP

下面連結 是我用關鍵字 找到的資料
https://serverfault.com/questions/1005648/docker-changes-iptables-forward-policy-to-drop

官方也有 試試看吧
但是使用了別的命令
iptables -I DOCKER-USER -i src_if -o dst_if -j ACCEPT
https://docs.docker.com/network/iptables/#docker-on-a-router

chan15 iT邦新手 2 級 ‧ 2023-01-30 07:38:53 檢舉

感謝您的回答,都試過了還是不行

0
JamesDoge
iT邦高手 1 級 ‧ 2023-02-04 16:24:58

不知道為什麼我的 container 無法取得網路,以至於對外的服務基本上都不能動

HI~container的問題,但沒附上Dockerfile
上一則回達問你容器 base 是什麼?
你回說 不行 然後也沒提供線索
真的只能隔空抓藥 或者 用線幫你把脈(懸絲診脈)
https://ithelp.ithome.com.tw/upload/images/20230204/20152569BHJ6SoSg7v.jpg

所以我猜可以通過在 Dockerfile 中安裝 dnsmasq
或使用自定義網路模式來解決這個問題

Dockerfile:

FROM alpine
RUN apk add dnsmasq
CMD ["dnsmasq", "-k", "-d"]

docker-compose.yml:

version: "3"
services:
  web:
    build: .
    ports:
      - "80:80"
  dns:
    build: .
    command: ["dnsmasq", "-d", "--no-daemon", "--log-queries", "--log-facility=-"]
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      default:
        ipv4_address: 172.28.1.1
  app:
    image: myapp
    networks:
      default:
        ipv4_address: 172.28.1.2
networks:
  default:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.28.0.0/16
          gateway: 172.28.0.1

僅供參考

chan15 iT邦新手 2 級 ‧ 2023-02-04 17:44:46 檢舉
docker run --rm busybox ping google.com

我不是不給,是文內就有光是用 busybox 就打不出去了 XD

我要發表回答

立即登入回答