dockerode icon indicating copy to clipboard operation
dockerode copied to clipboard

Passing platform to createContainer

Open themightychris opened this issue 2 years ago • 1 comments

I need to set platform: 'linux/amd64' when calling createContainer

The latest ContainerCreate API documents platform as a lowercase query parameter option alongside name

Screen Shot 2023-03-20 at 10 52 32 PM

I found one example in the readme of name: being mixed in with the uppercased API options when calling createContainer but I can't see in the code how that gets pulled out and passed as a query param to the underlying Docker API call like it and platform would need to

themightychris avatar Mar 21 '23 03:03 themightychris

I needed this functionality, and looking into it, when using createContainer, the function call lives at:

https://github.com/apocas/dockerode/blob/bb88194ff250c4f5b9a0eee2688c5dcc5f623ef9/lib/docker.js#L43-L77

where it constructs an object that is then passed to docker-modem that handles the actual communication to the docker process. The relevant code is then:

https://github.com/apocas/docker-modem/blob/e55461b7e821c49661e8410532bb46fc6d7fd212/lib/modem.js#L149-L155

In that code, if the path key in the passed options contains a ?, then it uses either opts._query || opts where for this case, the first doesn't exist, so it just uses the entire opts and appends that as a query string. So for the following example:

await docker.createContainer({
  Image: 'busybox',
  AttachStdin: false,
  AttachStdout: true,
  AttachStderr: true,
  Tty: true,
  Cmd: ['/bin/bash', '-c', 'tail -f /var/log/dmesg'],
  OpenStdin: false,
  StdinOnce: false,
  HostConfig: {
    AutoRemove: true,
  },
  name: 'foo',
  platform: 'linux/amd64',
});

Then the full path address that's called for docker ends up being:

/containers/create?Image=busybox&AttachStdin=false&AttachStdout=true&AttachStderr=true&Tty=true&Cmd=%5B%22%2Fbin%2Fbash%22%2C%22-c%22%2C%22tail%20-f%20%2Fvar%2Flog%2Fdmesg%22%5D&OpenStdin=false&StdinOnce=false&HostConfig=%7B%22AutoRemove%22%3Atrue%7D&name=foo&platform=linux%2Famd64

which we see contains the name and platform fields and are used, with the rest of the stuff being ignored by Docker itself. This does mean that the createContainer endpoint is forward facing to any new additions that get added, at the expense of sending a lot of junk on the path. An optimization might be to only include keys that start with a lowercase character, though it's possible (albeit feels unlikely) that Docker starts using query params that start with uppercase characters and now the API would need to be adjusted.

MasterOdin avatar Apr 24 '23 14:04 MasterOdin