Skip to content

Commit d4a4963

Browse files
committed
Log the detected Docker address we use
1 parent 1d60139 commit d4a4963

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

src/interceptors/docker/docker-interception-services.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ProxySettingCallback } from 'mockttp';
44
import { logError } from '../../error-tracking';
55
import { addShutdownHandler } from '../../shutdown';
66

7+
import { getDockerAddress } from './docker-utils';
78
import { DOCKER_BUILD_LABEL } from './docker-build-injection';
89
import { DOCKER_CONTAINER_LABEL } from './docker-commands';
910

@@ -76,9 +77,16 @@ export async function startDockerInterceptionServices(
7677
}
7778

7879
// Log if Docker was not available at proxy start, and why, for debugging later:
79-
isDockerAvailable({ logError: true }).then((isAvailable) => {
80-
if (isAvailable) console.log('Connected to Docker');
81-
// logError will log the specific not-available error, if this failed
80+
isDockerAvailable({ logError: true }).then(async (isAvailable) => {
81+
if (isAvailable) {
82+
const dockerAddress = await getDockerAddress(new Docker());
83+
console.log(`Connected to Docker at ${
84+
'socketPath' in dockerAddress
85+
? dockerAddress.socketPath
86+
: `tcp://${dockerAddress.host}:${dockerAddress.port}`
87+
}`);
88+
}
89+
// logError:true will log the specific not-available error, if this failed
8290
});
8391

8492
const networkMonitor = monitorDockerNetworkAliases(proxyPort);

src/interceptors/docker/docker-proxy.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { streamToBuffer } from '../../util/stream';
1414
import { logError } from '../../error-tracking';
1515
import { addShutdownHandler } from '../../shutdown';
1616

17+
import { getDockerAddress } from './docker-utils';
1718
import {
1819
isInterceptedContainer,
1920
transformContainerCreationConfig
@@ -76,18 +77,7 @@ async function createDockerProxy(
7677
) {
7778
const docker = new Dockerode();
7879

79-
// Hacky logic to reuse docker-modem's internal env + OS parsing logic to
80-
// work out where the local Docker host is:
81-
const modem = docker.modem as any as ({
82-
getSocketPath(): undefined | Promise<string>;
83-
host: string;
84-
port: number;
85-
});
86-
87-
const modemSocketPath = await modem.getSocketPath();
88-
const dockerHostOptions = modemSocketPath
89-
? { socketPath: modemSocketPath }
90-
: { host: modem.host, port: modem.port };
80+
const dockerHostOptions = await getDockerAddress(docker);
9181

9282
const agent = new http.Agent({ keepAlive: true });
9383

src/interceptors/docker/docker-utils.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,22 @@ export const waitForDockerStream = (
1515

1616
resolve();
1717
});
18-
});
18+
});
19+
20+
export const getDockerAddress = async (docker: Docker): Promise<
21+
| { socketPath: string }
22+
| { host: string, port: number }
23+
> => {
24+
// Hacky logic to reuse docker-modem's internal env + OS parsing logic to
25+
// work out where the local Docker host is:
26+
const modem = docker.modem as any as ({
27+
getSocketPath(): undefined | Promise<string>;
28+
host: string;
29+
port: number;
30+
});
31+
32+
const modemSocketPath = await modem.getSocketPath();
33+
return modemSocketPath
34+
? { socketPath: modemSocketPath }
35+
: { host: modem.host, port: modem.port };
36+
}

0 commit comments

Comments
 (0)