無法使用 podman 在兩個 rootfull 容器之間進行通信

無法使用 podman 在兩個 rootfull 容器之間進行通信

我有兩個正在運行的 nginx 容器。

一個正在偵聽連接埠 80,另一個正在偵聽 8080。

這是我運行它們的方式:

sudo podman run --rm \
 -t \
 -p 8080:80 \
 --publish-all \
 --name nginx-two \
 -v ./html2/:/usr/share/nginx/html \
 -v ./html2/conf/default.conf:/etc/nginx/conf.d/default.conf \
 -d nginx

第二:

sudo podman run --rm -t -p 80:80 --name nginx -v ./html/:/usr/share/nginx/html -v ./html/conf/conf.d:/etc/nginx/conf.d -d nginx

NGiNX 配置:

location / {
    proxy_pass http://10.88.0.37:8080;
}

我也嘗試過:

location / {
    proxy_pass http://127.0.0.1:8080;
}

此配置由容器使用--name=nginx

這是我得到的錯誤:

2020/01/26 15:33:05 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 10.88.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "localhost"
10.88.0.1 - - [26/Jan/2020:15:33:05 +0000] "GET / HTTP/1.1" 502 157 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"

有沒有辦法讓這些容器互相通訊?

我也嘗試使用--pod.但後來得到了這個錯誤:

Error: cannot set port bindings on an existing container network namespace

答案1

您不應該使用--publish-all,因為 podman-run 的手冊頁表示這會將所有公開的連接埠發佈到主機介面的隨機連接埠。因此,該-p選項就足夠了。

建立兩個容器所在的專用網路可以幫助解決您的問題,然後您可以使用命令--network=network-id的選項來引用它run

使用 pod 時,應在 pod 本身上定義連接埠映射,而不是在該 pod 內的容器上定義:

podman pod create --name mypod -p 80:80

由於連接埠 80 衝突,無法在具有兩個 nginx 實例的單一 pod 中運行。

Red Hat發表了很好的解釋:https://www.redhat.com/sysadmin/container-networking-podman

相關內容