Skip to content

Commit c9d100f

Browse files
committed
Fix the version check for Docker host-gateway support
1 parent 3f74f42 commit c9d100f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/interceptors/docker/docker-commands.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,28 @@ export const DOCKER_HOST_HOSTNAME = "host.docker.internal";
3737
* To make the above hostname work on Linux, where it's not supported by default, we need to map it to the
3838
* host ip. This method works out the host IP to use to do so.
3939
*/
40-
export const getDockerHostIp = (platform: typeof process.platform, dockerVersion: string | undefined, containerMetadata?: Docker.ContainerInspectInfo) => {
40+
export const getDockerHostIp = (
41+
platform: typeof process.platform,
42+
dockerVersion: { apiVersion: string } | { engineVersion: string },
43+
containerMetadata?: Docker.ContainerInspectInfo
44+
) => {
45+
const semverVersion = semver.coerce(
46+
'apiVersion' in dockerVersion
47+
? dockerVersion.apiVersion
48+
: dockerVersion.engineVersion
49+
);
50+
4151
if (platform !== 'linux') {
4252
// On non-linux platforms this method isn't necessary - host.docker.internal is always supported
4353
// so we can just use that.
4454
return DOCKER_HOST_HOSTNAME;
45-
} else if (dockerVersion &&
46-
semver.satisfies(semver.coerce(dockerVersion)?.version ?? '0.0.0', '>=1.21')
55+
} else if (
56+
semver.satisfies(
57+
semverVersion ?? '0.0.0',
58+
'apiVersion' in dockerVersion ? '>=1.41' : '>=20.10'
59+
)
4760
) {
61+
// This is supported in Docker Engine 20.10, so always supported at least in API 1.41+
4862
// Special name defined in new Docker versions, that refers to the host gateway
4963
return 'host-gateway';
5064
} else if (containerMetadata) {
@@ -271,7 +285,7 @@ export async function restartAndInjectContainer(
271285

272286
const proxyHost = getDockerHostIp(
273287
process.platform,
274-
(await docker.version()).ApiVersion,
288+
{ engineVersion: (await docker.version()).Version },
275289
containerDetails
276290
);
277291

src/interceptors/docker/docker-proxy.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async function createDockerProxy(proxyPort: number, httpsConfig: { certPath: str
111111
const reqUrl = new URL(req.url!, 'http://localhost');
112112
const reqPath = reqUrl.pathname;
113113

114-
const dockerVersion = API_VERSION_MATCH.exec(reqPath)?.[1];
114+
const dockerApiVersion = API_VERSION_MATCH.exec(reqPath)?.[1];
115115

116116
monitorDockerNetworkAliases(proxyPort);
117117

@@ -127,7 +127,7 @@ async function createDockerProxy(proxyPort: number, httpsConfig: { certPath: str
127127

128128
const proxyHost = getDockerHostIp(
129129
process.platform,
130-
dockerVersion ?? imageConfig?.DockerVersion,
130+
{ apiVersion: dockerApiVersion! },
131131
);
132132

133133
const transformedConfig = transformContainerCreationConfig(
@@ -247,7 +247,10 @@ async function createDockerProxy(proxyPort: number, httpsConfig: { certPath: str
247247
if (process.platform === 'linux') {
248248
reqUrl.searchParams.append(
249249
'extrahosts',
250-
`${DOCKER_HOST_HOSTNAME}:${getDockerHostIp(process.platform, dockerVersion)}`
250+
`${DOCKER_HOST_HOSTNAME}:${getDockerHostIp(
251+
process.platform,
252+
{ apiVersion: dockerApiVersion! }
253+
)}`
251254
);
252255
req.url = reqUrl.toString();
253256
}

0 commit comments

Comments
 (0)