Skip to content

Commit 82cd3fd

Browse files
committed
docker/install: Fix latest image install on lima
`latest` is not a valid git tag or revision to get the matching systemd unit files. Look up the exact source git commit from the `'org.opencontainers.image.revision` image config label. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent e2acba1 commit 82cd3fd

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

__tests__/docker/install.test.itg.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-g
4343
test.each([
4444
{type: 'image', tag: '27.3.1'} as InstallSourceImage,
4545
{type: 'image', tag: 'master'} as InstallSourceImage,
46+
{type: 'image', tag: 'latest'} as InstallSourceImage,
4647
{type: 'archive', version: 'v26.1.4', channel: 'stable'} as InstallSourceArchive,
4748
{type: 'archive', version: 'latest', channel: 'stable'} as InstallSourceArchive,
4849
])(

src/docker/assets.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,10 @@ provision:
237237
238238
HOME=/tmp undock moby/moby-bin:{{srcImageTag}} /usr/local/bin
239239
240-
wget https://raw.githubusercontent.com/moby/moby/{{srcImageTag}}/contrib/init/systemd/docker.service \
241-
https://raw.githubusercontent.com/moby/moby/v{{srcImageTag}}/contrib/init/systemd/docker.service \
242-
-O /etc/systemd/system/docker.service || true
243-
wget https://raw.githubusercontent.com/moby/moby/{{srcImageTag}}/contrib/init/systemd/docker.socket \
244-
https://raw.githubusercontent.com/moby/moby/v{{srcImageTag}}/contrib/init/systemd/docker.socket \
245-
-O /etc/systemd/system/docker.socket || true
240+
wget https://raw.githubusercontent.com/moby/moby/{{gitCommit}}/contrib/init/systemd/docker.service \
241+
-O /etc/systemd/system/docker.service
242+
wget https://raw.githubusercontent.com/moby/moby/{{gitCommit}}/contrib/init/systemd/docker.socket \
243+
-O /etc/systemd/system/docker.socket
246244
247245
sed -i 's|^ExecStart=.*|ExecStart=/usr/local/bin/dockerd -H fd://|' /etc/systemd/system/docker.service
248246
sed -i 's|containerd.service||' /etc/systemd/system/docker.service

src/docker/install.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import {Util} from '../util';
3434
import {limaYamlData, dockerServiceLogsPs1, setupDockerWinPs1} from './assets';
3535
import {GitHubRelease} from '../types/github';
3636
import {HubRepository} from '../hubRepository';
37+
import {Index} from '../types/oci';
38+
import {MEDIATYPE_IMAGE_INDEX_V1} from '../types/oci/mediatype';
39+
import {MEDIATYPE_IMAGE_MANIFEST_LIST_V2} from '../types/docker/mediatype';
40+
import {Manifest} from '../types/oci/manifest';
41+
import {ImageConfig} from '../types/oci/config';
3742

3843
export interface InstallSourceImage {
3944
type: 'image';
@@ -71,6 +76,8 @@ export class Install {
7176
private _version: string | undefined;
7277
private _toolDir: string | undefined;
7378

79+
private gitCommit: string | undefined;
80+
7481
private readonly limaInstanceName = 'docker-actions-toolkit';
7582

7683
constructor(opts: InstallOpts) {
@@ -127,12 +134,24 @@ export class Install {
127134
const cli = await HubRepository.build('dockereng/cli-bin');
128135
extractFolder = await cli.extractImage(tag);
129136

137+
const moby = await HubRepository.build('moby/moby-bin');
130138
if (['win32', 'linux'].includes(platform)) {
131139
core.info(`Downloading dockerd from moby/moby-bin:${tag}`);
132-
const moby = await HubRepository.build('moby/moby-bin');
133140
await moby.extractImage(tag, extractFolder);
134141
} else if (platform == 'darwin') {
135-
// On macOS, the docker daemon binary will be downloaded inside the lima VM
142+
// On macOS, the docker daemon binary will be downloaded inside the lima VM.
143+
// However, we will get the exact git revision from the image config
144+
// to get the matching systemd unit files.
145+
const manifest = await moby.getPlatformManifest(tag);
146+
const config = await moby.getConfig<ImageConfig>(manifest.config.digest);
147+
148+
core.info(`Docker image config labels: ${JSON.stringify(config.Labels, null, 2)}`);
149+
this.gitCommit = config.Labels?.['org.opencontainers.image.revision'];
150+
if (!this.gitCommit) {
151+
core.warning(`No git revision can be determined from the image. Will use master.`);
152+
this.gitCommit = 'master';
153+
}
154+
core.info(`Git revision is ${this.gitCommit}`);
136155
} else {
137156
core.warning(`dockerd not supported on ${platform}, only the Docker cli will be available`);
138157
}
@@ -193,6 +212,9 @@ export class Install {
193212
}
194213

195214
private async installDarwin(): Promise<string> {
215+
if (!this.gitCommit) {
216+
throw new Error('gitCommit must be set. Run download first.');
217+
}
196218
const src = this.source;
197219
const limaDir = path.join(os.homedir(), '.lima', this.limaInstanceName);
198220
await io.mkdirP(limaDir);
@@ -229,6 +251,7 @@ export class Install {
229251
customImages: Install.limaCustomImages(),
230252
daemonConfig: limaDaemonConfig,
231253
dockerSock: `${limaDir}/docker.sock`,
254+
gitCommit: this.gitCommit,
232255
srcType: src.type,
233256
srcArchiveVersion: this._version, // Use the resolved version (e.g. latest -> 27.4.0)
234257
srcArchiveChannel: srcArchive.channel,

src/hubRepository.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import * as core from '@actions/core';
2121
import {Manifest} from './types/oci/manifest';
2222
import * as tc from '@actions/tool-cache';
2323
import fs from 'fs';
24-
import {MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from './types/oci/mediatype';
25-
import {MEDIATYPE_IMAGE_MANIFEST_V2, MEDIATYPE_IMAGE_MANIFEST_LIST_V2} from './types/docker/mediatype';
24+
import {MEDIATYPE_IMAGE_CONFIG_V1, MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from './types/oci/mediatype';
25+
import {MEDIATYPE_IMAGE_MANIFEST_V2, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_CONFIG_V1 as DOCKER_MEDIATYPE_IMAGE_CONFIG_V1} from './types/docker/mediatype';
2626
import {DockerHub} from './dockerhub';
2727

2828
export class HubRepository {
@@ -40,15 +40,19 @@ export class HubRepository {
4040
return new HubRepository(repository, token);
4141
}
4242

43-
// Unpacks the image layers and returns the path to the extracted image.
44-
// Only OCI indexes/manifest list are supported for now.
45-
public async extractImage(tag: string, destDir?: string): Promise<string> {
46-
const index = await this.getManifest<Index>(tag);
43+
public async getPlatformManifest(tagOrDigest: string): Promise<Manifest> {
44+
const index = await this.getManifest<Index>(tagOrDigest);
4745
if (index.mediaType != MEDIATYPE_IMAGE_INDEX_V1 && index.mediaType != MEDIATYPE_IMAGE_MANIFEST_LIST_V2) {
4846
throw new Error(`Unsupported image media type: ${index.mediaType}`);
4947
}
5048
const digest = HubRepository.getPlatformManifestDigest(index);
51-
const manifest = await this.getManifest<Manifest>(digest);
49+
return await this.getManifest<Manifest>(digest);
50+
}
51+
52+
// Unpacks the image layers and returns the path to the extracted image.
53+
// Only OCI indexes/manifest list are supported for now.
54+
public async extractImage(tag: string, destDir?: string): Promise<string> {
55+
const manifest = await this.getPlatformManifest(tag);
5256

5357
const paths = manifest.layers.map(async layer => {
5458
const url = this.blobUrl(layer.digest);
@@ -99,11 +103,19 @@ export class HubRepository {
99103
}
100104

101105
public async getManifest<T>(tagOrDigest: string): Promise<T> {
102-
const url = `https://registry-1.docker.io/v2/${this.repo}/manifests/${tagOrDigest}`;
106+
return this.registryGet<T>(tagOrDigest, 'manifests', [MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V1, MEDIATYPE_IMAGE_MANIFEST_V2]);
107+
}
108+
109+
public async getConfig<T>(tagOrDigest: string): Promise<T> {
110+
return this.registryGet<T>(tagOrDigest, 'blobs', [MEDIATYPE_IMAGE_CONFIG_V1, DOCKER_MEDIATYPE_IMAGE_CONFIG_V1]);
111+
}
112+
113+
private async registryGet<T>(tagOrDigest: string, endpoint: 'manifests' | 'blobs', accept: Array<string>): Promise<T> {
114+
const url = `https://registry-1.docker.io/v2/${this.repo}/${endpoint}/${tagOrDigest}`;
103115

104116
const headers = {
105117
Authorization: `Bearer ${this.token}`,
106-
Accept: [MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V1, MEDIATYPE_IMAGE_MANIFEST_V2].join(', ')
118+
Accept: accept.join(', ')
107119
};
108120
const resp = await HubRepository.http.get(url, headers);
109121
const body = await resp.readBody();

src/types/docker/mediatype.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
export const MEDIATYPE_IMAGE_MANIFEST_LIST_V2 = 'application/vnd.docker.distribution.manifest.list.v2+json';
1818

1919
export const MEDIATYPE_IMAGE_MANIFEST_V2 = 'application/vnd.docker.distribution.manifest.v2+json';
20+
21+
export const MEDIATYPE_IMAGE_CONFIG_V1 = 'application/vnd.docker.container.image.v1+json';

src/types/oci/mediatype.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const MEDIATYPE_IMAGE_INDEX_V1 = 'application/vnd.oci.image.index.v1+json
2323
export const MEDIATYPE_IMAGE_LAYER_V1 = 'application/vnd.oci.image.layer.v1.tar';
2424

2525
export const MEDIATYPE_EMPTY_JSON_V1 = 'application/vnd.oci.empty.v1+json';
26+
27+
export const MEDIATYPE_IMAGE_CONFIG_V1 = 'application/vnd.oci.image.config.v1+json';

0 commit comments

Comments
 (0)