Skip to content

Commit f191aa0

Browse files
committed
Improve Docker blank image name, and fix cleanup
1 parent 24f28cc commit f191aa0

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/interceptors/docker/docker-data-injection.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { OVERRIDES_DIR } from '../terminal/terminal-env-overrides';
88
import { isDockerAvailable } from "./docker-interception-services";
99
import { waitForDockerStream } from './docker-utils';
1010

11-
const DOCKER_BLANK_TAG = 'tech.httptoolkit.docker.scratch';
11+
const DOCKER_BLANK_IMAGE_NAME = 'httptoolkit/scratch';
12+
const DOCKER_BLANK_LABEL = 'tech.httptoolkit.docker.scratch';
1213
const DOCKER_VOLUME_LABEL = 'tech.httptoolkit.docker.data-volume';
1314
const DOCKER_VOLUME_CERT_LABEL = 'tech.httptoolkit.docker.data-volume.cert-content';
1415

@@ -70,23 +71,24 @@ async function createBlankImage(docker: Docker) {
7071
// Dockerfile, sending no context, so it's super fast.
7172

7273
// Skip image creation if it already exists:
73-
const existingImage = await docker.getImage(DOCKER_BLANK_TAG).inspect()
74+
const existingImage = await docker.getImage(DOCKER_BLANK_IMAGE_NAME).inspect()
7475
.then(() => true)
7576
.catch(() => false);
76-
if (existingImage) return DOCKER_BLANK_TAG;
77+
if (existingImage) return DOCKER_BLANK_IMAGE_NAME;
7778

7879
const blankImageContextStream = await buildTarStream([{
7980
path: 'Dockerfile',
8081
contents: 'FROM scratch\nENTRYPOINT ["FAIL"]\n'
8182
}]);
8283
const buildStream = await docker.buildImage(blankImageContextStream, {
8384
dockerfile: 'Dockerfile',
84-
t: DOCKER_BLANK_TAG
85+
t: DOCKER_BLANK_IMAGE_NAME,
86+
labels: { [DOCKER_BLANK_LABEL]: '' }
8587
});
8688

8789
await waitForDockerStream(docker, buildStream);
8890

89-
return DOCKER_BLANK_TAG;
91+
return DOCKER_BLANK_IMAGE_NAME;
9092
}
9193

9294
// Parallel processing of a single Docker volume and the other assorted containers is asking for trouble,
@@ -136,8 +138,8 @@ export function ensureDockerInjectionVolumeExists(certContent: string) {
136138

137139
// Then we create a blank container from the blank image with the volume mounted:
138140
const blankContainer = await docker.createContainer({
139-
Image: DOCKER_BLANK_TAG,
140-
Labels: { [DOCKER_BLANK_TAG]: '' },
141+
Image: DOCKER_BLANK_IMAGE_NAME,
142+
Labels: { [DOCKER_BLANK_LABEL]: '' },
141143
HostConfig: {
142144
Binds: [`${DOCKER_DATA_VOLUME_NAME}:/data-volume`]
143145
}
@@ -210,21 +212,21 @@ async function cleanupDataInjectionVolumes(docker: Docker, options: { keepCurren
210212
async function cleanupDataInjectionTools(docker: Docker) {
211213
const blankContainers = await docker.listContainers({
212214
all: true,
213-
filters: JSON.stringify({ label: [DOCKER_BLANK_TAG] })
215+
filters: JSON.stringify({ label: [DOCKER_BLANK_LABEL] })
214216
});
215217
await Promise.all(
216218
blankContainers
217-
.filter(c => c.Labels?.[DOCKER_BLANK_TAG] !== undefined) // Be extra careful
219+
.filter(c => c.Labels?.[DOCKER_BLANK_LABEL] !== undefined) // Be extra careful
218220
.map(c => docker.getContainer(c.Id).remove({ force: true }))
219221
);
220222

221223
const blankImages = await docker.listImages({
222-
filters: JSON.stringify({ label: [DOCKER_BLANK_TAG] })
224+
filters: JSON.stringify({ label: [DOCKER_BLANK_LABEL] })
223225
});
224226

225227
await Promise.all(
226228
blankImages
227-
.filter(i => i.Labels?.[DOCKER_BLANK_TAG] !== undefined) // Be extra careful
229+
.filter(i => i.Labels?.[DOCKER_BLANK_LABEL] !== undefined) // Be extra careful
228230
.map(i => docker.getImage(i.Id).remove({ force: true }))
229231
);
230232
}

0 commit comments

Comments
 (0)