Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always uses socketPath unless it is set to null #114

Open
Hamzali opened this issue Dec 13, 2019 · 1 comment
Open

Always uses socketPath unless it is set to null #114

Hamzali opened this issue Dec 13, 2019 · 1 comment

Comments

@Hamzali
Copy link

Hamzali commented Dec 13, 2019

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
Copy link

zxlin commented Dec 27, 2019

@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:

var defaultOpts = function() {
var host;
var opts = {};
if (!process.env.DOCKER_HOST) {
// Windows socket path: //./pipe/docker_engine ( Windows 10 )
// Linux & Darwin socket path: /var/run/docker.sock
opts.socketPath = isWin ? '//./pipe/docker_engine' : '/var/run/docker.sock';
} else if (process.env.DOCKER_HOST.indexOf('unix://') === 0) {
// Strip off unix://, fall back to default of /var/run/docker.sock if
// unix:// was passed without a path
opts.socketPath = process.env.DOCKER_HOST.substring(7) || '/var/run/docker.sock';
} else if (process.env.DOCKER_HOST.indexOf('npipe://') === 0) {
// Strip off npipe://, fall back to default of //./pipe/docker_engine if
// npipe:// was passed without a path
opts.socketPath = process.env.DOCKER_HOST.substring(8) || '//./pipe/docker_engine';
} else {
var hostStr = process.env.DOCKER_HOST;
if(hostStr.indexOf('\/\/') < 0) {
hostStr = 'tcp://' + hostStr;
}
try {
host = new url.URL(hostStr);
} catch(err) {
throw new Error('DOCKER_HOST env variable should be something like tcp://localhost:1234');
}
opts.port = host.port;
if (process.env.DOCKER_TLS_VERIFY === '1' || opts.port === '2376') {
opts.protocol = 'https';
} else if (host.protocol === 'ssh:') {
opts.protocol = 'ssh';
opts.username = host.username;
opts.sshAuthAgent = process.env.SSH_AUTH_SOCK;
} else {
opts.protocol = 'http';
}
opts.host = host.hostname;
if (process.env.DOCKER_CERT_PATH) {
opts.ca = splitca(path.join(process.env.DOCKER_CERT_PATH, 'ca.pem'));
opts.cert = fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'cert.pem'));
opts.key = fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'key.pem'));
}
if (process.env.DOCKER_CLIENT_TIMEOUT) {
opts.timeout = parseInt(process.env.DOCKER_CLIENT_TIMEOUT, 10);
}
}
return opts;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants