Skip to content

Commit 6fb0d18

Browse files
committed
fix: add platform field and use JSON format for images parsing
- Add platform field to DockerComposeImListResultService type - Use --format json internally in images() for robust parsing - Fixes parsing issues with newer docker compose versions that include PLATFORM column
1 parent 49cf689 commit 6fb0d18

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export type DockerComposeImListResultService = {
105105
container: string
106106
repository: string
107107
tag: string
108+
platform: string
108109
id: string // 12 Byte id
109110
}
110111

@@ -206,6 +207,7 @@ export const mapImListOutput = (
206207
container: serviceLine.ContainerName,
207208
repository: serviceLine.Repository,
208209
tag: serviceLine.Tag,
210+
platform: serviceLine.Platform || '',
209211
id: idFragment
210212
}
211213
})
@@ -219,6 +221,7 @@ export const mapImListOutput = (
219221
.map((line) => {
220222
// the line has the columns in the following order:
221223
// CONTAINER REPOSITORY TAG IMAGE ID SIZE
224+
// Note: newer docker compose versions may include PLATFORM column
222225
const lineColumns = line.split(/\s{3,}/)
223226

224227
const containerFragment = lineColumns[0] || line
@@ -230,6 +233,7 @@ export const mapImListOutput = (
230233
container: containerFragment.trim(),
231234
repository: repositoryFragment.trim(),
232235
tag: tagFragment.trim(),
236+
platform: '',
233237
id: idFragment.trim()
234238
} as DockerComposeImListResultService
235239
})
@@ -631,8 +635,13 @@ export const images = async function (
631635
options?: IDockerComposeOptions
632636
): Promise<TypedDockerComposeResult<DockerComposeImListResult>> {
633637
try {
634-
const result = await execCompose('images', [], options)
635-
const data = mapImListOutput(result.out, options)
638+
// Always use JSON format for robust parsing across docker compose versions
639+
const jsonOptions: IDockerComposeOptions = {
640+
...options,
641+
commandOptions: [...(options?.commandOptions || []), ['--format', 'json']]
642+
}
643+
const result = await execCompose('images', [], jsonOptions)
644+
const data = mapImListOutput(result.out, jsonOptions)
636645
return {
637646
...result,
638647
data

test/compose.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ describe('parseImListOutput', (): void => {
10601060
container: 'compose_test_hello',
10611061
repository: 'hello-world',
10621062
tag: 'latest',
1063+
platform: '',
10631064
id: 'd2c94e258dcb'
10641065
})
10651066

@@ -1068,13 +1069,15 @@ describe('parseImListOutput', (): void => {
10681069
container: 'compose_test_proxy',
10691070
repository: 'nginx',
10701071
tag: '1.19.9-alpine',
1072+
platform: '',
10711073
id: '72ab4137bd85'
10721074
})
10731075

10741076
expect(psOut.services[2]).toEqual({
10751077
container: 'compose_test_web',
10761078
repository: 'nginx',
10771079
tag: '1.16.0',
1080+
platform: '',
10781081
id: 'ae893c58d83f'
10791082
})
10801083
})

0 commit comments

Comments
 (0)