Skip to content

Commit 637d901

Browse files
committed
Add support for Windows npipe
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent ba20205 commit 637d901

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

lib/docker-client.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { WritableStream } from 'node:stream/web';
3030
import { ReadableStream } from 'stream/web';
3131
import { jsonMessages } from './json-stream.js';
3232
import { Writable } from 'node:stream';
33+
import * as os from 'node:os';
3334

3435
export class DockerClient {
3536
private api: HTTPClient;
@@ -51,7 +52,7 @@ export class DockerClient {
5152

5253
/**
5354
* Create a DockerClient instance from a Docker host string
54-
* @param dockerHost Docker host string (e.g., "unix:/var/run/docker.sock", "tcp://localhost:2376", or "ssh://user@host[:port][/path/to/docker.sock]")
55+
* @param dockerHost Docker host string (e.g., "unix:/var/run/docker.sock", "tcp://localhost:2376", "ssh://user@host[:port][/path/to/docker.sock]", or "/var/run/docker.sock")
5556
* @param certificates Optional path to directory containing TLS certificates (ca.pem, cert.pem, key.pem) for TCP connections
5657
* @returns Promise that resolves to a connected DockerClient instance
5758
*/
@@ -65,6 +66,21 @@ export class DockerClient {
6566
// Unix socket connection - use SocketAgent with socket creation function
6667
const socketPath = dockerHost.substring(5); // Remove "unix:" prefix
6768

69+
try {
70+
const agent = new SocketAgent(() =>
71+
createConnection(socketPath),
72+
);
73+
return new DockerClient(agent, userAgent, headers);
74+
} catch (error) {
75+
throw new Error(
76+
`Failed to create Docker client for ${dockerHost}: ${getErrorMessage(error)}`,
77+
{ cause: error },
78+
);
79+
}
80+
} else if (dockerHost.startsWith('npipe:')) {
81+
// Windows name pipe connection - use SocketAgent with socket creation function
82+
const socketPath = dockerHost.substring(6); // Remove "npipe:" prefix
83+
6884
try {
6985
const agent = new SocketAgent(() =>
7086
createConnection(socketPath),
@@ -125,8 +141,19 @@ export class DockerClient {
125141
);
126142
}
127143
} else {
144+
try {
145+
await fsPromises.access(dockerHost);
146+
// File exists, treat it as a Unix socket
147+
const agent = new SocketAgent(() =>
148+
createConnection(dockerHost),
149+
);
150+
return new DockerClient(agent, userAgent, headers);
151+
} catch (error) {
152+
// If file doesn't exist, fall through to original error handling
153+
}
154+
128155
throw new Error(
129-
`Unsupported Docker host format: ${dockerHost}. Must start with "unix:", "tcp:", or "ssh:"`,
156+
`Unsupported Docker host format: ${dockerHost}. Must start with "unix:", "tcp:", "ssh:", or be a valid file path`,
130157
);
131158
}
132159
}
@@ -246,17 +273,21 @@ export class DockerClient {
246273
const configContent = await fsPromises.readFile(configPath, 'utf8');
247274
const config = JSON.parse(configContent);
248275

249-
if (config.currentContext) {
276+
if (config.currentContext && config.currentContext !== 'default') {
250277
// Use the specified current context
251278
return await DockerClient.fromDockerContext(
252279
config.currentContext,
253280
userAgent,
254281
headers,
255282
);
256283
} else {
284+
let dockerhost = 'unix:/var/run/docker.sock';
285+
if (os.platform() === 'win32') {
286+
dockerhost = 'npipe:////./pipe/docker_engine';
287+
}
257288
// No current context specified, use default
258289
return await DockerClient.fromDockerHost(
259-
'unix:/var/run/docker.sock',
290+
dockerhost,
260291
undefined,
261292
userAgent,
262293
headers,

0 commit comments

Comments
 (0)