dsmt 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/SECURITY.md +1 -1
- package/dist/cli.js +3 -3
- package/dist/cmd/export.js +43 -20
- package/dist/cmd/import.js +45 -20
- package/dist/lib/connection/checks.js +23 -0
- package/dist/lib/connection/client.js +48 -0
- package/dist/lib/connection/linux.js +23 -0
- package/dist/lib/connection/macos.js +27 -0
- package/dist/lib/connection/npipe.js +19 -0
- package/dist/lib/connection/optimal.js +55 -0
- package/dist/lib/connection/parser.js +26 -0
- package/dist/lib/connection/test.js +77 -0
- package/dist/lib/connection/windows.js +66 -0
- package/dist/lib/docker/container.js +10 -22
- package/dist/lib/docker/image.js +3 -7
- package/dist/lib/docker/sdk.js +26 -8
- package/dist/lib/docker/volume.js +44 -0
- package/dist/lib/etc/utils.js +10 -0
- package/dist/types/container.js +1 -0
- package/dist/types/volume.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 🐳 Docker Storage Migration Tool 📦
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`dsmt` is a command-line utility for seamlessly exporting and importing Docker volumes and bind mounts.
|
|
4
4
|
|
|
5
5
|
## 🔍 Overview
|
|
6
6
|
|
|
@@ -52,7 +52,7 @@ dsmt import /path/to/tarball.tar.gz /path/to/bind/mount
|
|
|
52
52
|
Both commands support the following options:
|
|
53
53
|
|
|
54
54
|
- `-v, --volume`: Explicitly specify source/destination as a Docker volume
|
|
55
|
-
- `-
|
|
55
|
+
- `-b, --bind`: Explicitly specify source/destination as a bind mount
|
|
56
56
|
|
|
57
57
|
The tool will automatically detect the source/destination type in most cases, but you can use these flags to be explicit.
|
|
58
58
|
|
package/SECURITY.md
CHANGED
package/dist/cli.js
CHANGED
|
@@ -6,13 +6,13 @@ const program = new Command();
|
|
|
6
6
|
program
|
|
7
7
|
.name("dsmt")
|
|
8
8
|
.description("Docker Storage Migration Tool")
|
|
9
|
-
.version("0.
|
|
9
|
+
.version("0.2.0");
|
|
10
10
|
program
|
|
11
11
|
.command("export")
|
|
12
12
|
.argument("<src>", "volume name or bind mount path")
|
|
13
13
|
.argument("<dst>", "path to export to")
|
|
14
14
|
.option("-v, --volume", "Volume")
|
|
15
|
-
.option("-
|
|
15
|
+
.option("-b, --bind", "Bind Mount")
|
|
16
16
|
.description("Export a Docker Volume or Bind Mount to a Tarball")
|
|
17
17
|
.action(async (src, dst, options) => await exportCmd(src, dst, options));
|
|
18
18
|
program
|
|
@@ -20,7 +20,7 @@ program
|
|
|
20
20
|
.argument("<src>", "path to import from")
|
|
21
21
|
.argument("<dst>", "volume name or bind mount path")
|
|
22
22
|
.option("-v, --volume", "Volume")
|
|
23
|
-
.option("-
|
|
23
|
+
.option("-b, --bind", "Bind Mount")
|
|
24
24
|
.description("Import a Docker Volume or Bind Mount from a Tarball")
|
|
25
25
|
.action(async (src, dst, options) => await importCmd(src, dst, options));
|
|
26
26
|
program.parse(process.argv);
|
package/dist/cmd/export.js
CHANGED
|
@@ -3,14 +3,15 @@ import chalk from "chalk";
|
|
|
3
3
|
import ora from "ora";
|
|
4
4
|
import path from "path";
|
|
5
5
|
import { ensureAbsolutePath } from "../lib/etc/utils.js";
|
|
6
|
+
const image = "busybox:latest";
|
|
6
7
|
export default async function exportCmd(src, dst, options) {
|
|
7
|
-
if (options.volume && options.
|
|
8
|
-
console.error(chalk.red("Cannot use both --volume and --
|
|
8
|
+
if (options.volume && options.bind) {
|
|
9
|
+
console.error(chalk.red("Cannot use both --volume and --bind options at the same time."));
|
|
9
10
|
process.exit(1);
|
|
10
11
|
}
|
|
11
|
-
if (!options.volume && !options.
|
|
12
|
-
if (src.
|
|
13
|
-
options.
|
|
12
|
+
if (!options.volume && !options.bind) {
|
|
13
|
+
if (src.includes("/") || src.includes("\\")) {
|
|
14
|
+
options.bind = true;
|
|
14
15
|
console.log(chalk.blue(`Auto-detected bind mount path: ${src}`));
|
|
15
16
|
}
|
|
16
17
|
else {
|
|
@@ -19,13 +20,13 @@ export default async function exportCmd(src, dst, options) {
|
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
if (options.volume) {
|
|
22
|
-
await exportVolume(src, dst
|
|
23
|
+
await exportVolume(src, dst);
|
|
23
24
|
}
|
|
24
|
-
else if (options.
|
|
25
|
-
await
|
|
25
|
+
else if (options.bind) {
|
|
26
|
+
await exportBind(src, dst);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
|
-
async function
|
|
29
|
+
async function exportBind(src, dst) {
|
|
29
30
|
const spinner = ora("Preparing to export Docker bind mount...").start();
|
|
30
31
|
try {
|
|
31
32
|
const srcPath = ensureAbsolutePath(src);
|
|
@@ -33,11 +34,22 @@ async function exportMount(src, dst, options) {
|
|
|
33
34
|
const name = path.basename(srcPath);
|
|
34
35
|
spinner.text = `Exporting bind mount from ${chalk.blue(srcPath)} to ${chalk.green(dst)}`;
|
|
35
36
|
await docker.run({
|
|
36
|
-
name: name
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
name: `dsmt-export-${name}`,
|
|
38
|
+
rm: true,
|
|
39
|
+
mounts: [
|
|
40
|
+
{
|
|
41
|
+
Type: "bind",
|
|
42
|
+
Source: srcPath,
|
|
43
|
+
Target: "/src",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
Type: "bind",
|
|
47
|
+
Source: dstPath,
|
|
48
|
+
Target: "/dst",
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
image: image,
|
|
52
|
+
cmdArgs: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
|
|
41
53
|
});
|
|
42
54
|
spinner.succeed(`Successfully exported bind mount to ${chalk.green(dstPath)}`);
|
|
43
55
|
}
|
|
@@ -46,7 +58,7 @@ async function exportMount(src, dst, options) {
|
|
|
46
58
|
process.exit(1);
|
|
47
59
|
}
|
|
48
60
|
}
|
|
49
|
-
async function exportVolume(src, dst
|
|
61
|
+
async function exportVolume(src, dst) {
|
|
50
62
|
const spinner = ora("Preparing to export Docker volume...").start();
|
|
51
63
|
try {
|
|
52
64
|
const volumeName = src;
|
|
@@ -54,11 +66,22 @@ async function exportVolume(src, dst, options) {
|
|
|
54
66
|
const name = path.basename(src);
|
|
55
67
|
spinner.text = `Exporting volume ${chalk.blue(src)} to ${chalk.green(dstPath)}`;
|
|
56
68
|
await docker.run({
|
|
57
|
-
name: name
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
name: `dsmt-export-${name}`,
|
|
70
|
+
rm: true,
|
|
71
|
+
mounts: [
|
|
72
|
+
{
|
|
73
|
+
Type: "volume",
|
|
74
|
+
Source: volumeName,
|
|
75
|
+
Target: "/src",
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
Type: "bind",
|
|
79
|
+
Source: dstPath,
|
|
80
|
+
Target: "/dst",
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
image,
|
|
84
|
+
cmdArgs: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
|
|
62
85
|
});
|
|
63
86
|
spinner.succeed(`Successfully exported volume to ${chalk.green(dstPath)}`);
|
|
64
87
|
}
|
package/dist/cmd/import.js
CHANGED
|
@@ -4,14 +4,15 @@ import ora from "ora";
|
|
|
4
4
|
import path from "path";
|
|
5
5
|
import fs from "fs";
|
|
6
6
|
import { ensureAbsolutePath } from "../lib/etc/utils.js";
|
|
7
|
+
const image = "busybox:latest";
|
|
7
8
|
export default async function importCmd(src, dst, options) {
|
|
8
|
-
if (options.volume && options.
|
|
9
|
-
console.error(chalk.red("Cannot use both --volume and --
|
|
9
|
+
if (options.volume && options.bind) {
|
|
10
|
+
console.error(chalk.red("Cannot use both --volume and --bind options at the same time."));
|
|
10
11
|
process.exit(1);
|
|
11
12
|
}
|
|
12
|
-
if (!options.volume && !options.
|
|
13
|
-
if (dst.
|
|
14
|
-
options.
|
|
13
|
+
if (!options.volume && !options.bind) {
|
|
14
|
+
if (dst.includes("/") || dst.includes("\\")) {
|
|
15
|
+
options.bind = true;
|
|
15
16
|
console.log(chalk.blue(`Auto-detected bind mount path: ${dst}`));
|
|
16
17
|
}
|
|
17
18
|
else {
|
|
@@ -20,13 +21,13 @@ export default async function importCmd(src, dst, options) {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
if (options.volume) {
|
|
23
|
-
await importVolume(src, dst
|
|
24
|
+
await importVolume(src, dst);
|
|
24
25
|
}
|
|
25
|
-
else if (options.
|
|
26
|
-
await
|
|
26
|
+
else if (options.bind) {
|
|
27
|
+
await importBind(src, dst);
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
|
-
async function
|
|
30
|
+
async function importBind(src, dst) {
|
|
30
31
|
const spinner = ora("Preparing to import to Docker bind mount...").start();
|
|
31
32
|
try {
|
|
32
33
|
const srcPath = ensureAbsolutePath(src);
|
|
@@ -35,13 +36,25 @@ async function importMount(src, dst, options) {
|
|
|
35
36
|
throw new Error(`Source file ${srcPath} does not exist`);
|
|
36
37
|
}
|
|
37
38
|
const name = path.basename(srcPath);
|
|
39
|
+
const srcDir = path.dirname(srcPath);
|
|
38
40
|
spinner.text = `Importing from ${chalk.blue(srcPath)} to bind mount ${chalk.green(dstPath)}`;
|
|
39
41
|
await docker.run({
|
|
40
|
-
name: name
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
name: `dsmt-import-${name}`,
|
|
43
|
+
rm: true,
|
|
44
|
+
mounts: [
|
|
45
|
+
{
|
|
46
|
+
Type: "bind",
|
|
47
|
+
Source: srcDir,
|
|
48
|
+
Target: "/src",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
Type: "bind",
|
|
52
|
+
Source: dstPath,
|
|
53
|
+
Target: "/dst",
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
image: image,
|
|
57
|
+
cmdArgs: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
|
|
45
58
|
});
|
|
46
59
|
spinner.succeed(`Successfully imported to bind mount at ${chalk.green(dstPath)}`);
|
|
47
60
|
}
|
|
@@ -50,7 +63,7 @@ async function importMount(src, dst, options) {
|
|
|
50
63
|
process.exit(1);
|
|
51
64
|
}
|
|
52
65
|
}
|
|
53
|
-
async function importVolume(src, dst
|
|
66
|
+
async function importVolume(src, dst) {
|
|
54
67
|
const spinner = ora("Preparing to import to Docker volume...").start();
|
|
55
68
|
try {
|
|
56
69
|
const srcPath = ensureAbsolutePath(src);
|
|
@@ -59,13 +72,25 @@ async function importVolume(src, dst, options) {
|
|
|
59
72
|
throw new Error(`Source file ${srcPath} does not exist`);
|
|
60
73
|
}
|
|
61
74
|
const name = path.basename(srcPath);
|
|
75
|
+
const srcDir = path.dirname(srcPath);
|
|
62
76
|
spinner.text = `Importing from ${chalk.blue(srcPath)} to volume ${chalk.green(volumeName)}`;
|
|
63
77
|
await docker.run({
|
|
64
|
-
name: name
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
name: `dsmt-import-${name}`,
|
|
79
|
+
rm: true,
|
|
80
|
+
mounts: [
|
|
81
|
+
{
|
|
82
|
+
Type: "bind",
|
|
83
|
+
Source: srcDir,
|
|
84
|
+
Target: "/src",
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
Type: "volume",
|
|
88
|
+
Source: volumeName,
|
|
89
|
+
Target: "/dst",
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
image: image,
|
|
93
|
+
cmdArgs: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
|
|
69
94
|
});
|
|
70
95
|
spinner.succeed(`Successfully imported to volume ${chalk.green(volumeName)}`);
|
|
71
96
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import os from "os";
|
|
3
|
+
export function isSocketAccessible(socketPath) {
|
|
4
|
+
try {
|
|
5
|
+
const stats = fs.statSync(socketPath);
|
|
6
|
+
return stats.isSocket();
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function isNamedPipeAccessible(pipePath) {
|
|
13
|
+
if (os.platform() !== "win32") {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const stats = fs.statSync(pipePath);
|
|
18
|
+
return stats.isFIFO();
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
import { createNamedPipeAgent } from "./npipe.js";
|
|
5
|
+
import { getBestDockerConnection } from "./optimal.js";
|
|
6
|
+
export async function dockerClient(endpoint, options = {}) {
|
|
7
|
+
const dockerConfig = await initializeDockerConnection();
|
|
8
|
+
const config = createDockerAxiosConfig(dockerConfig, endpoint, options);
|
|
9
|
+
return axios(config);
|
|
10
|
+
}
|
|
11
|
+
export async function initializeDockerConnection() {
|
|
12
|
+
try {
|
|
13
|
+
const dockerConfig = await getBestDockerConnection();
|
|
14
|
+
return dockerConfig;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
console.error(chalk.red("Failed to connect to Docker:"), error.message);
|
|
18
|
+
console.error(chalk.yellow("Make sure Docker is running and accessible."));
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function createDockerAxiosConfig(dockerConfig, endpoint, options = {}) {
|
|
23
|
+
// const dockerConfig = getDockerConfig();
|
|
24
|
+
const config = {
|
|
25
|
+
...options,
|
|
26
|
+
url: `http://localhost${endpoint}`,
|
|
27
|
+
};
|
|
28
|
+
if (dockerConfig.socketPath) {
|
|
29
|
+
// Handle Windows named pipes differently
|
|
30
|
+
if (os.platform() === "win32" && dockerConfig.socketPath.includes("pipe")) {
|
|
31
|
+
// For Windows named pipes, we need to use a custom HTTP agent
|
|
32
|
+
config.httpAgent = createNamedPipeAgent(dockerConfig.socketPath);
|
|
33
|
+
config.baseURL = "http://localhost";
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Unix socket
|
|
37
|
+
config.socketPath = dockerConfig.socketPath;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (dockerConfig.host && dockerConfig.port) {
|
|
41
|
+
// TCP connection
|
|
42
|
+
config.baseURL = `http://${dockerConfig.host}:${dockerConfig.port}`;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
throw new Error("Invalid Docker connection configuration");
|
|
46
|
+
}
|
|
47
|
+
return config;
|
|
48
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { parseDockerHost } from "./parser.js";
|
|
4
|
+
import { isSocketAccessible } from "./checks.js";
|
|
5
|
+
export function detectLinuxDockerConnection() {
|
|
6
|
+
const socketPaths = [
|
|
7
|
+
"/var/run/docker.sock", // Standard Docker socket
|
|
8
|
+
path.join(os.homedir(), ".docker", "desktop", "docker.sock"), // Rootless Docker
|
|
9
|
+
// Podman
|
|
10
|
+
`/run/user/${process.getuid?.() || 1000}/podman/podman.sock`,
|
|
11
|
+
path.join(os.homedir(), ".local", "share", "containers", "podman", "machine", "podman.sock"),
|
|
12
|
+
];
|
|
13
|
+
for (const socketPath of socketPaths) {
|
|
14
|
+
if (isSocketAccessible(socketPath)) {
|
|
15
|
+
return { socketPath };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const dockerHost = process.env.DOCKER_HOST;
|
|
19
|
+
if (dockerHost) {
|
|
20
|
+
return parseDockerHost(dockerHost);
|
|
21
|
+
}
|
|
22
|
+
return { socketPath: "/var/run/docker.sock" };
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { parseDockerHost } from "./parser.js";
|
|
4
|
+
import { isSocketAccessible } from "./checks.js";
|
|
5
|
+
export function detectMacDockerConnection() {
|
|
6
|
+
const socketPaths = [
|
|
7
|
+
// Docker Desktop for Mac
|
|
8
|
+
path.join(os.homedir(), ".docker", "desktop", "docker.sock"),
|
|
9
|
+
path.join(os.homedir(), ".docker", "run", "docker.sock"),
|
|
10
|
+
// Standard Unix socket
|
|
11
|
+
"/var/run/docker.sock",
|
|
12
|
+
// Colima
|
|
13
|
+
path.join(os.homedir(), ".colima", "default", "docker.sock"),
|
|
14
|
+
// Podman Desktop
|
|
15
|
+
path.join(os.homedir(), ".local", "share", "containers", "podman", "machine", "podman.sock"),
|
|
16
|
+
];
|
|
17
|
+
for (const socketPath of socketPaths) {
|
|
18
|
+
if (isSocketAccessible(socketPath)) {
|
|
19
|
+
return { socketPath };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const dockerHost = process.env.DOCKER_HOST;
|
|
23
|
+
if (dockerHost) {
|
|
24
|
+
return parseDockerHost(dockerHost);
|
|
25
|
+
}
|
|
26
|
+
return { socketPath: "/var/run/docker.sock" };
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import net from "net";
|
|
2
|
+
import http from "http";
|
|
3
|
+
function createNamedPipeAgent(pipePath) {
|
|
4
|
+
const agent = new http.Agent();
|
|
5
|
+
agent.createConnection = function (options, callback) {
|
|
6
|
+
const socket = net.createConnection(pipePath);
|
|
7
|
+
socket.on("connect", () => {
|
|
8
|
+
if (callback)
|
|
9
|
+
callback(null, socket);
|
|
10
|
+
});
|
|
11
|
+
socket.on("error", (err) => {
|
|
12
|
+
if (callback)
|
|
13
|
+
callback(err);
|
|
14
|
+
});
|
|
15
|
+
return socket;
|
|
16
|
+
};
|
|
17
|
+
return agent;
|
|
18
|
+
}
|
|
19
|
+
export { createNamedPipeAgent };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { detectWindowsDockerConnection } from "./windows.js";
|
|
4
|
+
import { detectMacDockerConnection } from "./macos.js";
|
|
5
|
+
import { detectLinuxDockerConnection } from "./linux.js";
|
|
6
|
+
import { testDockerConnection } from "./test.js";
|
|
7
|
+
export function detectDockerConnection() {
|
|
8
|
+
const platform = os.platform();
|
|
9
|
+
switch (platform) {
|
|
10
|
+
case "win32":
|
|
11
|
+
return detectWindowsDockerConnection();
|
|
12
|
+
case "darwin":
|
|
13
|
+
return detectMacDockerConnection();
|
|
14
|
+
case "linux":
|
|
15
|
+
return detectLinuxDockerConnection();
|
|
16
|
+
default:
|
|
17
|
+
return detectLinuxDockerConnection();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export async function getBestDockerConnection() {
|
|
21
|
+
const primaryConfig = detectDockerConnection();
|
|
22
|
+
if (await testDockerConnection(primaryConfig)) {
|
|
23
|
+
return primaryConfig;
|
|
24
|
+
}
|
|
25
|
+
const fallbackConfigs = [];
|
|
26
|
+
// Platform-specific fallbacks
|
|
27
|
+
if (os.platform() === "win32") {
|
|
28
|
+
// Windows Docker Desktop named pipes
|
|
29
|
+
fallbackConfigs.push({ socketPath: "\\\\.\\pipe\\dockerDesktopLinuxEngine" }, { socketPath: "\\\\.\\pipe\\docker_engine" }, { socketPath: "\\\\.\\pipe\\dockerWindowsEngine" }, { host: "localhost", port: 2375 }, { host: "localhost", port: 2376 });
|
|
30
|
+
}
|
|
31
|
+
else if (os.platform() === "darwin") {
|
|
32
|
+
// macOS specific paths
|
|
33
|
+
fallbackConfigs.push({
|
|
34
|
+
socketPath: path.join(os.homedir(), ".docker", "desktop", "docker.sock"),
|
|
35
|
+
}, { socketPath: path.join(os.homedir(), ".docker", "run", "docker.sock") }, {
|
|
36
|
+
socketPath: path.join(os.homedir(), ".colima", "default", "docker.sock"),
|
|
37
|
+
}, { socketPath: "/var/run/docker.sock" }, { host: "localhost", port: 2375 }, { host: "localhost", port: 2376 });
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// Linux and other Unix-like systems
|
|
41
|
+
fallbackConfigs.push({ socketPath: "/var/run/docker.sock" }, {
|
|
42
|
+
socketPath: path.join(os.homedir(), ".docker", "desktop", "docker.sock"),
|
|
43
|
+
}, {
|
|
44
|
+
socketPath: `/run/user/${process.getuid?.() || 1000}/podman/podman.sock`,
|
|
45
|
+
}, { host: "localhost", port: 2375 }, { host: "localhost", port: 2376 });
|
|
46
|
+
}
|
|
47
|
+
for (const config of fallbackConfigs) {
|
|
48
|
+
if (await testDockerConnection(config)) {
|
|
49
|
+
return config;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
throw new Error(`Failed to connect to Docker. Tried primary config: ${JSON.stringify(primaryConfig)} ` +
|
|
53
|
+
`and ${fallbackConfigs.length} fallback configurations. ` +
|
|
54
|
+
`Make sure Docker is running and accessible.`);
|
|
55
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function parseDockerHost(dockerHost) {
|
|
2
|
+
try {
|
|
3
|
+
if (dockerHost.startsWith("npipe:")) {
|
|
4
|
+
const pipePath = dockerHost.replace("npipe:////./pipe/", "\\\\.\\pipe\\");
|
|
5
|
+
return { socketPath: pipePath };
|
|
6
|
+
}
|
|
7
|
+
const url = new URL(dockerHost);
|
|
8
|
+
switch (url.protocol) {
|
|
9
|
+
case "unix:":
|
|
10
|
+
return { socketPath: url.pathname };
|
|
11
|
+
case "tcp:":
|
|
12
|
+
return {
|
|
13
|
+
host: url.hostname || "localhost",
|
|
14
|
+
port: parseInt(url.port) || 2376,
|
|
15
|
+
};
|
|
16
|
+
case "npipe:":
|
|
17
|
+
return { socketPath: url.pathname };
|
|
18
|
+
default:
|
|
19
|
+
throw new Error(`Unsupported protocol: ${url.protocol}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.warn(`Failed to parse DOCKER_HOST "${dockerHost}":`, error);
|
|
24
|
+
return { socketPath: "/var/run/docker.sock" };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import { createNamedPipeAgent } from "./npipe.js";
|
|
4
|
+
export async function testDockerConnection(config) {
|
|
5
|
+
try {
|
|
6
|
+
if (config.socketPath) {
|
|
7
|
+
// Handle Windows named pipes specially
|
|
8
|
+
if (os.platform() === "win32" && config.socketPath.includes("pipe")) {
|
|
9
|
+
return await testWindowsNamedPipe(config.socketPath);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
// Unix socket connection
|
|
13
|
+
const axios = (await import("axios")).default;
|
|
14
|
+
const axiosConfig = {
|
|
15
|
+
method: "GET",
|
|
16
|
+
url: "http://localhost/_ping",
|
|
17
|
+
socketPath: config.socketPath,
|
|
18
|
+
timeout: 5000,
|
|
19
|
+
};
|
|
20
|
+
const response = await axios(axiosConfig);
|
|
21
|
+
return response.status === 200 && response.data === "OK";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else if (config.host && config.port) {
|
|
25
|
+
// TCP connection
|
|
26
|
+
const axios = (await import("axios")).default;
|
|
27
|
+
const axiosConfig = {
|
|
28
|
+
method: "GET",
|
|
29
|
+
baseURL: `http://${config.host}:${config.port}`,
|
|
30
|
+
url: "/_ping",
|
|
31
|
+
timeout: 5000,
|
|
32
|
+
};
|
|
33
|
+
const response = await axios(axiosConfig);
|
|
34
|
+
return response.status === 200 && response.data === "OK";
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export async function testWindowsNamedPipe(pipePath) {
|
|
45
|
+
if (os.platform() !== "win32") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
const agent = createNamedPipeAgent(pipePath);
|
|
50
|
+
const options = {
|
|
51
|
+
agent: agent,
|
|
52
|
+
hostname: "localhost",
|
|
53
|
+
port: 80,
|
|
54
|
+
path: "/_ping",
|
|
55
|
+
method: "GET",
|
|
56
|
+
timeout: 5000,
|
|
57
|
+
};
|
|
58
|
+
const req = http.request(options, (res) => {
|
|
59
|
+
let data = "";
|
|
60
|
+
res.on("data", (chunk) => {
|
|
61
|
+
data += chunk;
|
|
62
|
+
});
|
|
63
|
+
res.on("end", () => {
|
|
64
|
+
resolve(res.statusCode === 200 && data.trim() === "OK");
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
req.on("error", () => {
|
|
68
|
+
resolve(false);
|
|
69
|
+
});
|
|
70
|
+
req.on("timeout", () => {
|
|
71
|
+
req.destroy();
|
|
72
|
+
resolve(false);
|
|
73
|
+
});
|
|
74
|
+
req.setTimeout(5000);
|
|
75
|
+
req.end();
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { execSync } from "child_process";
|
|
5
|
+
import { parseDockerHost } from "./parser.js";
|
|
6
|
+
import { isNamedPipeAccessible } from "./checks.js";
|
|
7
|
+
export function detectWindowsDockerConnection() {
|
|
8
|
+
// Check if DOCKER_HOST environment variable is set to a named pipe
|
|
9
|
+
const dockerHost = process.env.DOCKER_HOST;
|
|
10
|
+
if (dockerHost && dockerHost.startsWith("npipe:")) {
|
|
11
|
+
return parseDockerHost(dockerHost);
|
|
12
|
+
}
|
|
13
|
+
// Check if Docker context is set to a named pipe
|
|
14
|
+
try {
|
|
15
|
+
const contextName = execSync("docker context show", {
|
|
16
|
+
encoding: "utf8",
|
|
17
|
+
}).trim();
|
|
18
|
+
const contextInfo = JSON.parse(execSync(`docker context inspect ${contextName}`, { encoding: "utf8" }));
|
|
19
|
+
if (contextInfo &&
|
|
20
|
+
contextInfo[0] &&
|
|
21
|
+
contextInfo[0].Endpoints &&
|
|
22
|
+
contextInfo[0].Endpoints.docker) {
|
|
23
|
+
const dockerEndpoint = contextInfo[0].Endpoints.docker.Host;
|
|
24
|
+
if (dockerEndpoint && dockerEndpoint.startsWith("npipe:")) {
|
|
25
|
+
return parseDockerHost(dockerEndpoint);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.debug("Could not detect Docker context:", error);
|
|
31
|
+
}
|
|
32
|
+
// Common Docker Desktop named pipe paths
|
|
33
|
+
const namedPipePaths = [
|
|
34
|
+
"\\\\.\\pipe\\dockerDesktopLinuxEngine", // Docker Desktop Linux containers
|
|
35
|
+
"\\\\.\\pipe\\docker_engine", // Default Docker engine
|
|
36
|
+
"\\\\.\\pipe\\dockerWindowsEngine", // Docker Desktop Windows containers
|
|
37
|
+
];
|
|
38
|
+
// Check if any named pipe exists
|
|
39
|
+
for (const pipePath of namedPipePaths) {
|
|
40
|
+
if (isNamedPipeAccessible(pipePath)) {
|
|
41
|
+
return { socketPath: pipePath };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Check if Docker Desktop is installed
|
|
45
|
+
const dockerDesktopPaths = [
|
|
46
|
+
path.join(os.homedir(), "AppData", "Roaming", "Docker", "settings.json"),
|
|
47
|
+
path.join(os.homedir(), "AppData", "Local", "Docker", "settings.json"),
|
|
48
|
+
];
|
|
49
|
+
const isDockerDesktopInstalled = dockerDesktopPaths.some((p) => {
|
|
50
|
+
try {
|
|
51
|
+
return fs.existsSync(p);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
if (isDockerDesktopInstalled) {
|
|
58
|
+
// Return the most common Docker Desktop pipe
|
|
59
|
+
return { socketPath: "\\\\.\\pipe\\dockerDesktopLinuxEngine" };
|
|
60
|
+
}
|
|
61
|
+
// Fallback to TCP for other Docker implementations on Windows
|
|
62
|
+
return {
|
|
63
|
+
host: "localhost",
|
|
64
|
+
port: 2375,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
export async function containerCreate(
|
|
3
|
-
const binds = [`${src}:/src`, `${dst}:/dst`];
|
|
1
|
+
import { dockerClient } from "../connection/client.js";
|
|
2
|
+
export async function containerCreate(name, { Image, Cmd, Tty = false, HostConfig }) {
|
|
4
3
|
try {
|
|
5
|
-
const createResponse = await
|
|
4
|
+
const createResponse = await dockerClient(`/containers/create${name ? `?name=${name}` : ""}`, {
|
|
6
5
|
method: "POST",
|
|
7
|
-
socketPath: "/var/run/docker.sock",
|
|
8
|
-
url: `http://localhost/containers/create`,
|
|
9
6
|
data: {
|
|
10
|
-
Image
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
HostConfig
|
|
14
|
-
AutoRemove: true,
|
|
15
|
-
Binds: binds,
|
|
16
|
-
},
|
|
7
|
+
Image,
|
|
8
|
+
Cmd,
|
|
9
|
+
Tty,
|
|
10
|
+
HostConfig,
|
|
17
11
|
},
|
|
18
12
|
});
|
|
19
13
|
if (!createResponse.data || !createResponse.data.Id) {
|
|
@@ -28,10 +22,8 @@ export async function containerCreate({ name, src, dst, cmd, image, }) {
|
|
|
28
22
|
}
|
|
29
23
|
export async function containerStart(containerId) {
|
|
30
24
|
try {
|
|
31
|
-
await
|
|
25
|
+
await dockerClient(`/containers/${containerId}/start`, {
|
|
32
26
|
method: "POST",
|
|
33
|
-
socketPath: "/var/run/docker.sock",
|
|
34
|
-
url: `http://localhost/containers/${containerId}/start`,
|
|
35
27
|
});
|
|
36
28
|
}
|
|
37
29
|
catch (error) {
|
|
@@ -42,10 +34,8 @@ export async function containerStart(containerId) {
|
|
|
42
34
|
}
|
|
43
35
|
export async function containerWait(containerId) {
|
|
44
36
|
try {
|
|
45
|
-
const response = await
|
|
37
|
+
const response = await dockerClient(`/containers/${containerId}/wait`, {
|
|
46
38
|
method: "POST",
|
|
47
|
-
socketPath: "/var/run/docker.sock",
|
|
48
|
-
url: `http://localhost/containers/${containerId}/wait`,
|
|
49
39
|
});
|
|
50
40
|
if (response.data.StatusCode !== 0) {
|
|
51
41
|
throw new Error(`Container ${containerId} exited with status code ${response.data.StatusCode}`);
|
|
@@ -59,10 +49,8 @@ export async function containerWait(containerId) {
|
|
|
59
49
|
}
|
|
60
50
|
export async function containerRemove(containerId) {
|
|
61
51
|
try {
|
|
62
|
-
await
|
|
52
|
+
await dockerClient(`/containers/${containerId}`, {
|
|
63
53
|
method: "DELETE",
|
|
64
|
-
socketPath: "/var/run/docker.sock",
|
|
65
|
-
url: `http://localhost/containers/${containerId}`,
|
|
66
54
|
});
|
|
67
55
|
}
|
|
68
56
|
catch (error) {
|
package/dist/lib/docker/image.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { dockerClient } from "../connection/client.js";
|
|
2
2
|
export async function imageList() {
|
|
3
3
|
try {
|
|
4
|
-
const response = await
|
|
4
|
+
const response = await dockerClient("/images/json", {
|
|
5
5
|
method: "GET",
|
|
6
|
-
socketPath: "/var/run/docker.sock",
|
|
7
|
-
url: `http://localhost/images/json`,
|
|
8
6
|
});
|
|
9
7
|
return response.data
|
|
10
8
|
.map((image) => image.RepoTags)
|
|
@@ -18,10 +16,8 @@ export async function imageList() {
|
|
|
18
16
|
}
|
|
19
17
|
export async function imagePull(image) {
|
|
20
18
|
try {
|
|
21
|
-
const response = await
|
|
19
|
+
const response = await dockerClient(`/images/create?fromImage=${image}`, {
|
|
22
20
|
method: "POST",
|
|
23
|
-
socketPath: "/var/run/docker.sock",
|
|
24
|
-
url: `http://localhost/images/create?fromImage=${image}`,
|
|
25
21
|
});
|
|
26
22
|
return response.data;
|
|
27
23
|
}
|
package/dist/lib/docker/sdk.js
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
|
-
const image = "busybox:latest";
|
|
2
1
|
import { containerCreate, containerStart, containerWait, containerRemove, } from "./container.js";
|
|
3
2
|
import { imageList, imagePull } from "./image.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
3
|
+
import { volumeCreate, volumeList } from "./volume.js";
|
|
4
|
+
/**
|
|
5
|
+
* @description Create and run a new container from an image
|
|
6
|
+
* @param options - Options for running the container
|
|
7
|
+
*/
|
|
8
|
+
export async function run(options) {
|
|
9
|
+
const { name, rm, mounts, volumes, tty, image, cmdArgs } = options;
|
|
10
|
+
let hostConfig = {
|
|
11
|
+
AutoRemove: rm,
|
|
12
|
+
Binds: volumes?.map((v) => `${v.Name}:${v.Mountpoint}`),
|
|
13
|
+
Mounts: mounts?.map((m) => ({
|
|
14
|
+
Type: m.Type,
|
|
15
|
+
Source: m.Source,
|
|
16
|
+
Target: m.Target,
|
|
17
|
+
ReadOnly: m.ReadOnly,
|
|
18
|
+
Consistency: m.Consistency,
|
|
19
|
+
})),
|
|
20
|
+
};
|
|
21
|
+
const containerId = await containerCreate(name, {
|
|
22
|
+
HostConfig: hostConfig,
|
|
23
|
+
Tty: tty || false,
|
|
24
|
+
Image: image,
|
|
25
|
+
Cmd: cmdArgs,
|
|
26
|
+
});
|
|
11
27
|
await containerStart(containerId);
|
|
12
28
|
await containerWait(containerId);
|
|
13
29
|
}
|
|
@@ -19,5 +35,7 @@ const docker = {
|
|
|
19
35
|
containerRemove,
|
|
20
36
|
imageList,
|
|
21
37
|
imagePull,
|
|
38
|
+
volumeList,
|
|
39
|
+
volumeCreate,
|
|
22
40
|
};
|
|
23
41
|
export default docker;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { dockerClient } from "../connection/client.js";
|
|
2
|
+
export async function volumeList() {
|
|
3
|
+
try {
|
|
4
|
+
const response = await dockerClient("/volumes", {
|
|
5
|
+
method: "GET",
|
|
6
|
+
});
|
|
7
|
+
const data = response.data;
|
|
8
|
+
const volumes = data.Volumes.map((vol) => {
|
|
9
|
+
return {
|
|
10
|
+
Name: vol.Name,
|
|
11
|
+
Driver: vol.Driver,
|
|
12
|
+
Mountpoint: vol.Mountpoint,
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
return volumes;
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
console.error("Failed to list Docker volumes:", error);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function volumeCreate({ Name, Driver }) {
|
|
23
|
+
if (!Name) {
|
|
24
|
+
throw new Error("Volume name is required for creation.");
|
|
25
|
+
}
|
|
26
|
+
if (!Driver) {
|
|
27
|
+
Driver = "local";
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const response = await dockerClient("/volumes/create", {
|
|
31
|
+
method: "POST",
|
|
32
|
+
data: {
|
|
33
|
+
Name,
|
|
34
|
+
Driver,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
const data = response.data;
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error(`Failed to create Docker volume ${Name}:`, error);
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/lib/etc/utils.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
+
import { volumeList } from "../docker/volume.js";
|
|
2
3
|
export function ensureAbsolutePath(p) {
|
|
3
4
|
return path.isAbsolute(p) ? p : path.resolve(process.cwd(), p);
|
|
4
5
|
}
|
|
6
|
+
export async function volumeExists(name) {
|
|
7
|
+
try {
|
|
8
|
+
const volumes = await volumeList();
|
|
9
|
+
return volumes.map((v) => v.Name).includes(name);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|