从 Podman 运行的无根容器内托管 SSH 隧道访问

从 Podman 运行的无根容器内托管 SSH 隧道访问

背景

系统正在运行一个基于docker.io/rocker/shiny-verse:latest镜像的无根容器,该镜像发布了一个端口(例如 8000)用于远程 Web 连接(以访问 Shiny UI)。shiny容器内运行的服务器需要访问远程主机上运行的数据库。容器运行的本地主机使用 SSH 隧道(容器外)将数据库连接从本地主机包装到远程数据库服务器。重现的基本步骤(较少凭据):

$ # In host environment (logged in as unprivileged account that runs rootless container)...
$ ssh -fvNx -L 5432:localhost:5432 dbuser@dbserver
$ psql -h 127.0.0.1 -p 5432 ... # Able to connect to database from host environment (using non-privileged account that runs the rootless container)
$ Rscript -e ‘x <- pool::dbPool(drv = RPostgreSQL::PostgreSQL(), ....)’ # Command succeeds
$ podman run —rm -it —publish 8000:8000 —expose 5432 —entrypoint ‘[“R”]’ docker.io/rocker/shiny-verse:latest
> # Within container interactive R session...
> x <- pool::dbPool(drv = RPostgreSQL::PostgreSQL(), ....)
> # Error in postgresqlNewConnection(drv, ...):
> #   RS-DBI driver ... could not connect ... Is the server running ... and accepting connections ...

问题

由于我无法(或者我认为我不能)在运行时发布端口 5432 podman run ...(因为该端口已被主机上运行的 SSH 隧道绑定),我该如何运行容器映像,以便我可以使用在容器内建立的主机上的 SSH 隧道访问远程数据库?

其他想法

  • 日志中没有任何内容表明防火墙数据包被拒绝(并且没有 SELinux 拒绝)
  • 希望找到一种不涉及破坏网络数据包的解决方案
  • 虽然使用该—network=host选项可以podman run解决问题,但它似乎过于宽容,无法保留无根容器的一些重要安全优势

环境

podman 1.9.2
Fedora 32 (5.6.14-300.fc32.x96_64)
R 4.0.0
OpenSSH_8.2p1
OpenSSL 1.1.1g

相关内容