You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Engine无法通过WebSocket连接:求助解决方法

问题:无法通过Docker Engine API v1.24的WebSocket接口附着到容器

我想要编写一个后端来自动化运行Docker容器,为此使用Docker Engine API v1.24的WebSocket容器附着接口。但我无法成功连接该WebSocket,于是制作了一个测试HTML文件来测试端点:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<label>
    Send Command
    <input type="text" autofocus>
</label>
<button onclick="sendCommand()">Send</button>
<div id="output"></div>
<script>
    const link = "ws://127.0.0.1:2375/v1.24/containers/sharp_hellman/attach/ws?logs=true&stream=true&stdin=true&stdout=true&stderr=true";
    const ws = new WebSocket(link);
    ws.onmessage = (msg) => {
        document.querySelector('#output').innerHTML += msg
    };
    function sendCommand() {
        ws.send(document.querySelector('input').value)
        document.querySelector('input').value = ''
    }
</script>
</body>
</html>

在Firefox和Edge的网络标签中,我得到了101 Switching Protocols响应,但控制台出现错误,且不同浏览器报错信息不同:

  • Firefox报错:The connection to ws://127.0.0.1:2375/v1.24/containers/sharp_hellman/attach/ws?logs=true&stream=true&stdin=true&stdout=true&stderr=true was interrupted while the page was loading.
  • Edge报错:WebSocket connection to 'ws://127.0.0.1:2375/v1.24/containers/sharp_hellman/attach/ws?logs=true&stream=true&stdin=true&stdout=true&stderr=true' failed: Invalid frame header

我使用Windows系统,已开启“Expose daemon on tcp://localhost:2375 without TLS”选项。执行docker ps命令得到如下结果:

CONTAINER ID   IMAGE                   COMMAND    CREATED      STATUS                    PORTS       NAMES
6743bf6743bf   itzg/minecraft-server   "/start"   2 days ago   Up 39 minutes (healthy)   25565/tcp   sharp_hellman

请问如何解决该问题,以通过API附着到Docker Engine?


内容的提问来源于stack exchange,提问作者CORRIT_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 05:05:23