【问题标题】:Docker - Publish localhost HTTPS server from container to hostDocker - 将 localhost HTTPS 服务器从容器发布到主机
【发布时间】:2020-01-31 17:16:27
【问题描述】:

我已经构建了我的 docker 映像以在本地运行 HTTPS Node.js 服务器,并配置了所有必需的 TLS 证书:

...
var port = config.port || 9010, https;
var tlsOptions = {
    pfx: fs.readFileSync('./tls/keystore.p12'),
    passphrase: ******,
    honorCipherOrder: true,
    secureOptions: constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3    
};
try {
    https = require('https').Server(tlsOptions, app);
} catch (e) {
    console.error('Fail to start HTTPS server ' + e);
}
...

我已经使用curl在容器内部和主机中成功测试了它。

我现在将发布容器的 https://localhost:9010 与 docker 主机的 https://localhost:9010 绑定。我使用了以下命令:

docker container run --publish 9010:9010 --detach --name https_server_container https_server:1.0

当我从 docker 的主机(我的本地机器)运行 curl https://localhost:9010时,我收到此错误:

curl: (35) schannel: failed to receive handshake, SSL/TLS connection failed

我曾尝试关注 docker doc Protect the Docker daemon socket 但没有。

在容器中正确发布 https 节点服务器应该遵循哪些步骤?

谢谢

【问题讨论】:

  • 容器内的服务需要监听0.0.0.0(所有接口),否则无法从容器外访问。您是否设置正确,还是仅在容器私有 localhost 上侦听?
  • 嗨,大卫,非常感谢您的评论。它解决了我的问题!我的 Node.js 服务器监听的是 127.0.0.1 而不是 0.0.0.0,我在 0.0.0.0 中进行了更改,现在我可以从 docker 的主机访问它。

标签: node.js docker ssl https tls1.2


【解决方案1】:

我认为 docker 命令有一些错字:

docker container run --publish 9010:9010 --detach --name https_server_container https_server:1.0

你需要在 9010 和 --detach 之间用空格( )

【讨论】:

  • 您好,感谢您的回答,但这是问题中的输入错误。我已经用 9010 和 --detach 之间的空格执行了命令
【解决方案2】:

David Maze 所示,我将在 docker 容器中运行的 Node.js 服务器的服务器主机从 127.0.0.1 更改为 0.0.0.0

...
https.listen(port, '0.0.0.0', function() {
    ...
});

通过这种方式,我现在可以通过 https://localhost:9010/ 从我的 docker 主机访问 docker 容器中的服务器。

【讨论】:

    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2013-07-20
    相关资源
    最近更新 更多