Skip to content

Commit f1119fa

Browse files
committed
Catch possible conflict when starting existing intercepted containers
1 parent 95344e3 commit f1119fa

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/interceptors/docker/docker-proxy.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ async function createDockerProxy(proxyPort: number, httpsConfig: { certPath: str
163163
"message": `Conflict. The container name ${
164164
containerName
165165
} is already in use by a running container.\n${''
166-
}HTTP Toolkit won't intercept this by default to avoid conflicts with shared resources. ${''
167-
}To create & intercept this container, either stop the existing unintercepted container, or use a different name.`
166+
}HTTP Toolkit won't intercept this by default to avoid conflicts over shared resources. ${''
167+
}To create & intercept this container, either stop the existing unintercepted container, or use a different container name.`
168168
}));
169169
return;
170170
} else if (existingContainer || hasDockerComposeLabels) {
@@ -185,12 +185,34 @@ async function createDockerProxy(proxyPort: number, httpsConfig: { certPath: str
185185
const startContainerMatch = START_CONTAINER_MATCHER.exec(reqPath);
186186
if (startContainerMatch) {
187187
const containerId = startContainerMatch[1];
188-
if (!isInterceptedContainer(await docker.getContainer(containerId).inspect(), proxyPort)) {
188+
const containerData = await docker.getContainer(containerId).inspect();
189+
190+
if (!isInterceptedContainer(containerData, proxyPort)) {
189191
res.writeHead(400).end(
190192
"HTTP Toolkit cannot intercept startup of preexisting non-intercepted containers. " +
191193
"The container must be recreated here first - try `docker run <image>` instead."
192194
);
193195
}
196+
197+
if (containerData.Name.endsWith(`_HTK${proxyPort}`)) {
198+
// Trim initial slash and our HTK suffix:
199+
const clonedContainerName = containerData.Name.slice(1, -1 * `_HTK${proxyPort}`.length);
200+
const clonedContainerData = await docker.getContainer(clonedContainerName)
201+
.inspect()
202+
.catch(() => undefined);
203+
204+
if (clonedContainerData && clonedContainerData.State.Running) {
205+
// If you successfully intercept a docker-compose container, stop it & start the original container(s),
206+
// and then restart the already-created intercepted container, you could risk conflicts, so we warn you:
207+
res.writeHead(409).end(
208+
`Conflict: an unintercepted container with the same base name is already running.\n${''
209+
}HTTP Toolkit won't launch intercepted containers in parallel by default to avoid conflicts ${''
210+
}over shared resources. To create & intercept this container, either stop the existing ${''
211+
}unintercepted container, or use a different container name.`
212+
213+
);
214+
}
215+
}
194216
}
195217

196218
if (

0 commit comments

Comments
 (0)