|
1 | | -const isDocker = (() => { |
2 | | - let isDockerProcess; |
3 | | - |
4 | | - const hostname = require("os").hostname(); |
5 | | - const pattern = new RegExp( |
6 | | - "[0-9]+\:[a-z_-]+\:\/docker\/" + hostname + "[0-9a-z]+", "i", |
7 | | - ); |
| 1 | +const {execSync} = require("child_process"); |
| 2 | +const {hostname} = require("os"); |
8 | 3 |
|
| 4 | +const isDocker = (() => { |
| 5 | + // assumption: an Embark container is always a Linux Docker container, though |
| 6 | + // the Docker host may be Linux, macOS, or Windows |
| 7 | + if (process.platform !== "linux") { return false; } |
9 | 8 | try { |
10 | | - isDockerProcess = require("child_process") |
11 | | - .execSync( |
12 | | - "cat /proc/self/cgroup", |
13 | | - {stdio: ["ignore", "pipe", "ignore"]}, |
14 | | - ) |
15 | | - .toString().match(pattern) !== null; |
| 9 | + return ( |
| 10 | + new RegExp(`[0-9]+\:[a-z_-]+\:\/docker\/${hostname()}[0-9a-z]+`, "i") |
| 11 | + ).test( |
| 12 | + execSync( |
| 13 | + "cat /proc/self/cgroup", |
| 14 | + {stdio: ["ignore", "pipe", "ignore"]}, |
| 15 | + ).toString(), |
| 16 | + ); |
16 | 17 | } catch (e) { |
17 | | - isDockerProcess = false; |
| 18 | + return false; |
18 | 19 | } |
19 | | - |
20 | | - return isDockerProcess; |
21 | 20 | })(); |
22 | 21 |
|
23 | 22 | const defaultHost = isDocker ? "0.0.0.0" : "localhost"; |
24 | 23 |
|
25 | | -// when we"re runing in Docker, we can expect (generally, in a development |
| 24 | +// when we're runing in Docker, we can expect (generally, in a development |
26 | 25 | // scenario) that the user would like to connect to the service in the |
27 | | -// container via the **host"s** loopback address, so this helper can be used to |
| 26 | +// container via the **host's** loopback address, so this helper can be used to |
28 | 27 | // swap 0.0.0.0 for localhost in code/messages that pertain to client-side |
29 | 28 | function canonicalHost(host: string): string { |
30 | 29 | return isDocker && host === "0.0.0.0" ? "localhost" : host; |
|
0 commit comments