Skip to content

Commit 8e9ab98

Browse files
authored
Merge pull request #3940 from Dokploy/3806-bug-traefik-and-dokploy-fails-to-start-when-port-8080-is-already-in-use-service-crash
fix: improve port conflict detection by enhancing error messages and …
2 parents ec7df05 + ce82e23 commit 8e9ab98

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

apps/dokploy/server/api/routers/settings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ export const settingsRouter = createTRPCRouter({
149149
// Check if port 8080 is already in use before enabling dashboard
150150
const portCheck = await checkPortInUse(8080, input.serverId);
151151
if (portCheck.isInUse) {
152-
const conflictingContainer = portCheck.conflictingContainer
153-
? ` by container "${portCheck.conflictingContainer}"`
152+
const conflictInfo = portCheck.conflictingContainer
153+
? ` by ${portCheck.conflictingContainer}`
154154
: "";
155155
throw new TRPCError({
156156
code: "CONFLICT",
157-
message: `Port 8080 is already in use${conflictingContainer}. Please stop the conflicting service or use a different port for the Traefik dashboard.`,
157+
message: `Port 8080 is already in use${conflictInfo}. Please stop the conflicting service or use a different port for the Traefik dashboard.`,
158158
});
159159
}
160160
newPorts.push({

packages/server/src/services/settings.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,38 @@ export const checkPortInUse = async (
413413
serverId?: string,
414414
): Promise<{ isInUse: boolean; conflictingContainer?: string }> => {
415415
try {
416-
const command = `docker ps -a --format '{{.Names}}' | grep -v '^dokploy-traefik$' | while read name; do docker port "$name" 2>/dev/null | grep -q ':${port}' && echo "$name" && break; done || true`;
417-
const { stdout } = serverId
418-
? await execAsyncRemote(serverId, command)
419-
: await execAsync(command);
416+
// Check if port is in use by a Docker container
417+
const dockerCommand = `docker ps -a --format '{{.Names}}' | grep -v '^dokploy-traefik$' | while read name; do docker port "$name" 2>/dev/null | grep -q ':${port}' && echo "$name" && break; done || true`;
418+
const { stdout: dockerOut } = serverId
419+
? await execAsyncRemote(serverId, dockerCommand)
420+
: await execAsync(dockerCommand);
420421

421-
const container = stdout.trim();
422+
const container = dockerOut.trim();
422423

423-
return {
424-
isInUse: !!container,
425-
conflictingContainer: container || undefined,
426-
};
424+
if (container) {
425+
return {
426+
isInUse: true,
427+
conflictingContainer: `container "${container}"`,
428+
};
429+
}
430+
431+
// Check if port is in use by a host-level service (non-Docker)
432+
// Dokploy runs inside a container, so we spawn an ephemeral container
433+
// with --net=host to share the host's network stack and use nc -z to
434+
// check if something is listening on the port
435+
const hostCommand = `docker run --rm --net=host busybox sh -c 'nc -z 0.0.0.0 ${port} 2>/dev/null && echo in_use || echo free'`;
436+
const { stdout: hostOut } = serverId
437+
? await execAsyncRemote(serverId, hostCommand)
438+
: await execAsync(hostCommand);
439+
440+
if (hostOut.includes("in_use")) {
441+
return {
442+
isInUse: true,
443+
conflictingContainer: "a host-level service",
444+
};
445+
}
446+
447+
return { isInUse: false };
427448
} catch (error) {
428449
console.error("Error checking port availability:", error);
429450
return { isInUse: false };

0 commit comments

Comments
 (0)