我认为你至少有两个选择:
选择1:通过Unix域套接字连接
如果您可以让Postgres监听Unix域套接字,那么您可以将该套接字传递给绑定挂载的容器。使用 podman run
和命令行参数--volume
或--mount
。
可能像这样:
--mount type=bind,src=/path/to/socket/on/host,target=/path/to/socket/in/container
如果您的系统启用了SELINUX,则需要添加选项Z
--volume /path/to/socket/on/host:/path/to/socket/in/container:Z
选择2:通过TCP套接字连接
我认为您可以在podman run
命令中添加选项
--network slirp4netns:allow_host_loopback=true
并连接到IP地址10.0.2.2
。
从podman run
man页面引用:“允许slirp4netns访问主机回环IP(10.0.2.2,为方便起见添加到/etc/hosts作为host.containers.internal)”。 另请参见slirp4netns.1.md。
(IP地址10.0.2.2
是硬编码在slirp4netns的源代码中的默认值)。
以下是连接到本地主机上运行的Web服务器的容器示例:
esjolund@laptop:~$ curl -sS http://localhost:8000/file.txt
hello
esjolund@laptop:~$ cat /etc/issue
Ubuntu 20.04.2 LTS \n \l
esjolund@laptop:~$ podman --version
podman version 3.0.1
esjolund@laptop:~$ podman run --rm docker.io/library/fedora cur-l -sS http://10.0.2.2:8000/file.txt
curl: (7) Failed to connect to 10.0.2.2 port 8000: Network is unreachable
esjolund@laptop:~$ podman run --rm --network slirp4netns:allow_host_loopback=true docker.io/library/fedora curl -sS http://10.0.2.2:8000/file.txt
hello
esjolund@laptop:~$
10.0.2.2
中的最后一位数字2
来自于这段源代码 https://github.com/rootless-containers/slirp4netns/blob/462be177a5282a7dc76b2308a55b745ef9d50d2d/main.c#L30 - Erik Sjölund