无法使用来自 Docker 容器的 SSH 隧道连接到远程数据库

Unable to connect to remote Database using SSH tunneling from a Docker Container

我的脚本 运行 在我的本地主机上完全正常,直到我在 Docker 容器中启动 运行 它。我不知道出了什么问题。我得到 INFO:连接到 DB 失败!由于异常:没有密码或 public 密钥可用! 错误

我的 db_config.py 文件:

with SSHTunnelForwarder(
                (<Bastian Host IP>, 22),
                ssh_username=<user>,
                ssh_pkey='oalkey.openssh',
                ssh_private_key_password=<pass>,
                remote_bind_address=(<Remote DB IP>, 1521),
                local_bind_address=('localhost', 1521)
         ) as tunnel:
            dsn_tns = 

cx_Oracle.makedsn(os.getenv("LOCAL_BIND_HOST",'localhost'),os.getenv("SSH_TUNNEL_PORT",1521),
                                        service_name=os.getenv("OCI_DSP_SERVICE","default"))
            self.db_connection = cx_Oracle.connect(os.getenv("OCI_DAS_NAME"),
                                                   os.getenv("OCI_DAS_PASS"), dsn_tns)
            logger.info('Connection to  DB successful')
        #server.stop()
    except Exception as ex:
        logger.info('Connection to DB failed! due to Exception: {}'.format(ex))

哦,我解决了这个问题。这是因为我的 Dockerfile 中的 WORKDIR 指向根位置,我不得不将 ssh_pkey='oalkey.openssh' 中的相对路径写入 ssh_pkey='app/var/www/oalkey.openssh'。简单:-)