apocas / dockerode

Docker + Node = Dockerode (Node.js module for Docker's Remote API)
Apache License 2.0
4.35k stars 460 forks source link

containerAttach is sending parameter as POST JSON payload while it shouldn't #742

Open benoitf opened 1 year ago

benoitf commented 1 year ago

Hello,

When trying to use dockerode with podman engine, I noticed that I received echo of queries when using attach on a container.

Reading https://docs.docker.com/engine/api/v1.43/#tag/Container/operation/ContainerAttach we can see that parameters like stdin, stdout, stream, etc are all query parameters like /v1.43/containers/{id}/attach?stdin=...&stdout=... but dockerode also send the options as a JSON payload {"stream": true, "stdin":true, "stdout":true}

the API don't mention a payload so for attach API, dockerode shouldn't send any body on the REST API call.

absorbb commented 4 months ago

That can be a real problem when trying to work with images that expect some meaningful input in stdin.

We've implemented workaround using proxy object:

container.modem = new Proxy(container.modem, {
      get(target, prop) {
        const origMethod = target[prop];
        if (prop === "dial") {
            return function (...args) {
              if (args[0].path.endsWith("/attach?")) {
                // make request body empty
                args[0].file = new Buffer("")
              }
              return origMethod.apply(target, args)
            }
        } else {
          return origMethod
        }
      }
    })