File size: 1,028 Bytes
b80fc11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { execAsync } from "@dokploy/server";

/** Returns if the current operating system is Windows Subsystem for Linux (WSL). */
export const isWSL = async () => {
	try {
		const { stdout } = await execAsync("uname -r");
		const isWSL = stdout.includes("microsoft");
		return isWSL;
	} catch (_error) {
		return false;
	}
};

/** Returns the Docker host IP address. */
export const getDockerHost = async (): Promise<string> => {
	if (process.env.NODE_ENV === "production") {
		if (process.platform === "linux" && !(await isWSL())) {
			try {
				// Try to get the Docker bridge IP first
				const { stdout } = await execAsync(
					"ip route | awk '/default/ {print $3}'",
				);

				const hostIp = stdout.trim();
				if (!hostIp) {
					throw new Error("Failed to get Docker host IP");
				}

				return hostIp;
			} catch (error) {
				console.error("Failed to get Docker host IP:", error);
				return "172.17.0.1"; // Default Docker bridge network IP
			}
		}

		return "host.docker.internal";
	}

	return "localhost";
};