我正在尝试使用两个 podman 容器部署一个 Web 应用程序。一个运行 Gunicorn,另一个运行 Web 服务器作为反向代理。
但是,只有当我在具有 root 权限的主机上运行容器时,容器之间的通信才能成功。有办法解决这个问题吗?
这是一个没有 root 的示例(返回空 IP 地址):
$ podman run -dt --name test --rm -p 8000:8000 python:3.9 python -m http.server
$ podman container inspect test | grep IPAddress
"IPAddress": "",
有了root,可以做:
$ sudo podman run -dt --name test --rm -p 8000:8000 python:3.9 python -m http.server
$ sudo podman container inspect test | grep IPAddress
"IPAddress": "10.88.0.11",
"IPAddress": "10.88.0.11",
$ cat Dockerfile
FROM caddy:2
COPY Caddyfile /etc/caddy/Caddyfile
$ cat Caddyfile
:8001 {
reverse_proxy * 10.88.0.11:8000
}
$ sudo podman build . -t revproxy
$ sudo podman run -dt --rm -p 8001:8001 revproxy
并且代理在端口 8001 上成功工作。
两个容器必须位于同一个 Pod 或同一个网络中。
使用 Pod,可以执行以下操作:
$ podman pod create -n test
$ podman run --rm --pod test python:3 python3 -m http.server
在另一个外壳中:
$ podman run --rm --pod test python:3 curl localhost:8000
无论您运行的是 root 容器还是无 root 容器,您始终可以使用
host.containers.internal
访问主机(以及其他容器)。
假设我们以这种方式运行这些容器:
$ sudo podman run -d --name httpd -p 8080:80 docker.io/httpd
$ podman run -d --name httpd -p 8888:80 docker.io/httpd
如果我们随后使用 ubuntu 映像启动交互式 bash,如下所示:
$ podman run -it ubuntu bash
$ sudo podman -it ubuntu bash
我们可以像这样卷曲到两个 httpd (Apache2) 容器:
$ curl host.containers.internal:8080
$ curl host.containers.internal:8080
神奇之处在于容器
/etc/hosts
文件,其中包含如下条目:
172.17.23.45 host.containers.internal
或者如果您正在运行rootful:
10.88.0.1 host.containers.internal
这两个 ip: 代表您的主机。