apocas / dockerode

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

socket initialization not working in windows. #290

Closed dhavalyours closed 8 years ago

dhavalyours commented 8 years ago

Integrating this library, docker socker command works in mac but not on windows.

var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock'; var stats = fs.statSync(socket);

It gives following error fs.js:992 Uncaught Error: ENOENT: no such file or directory, stat 'C:\var\run\docker.sock'

Seikho commented 8 years ago

You'll most likely need to use TCP to get dockerode working on Windows. Follow [https://msdn.microsoft.com/en-au/virtualization/windowscontainers/docker/configure_docker_daemon](these docs). In particular you'll want to add something like "hosts": ["0.0.0.0:2375", "npipe://"] to your Docker config and connect via new Dockerode({ port: 2375 }) instead.

If you do figure out how to get the socketPath working, I'd be interested to see it!

LongLiveCHIEF commented 8 years ago

@dhavalyours what version of windows are you running, and do you have the Windows subsystem for Linux enabled?

dhavalyours commented 8 years ago

Hi, I m running windows 7. i have installed docker through virtual machine.

Seikho commented 8 years ago

Where are you trying to use dockerode form? Windows or Linux?

You will most likely need to enable TCP and use similar instructions to what I said earlier.

dhavalyours commented 8 years ago

Hi,

i tried initializing through following code i am getting container undefined.

var docker = new Docker({ protocol: 'tcp', host: '0.0.0.0', port: 2376 });

docker.listContainers(function (err, containers) {
    console.log("cp112343   "+err);
    console.log("container is  "+containers);

});
Seikho commented 8 years ago

You'll need to configure docker to accept connections over TCP. On my phone at the moment so I can't dig up a link. You're looking to set something like DOCKER_OPTS="-h tcp://0.0.0.0:2375 -h unix:///var/run/docker.sock" in /etc/default/docker

apocas commented 8 years ago

On Windows, TCP is the way to go regarding this.

VictorLeach96 commented 7 years ago

Using the latest Docker for Windows release I've managed to get it working through a pipe using the socket configuration.

socketPath: '//./pipe/docker_engine'

Had trouble finding this as it was not documented.

Telokis commented 7 years ago

By @apocas :

On Windows, TCP is the way to go regarding this.

How am I supposed to use TCP, then ? I tried setting protocol to 'TCP' but it doesn't seem to exist. Do I have to go inside my VM to change stuff ?

OmgImAlexis commented 7 years ago

@apocas could you add something to check if the OS is Windows or NIX and change the default from /var/run/docker.sock to use TCP since the default shouldn't just work if you're using NIX.

Telokis commented 7 years ago

I managed to make it work on Windows 7 using Docker Toolbox. I post the bit of code used in case someone needs it later :

import Docker from 'dockerode';
import fs from 'fs';

const isWin = process.platform === 'win32';
let docker;

if (isWin) {
    const BASE_PATH = "C:/Users/<username>/.docker/machine/machines/default/";
    docker = new Docker({
        host: '192.168.99.100',
        protocol: 'https',
        ca: fs.readFileSync(BASE_PATH + 'ca.pem'),
        cert: fs.readFileSync(BASE_PATH + 'cert.pem'),
        key: fs.readFileSync(BASE_PATH + 'key.pem'),
        port: 2376
    });
}

The host and port were obtained with the command docker-machine url default which returned tcp://192.168.99.100:2376. I just ignored the tcp:// part and forced the protocol to be 'https'. Finally, I gave the paths to the required certificates and the connection worked as expected.