apocas / docker-modem

Docker Remote API network stack driver.
Apache License 2.0
234 stars 112 forks source link

Always uses socketPath unless it is set to null #114

Open Hamzali opened 4 years ago

Hamzali commented 4 years ago

Hello and thanks for terrific package :)

I followed the examples in the readme and did:

const options = {
    protocol:'http',
    host: '127.0.0.1',
    port: 2375
}

But it would still connect to the default unix socket. Finally I figured out that explicitly setting socketPath to null I could get it to respect the options. I.e. this works:

const options = {
    protocol:'http',
    host: '127.0.0.1',
    port: 2375,
    socketPath: null
}

the solution lies under here, it should first check for http then go for socket option; in lib/modem.js

if (this.socketPath) {
    optionsf.socketPath = this.socketPath;
  } else {
    var urlp = url.parse(address);
    optionsf.hostname = urlp.hostname;
    optionsf.port = urlp.port;
    optionsf.path = urlp.path;
  }
zxlin commented 4 years ago

@apocas would it be wise for us to consider exposing this as a utility function for users of the lib to call so that they may explicitly pass this into the constructor rather than it being auto-populated? This can avoid some ambiguity with the options being passed in and how they're manipulated. Or if a null or undefined is passed as the constructor, it'll use the defaults, but an empty object {} should be treated as user wants to supply their own configs and if any critical configs are missing, then we should throw an error from the constructor.

Examples

new Docker(); // uses the defaultOpts function
new Docker({}); // does not use defaultOpts function, will throw an error since neither socketPath nor host/port is defined
new Docker({ host: '10.0.0.123' }); // host is defined, defaultOpts should not be called, but will throw error for missing port parameter

This will result in a major revision since this is technical breaking changes as we're going to be modifying the default behavior. If we can come to a consensus here, I can start a PR with these changes.

I understand that defaultOpts function does a few more things, we'll likely want to move those out of the function to set up the initial states.

Thanks for all the hard work! Let me know what you and other folks think is the best way forward.

Lines in question here for reference: https://github.com/apocas/docker-modem/blob/3e3bb249d8e247af4c9aafba25c015d7488688f6/lib/modem.js#L16-L69