問題描述
Docker ‑ 將 localhost HTTPS 服務器從容器發佈到主機 (Docker ‑ Publish localhost HTTPS server from container to host)
我已經構建了我的 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
參考解法
方法 1:
Way I see it there is some typo with docker command :
docker container run ‑‑publish 9010:9010 ‑‑detach ‑‑name https_server_container https_server:1.0
you need to space( ) between 9010 and ‑‑detach
方法 2:
As indicated by David Maze I changed the server host of my Node.js server runned in the docker container from 127.0.0.1
to 0.0.0.0
.
...
https.listen(port, '0.0.0.0', function() {
...
});
In this manner I can now reach from my docker host the server in the docker container via https://localhost:9010/.
(by Luca Motta、Stark Jeon、Luca Motta)