Skip to content

Commit db0cb66

Browse files
authored
fix(server-setup): report the installed Docker version in the setup banner (Dokploy#4723)
1 parent 91abc93 commit db0cb66

2 files changed

Lines changed: 115 additions & 1 deletion

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { execFileSync, execSync } from "node:child_process";
2+
import { chmodSync, mkdtempSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import path from "node:path";
5+
import { defaultCommand, reportDockerVersion } from "@dokploy/server";
6+
import { describe, expect, it } from "vitest";
7+
8+
const resolveBin = (name: string) =>
9+
execSync(`command -v ${name}`, { encoding: "utf8" }).trim();
10+
11+
/**
12+
* Build a sandbox PATH so `command -v docker` only sees our fake docker
13+
* binary (or nothing), regardless of what the host has installed.
14+
*/
15+
const makeSandbox = (dockerShim?: string) => {
16+
const dir = mkdtempSync(path.join(tmpdir(), "dokploy-server-setup-"));
17+
for (const tool of ["awk", "tr"]) {
18+
const shim = path.join(dir, tool);
19+
writeFileSync(shim, `#!/bin/sh\nexec ${resolveBin(tool)} "$@"\n`);
20+
chmodSync(shim, 0o755);
21+
}
22+
if (dockerShim) {
23+
const shim = path.join(dir, "docker");
24+
writeFileSync(shim, dockerShim);
25+
chmodSync(shim, 0o755);
26+
}
27+
return dir;
28+
};
29+
30+
const runReport = (sandboxPath: string) => {
31+
const script = [
32+
"DOCKER_VERSION=28.5.0",
33+
reportDockerVersion(),
34+
'echo "$DOCKER_VERSION_REPORT"',
35+
].join("\n");
36+
return execFileSync(resolveBin("bash"), ["-c", script], {
37+
encoding: "utf8",
38+
env: { ...process.env, PATH: sandboxPath },
39+
})
40+
.trim()
41+
.split("\n")
42+
.pop();
43+
};
44+
45+
describe("reportDockerVersion", () => {
46+
it("reports the engine version when docker and its daemon are available", () => {
47+
const sandbox = makeSandbox(
48+
[
49+
"#!/bin/sh",
50+
'if [ "$1" = "--version" ]; then',
51+
' echo "Docker version 25.0.0, build aaaaaaa"',
52+
" exit 0",
53+
"fi",
54+
'if [ "$1" = "version" ]; then',
55+
' echo "29.4.3"',
56+
" exit 0",
57+
"fi",
58+
"exit 1",
59+
].join("\n"),
60+
);
61+
expect(runReport(sandbox)).toBe("29.4.3 (already installed)");
62+
});
63+
64+
it("falls back to the client version when the daemon is unreachable", () => {
65+
const sandbox = makeSandbox(
66+
[
67+
"#!/bin/sh",
68+
'if [ "$1" = "--version" ]; then',
69+
' echo "Docker version 29.4.3, build 055a478"',
70+
" exit 0",
71+
"fi",
72+
'echo "Cannot connect to the Docker daemon" >&2',
73+
"exit 1",
74+
].join("\n"),
75+
);
76+
expect(runReport(sandbox)).toBe("29.4.3 (already installed)");
77+
});
78+
79+
it("reports the pinned version to be installed when docker is missing", () => {
80+
expect(runReport(makeSandbox())).toBe("28.5.0 (will be installed)");
81+
});
82+
});
83+
84+
describe("defaultCommand", () => {
85+
it.each([false, true])(
86+
"prints the detected Docker version in the setup banner (isBuildServer=%s)",
87+
(isBuildServer) => {
88+
const script = defaultCommand(isBuildServer);
89+
expect(script).toContain(reportDockerVersion());
90+
expect(script).toContain(
91+
'echo "| Docker | $DOCKER_VERSION_REPORT"',
92+
);
93+
expect(script).not.toContain(
94+
'echo "| Docker | $DOCKER_VERSION"',
95+
);
96+
},
97+
);
98+
});

‎packages/server/src/setup/server-setup.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ export const serverSetup = async (
106106
}
107107
};
108108

109+
export const reportDockerVersion = () => `
110+
if command -v docker >/dev/null 2>&1; then
111+
INSTALLED_DOCKER_VERSION=$(docker version --format '{{.Server.Version}}' 2>/dev/null || true)
112+
if [ -z "$INSTALLED_DOCKER_VERSION" ]; then
113+
INSTALLED_DOCKER_VERSION=$(docker --version 2>/dev/null | awk '{print $3}' | tr -d ',' || true)
114+
fi
115+
if [ -z "$INSTALLED_DOCKER_VERSION" ]; then
116+
INSTALLED_DOCKER_VERSION="unknown"
117+
fi
118+
DOCKER_VERSION_REPORT="$INSTALLED_DOCKER_VERSION (already installed)"
119+
else
120+
DOCKER_VERSION_REPORT="$DOCKER_VERSION (will be installed)"
121+
fi
122+
`;
123+
109124
export const defaultCommand = (isBuildServer = false) => {
110125
const bashCommand = `
111126
set -e;
@@ -174,10 +189,11 @@ arch | ubuntu | debian | raspbian | centos | fedora | rhel | ol | rocky | sles |
174189
;;
175190
esac
176191
192+
${reportDockerVersion()}
177193
echo -e "---------------------------------------------"
178194
echo "| CPU Architecture | $SYS_ARCH"
179195
echo "| Operating System | $OS_TYPE $OS_VERSION"
180-
echo "| Docker | $DOCKER_VERSION"
196+
echo "| Docker | $DOCKER_VERSION_REPORT"
181197
${isBuildServer ? 'echo "| Server Type | Build Server"' : ""}
182198
echo -e "---------------------------------------------\n"
183199
echo -e "1. Installing required packages (curl, wget, git, jq, openssl). "

0 commit comments

Comments
 (0)