mocker icon indicating copy to clipboard operation
mocker copied to clipboard

`compose` ignores the service `platform:` key

Open PixCZ opened this issue 1 month ago • 1 comments

mocker version: 0.9.7 (Homebrew) — macOS 27, container 1.5.0, arm64; reproduced on 0.9.6 and again on 0.9.7. Code references are against main @ 4402015.

Summary

platform: is never read from the compose file (ComposeFile.swift has no such key), so a service declaring platform: linux/amd64 pulls and runs the host's arm64 variant. mocker run --platform linux/amd64 works, so the pieces exist; compose just does not pass the value along.

Reproduction

services:
  web:
    image: busybox:1.36
    platform: linux/amd64
    command: ["sh", "-c", "uname -m; sleep 60"]
$ mocker compose up -d && mocker logs <project>-web-1
aarch64                 # Docker (with Rosetta): x86_64

Proposed behaviour (what the branch does)

  • ComposeService parses platform, merges it across files (later file wins) and includes it in the config hash, so changing it recreates the container on the next up.
  • The orchestrator passes it to the pull — important, because ImageManager.pull otherwise short-circuits on the already-present arm64 image — and to the run config, where it becomes --os/--arch. compose pull passes it too.

After, same file: x86_64.


A tested fix is on my fork, branch fix/compose-platform (compare); the diff is below. Happy to open it as a PR if you want it, or you can take it from here — see the note in # about coordinating.

Proposed fix: git diff main...fix/compose-platform
diff --git a/Sources/Mocker/Commands/Compose.swift b/Sources/Mocker/Commands/Compose.swift
index efff34b..d4864dd 100644
--- a/Sources/Mocker/Commands/Compose.swift
+++ b/Sources/Mocker/Commands/Compose.swift
@@ -794,7 +794,7 @@ struct ComposePull: AsyncParsableCommand {
             guard let image = service.image else { continue }
             do {
                 if !quiet { print("Pulling \(name) (\(image))...") }
-                _ = try await manager.pull(image)
+                _ = try await manager.pull(image, platform: service.platform)
                 if !quiet { print("Pulled \(name)") }
             } catch {
                 if ignorePullFailures {
diff --git a/Sources/MockerKit/Compose/ComposeFile.swift b/Sources/MockerKit/Compose/ComposeFile.swift
index fd54858..96a0974 100644
--- a/Sources/MockerKit/Compose/ComposeFile.swift
+++ b/Sources/MockerKit/Compose/ComposeFile.swift
@@ -530,6 +530,8 @@ public struct ComposeService: Sendable {
     public var labels: [String: String]
     public var hostname: String?
     public var workingDir: String?
+    /// `platform:` (e.g. `linux/amd64`), forwarded to the pull and the run.
+    public var platform: String?
 
     // Resource limits — Docker Compose spec (legacy top-level + deploy.resources)
     public var memLimit: String?
@@ -547,7 +549,7 @@ public struct ComposeService: Sendable {
 
     private enum HashCodingKeys: String, CodingKey {
         case name, image, command, entrypoint, environment, ports, volumes, networks
-        case restart, labels, hostname, workingDir
+        case restart, labels, hostname, workingDir, platform
         case memLimit, cpus, memReservation, cpusReservation, memSwapLimit
         case shmSize, pidsLimit
         case restartPolicyDelay, restartPolicyMaxAttempts, restartPolicyWindow
@@ -643,6 +645,7 @@ public struct ComposeService: Sendable {
             labels: labels,
             hostname: dict["hostname"] as? String,
             workingDir: dict["working_dir"] as? String,
+            platform: dict["platform"] as? String,
             memLimit: memLimit,
             cpus: cpus,
             memReservation: memReservation,
@@ -681,6 +684,7 @@ public struct ComposeService: Sendable {
             labels: labels.merging(other.labels) { _, new in new },
             hostname: other.hostname ?? hostname,
             workingDir: other.workingDir ?? workingDir,
+            platform: other.platform ?? platform,
             memLimit: other.memLimit ?? memLimit,
             cpus: other.cpus ?? cpus,
             memReservation: other.memReservation ?? memReservation,
@@ -860,6 +864,7 @@ extension ComposeService: Encodable {
         try c.encode(labels, forKey: .labels)
         try c.encodeIfPresent(hostname, forKey: .hostname)
         try c.encodeIfPresent(workingDir, forKey: .workingDir)
+        try c.encodeIfPresent(platform, forKey: .platform)
         try c.encodeIfPresent(memLimit, forKey: .memLimit)
         try c.encodeIfPresent(cpus, forKey: .cpus)
         try c.encodeIfPresent(memReservation, forKey: .memReservation)
diff --git a/Sources/MockerKit/Compose/ComposeOrchestrator.swift b/Sources/MockerKit/Compose/ComposeOrchestrator.swift
index c84739e..bfcb267 100644
--- a/Sources/MockerKit/Compose/ComposeOrchestrator.swift
+++ b/Sources/MockerKit/Compose/ComposeOrchestrator.swift
@@ -565,7 +565,9 @@ public actor ComposeOrchestrator {
         // both `image:` and `build:` is built and tagged with `image:` — not pulled.
         switch service.resolveImageSource(projectName: projectName, noBuild: noBuild) {
         case .pull(let image):
-            _ = try await imageManager.pull(image)
+            // Without the platform the pull fetches the host's variant and the run
+            // (`--os/--arch` from `platform:`) then finds no matching image.
+            _ = try await imageManager.pull(image, platform: service.platform)
         case .build(let tag, let build):
             // Skip the rebuild only when `--build` wasn't requested and the image exists.
             var shouldBuild = forceBuild
@@ -642,6 +644,7 @@ public actor ComposeOrchestrator {
             hostname: service.hostname,
             restartPolicy: service.restart.flatMap { RestartPolicy(rawValue: $0) } ?? .no,
             entrypoint: exec.entrypoint,
+            platform: service.platform,
             // `restartPolicy` and `shmSize` (like the soft mem/cpu reservations) are stored on the
             // config for Docker surface parity, mirroring `run`/`create`. Apple's `container` CLI
             // currently exposes no `--restart`/`--shm-size` flags, so these are NOT enforced by the
diff --git a/Tests/MockerKitTests/ComposePlatformTests.swift b/Tests/MockerKitTests/ComposePlatformTests.swift
new file mode 100644
index 0000000..4e49ff3
--- /dev/null
+++ b/Tests/MockerKitTests/ComposePlatformTests.swift
@@ -0,0 +1,33 @@
+import Testing
+@testable import MockerKit
+
+/// `platform:` on a service must survive parsing and take part in the config hash,
+/// so that changing it recreates the container on the next `up`.
+@Suite("Compose service platform")
+struct ComposePlatformTests {
+
+    private func parse(_ platformLine: String) throws -> ComposeService {
+        let yaml = """
+        services:
+          web:
+            image: nginx
+        \(platformLine)
+        """
+        let compose = try ComposeFile.parse(yaml)
+        return try #require(compose.services["web"])
+    }
+
+    @Test("platform: is parsed from the service definition")
+    func platformParsed() throws {
+        #expect(try parse("    platform: linux/amd64").platform == "linux/amd64")
+    }
+
+    @Test("changing platform changes the service config hash")
+    func platformAffectsHash() throws {
+        let amd64 = try parse("    platform: linux/amd64")
+        let arm64 = try parse("    platform: linux/arm64")
+        let none = try parse("")
+        #expect(ComposeService.hash(of: amd64) != ComposeService.hash(of: arm64))
+        #expect(ComposeService.hash(of: amd64) != ComposeService.hash(of: none))
+    }
+}

PixCZ avatar Oct 21 '26 08:10 PixCZ

Shipped in 0.9.8 (ref #109), checked with the Homebrew build on container 1.5.0. Thanks @BluPix!

us avatar Oct 25 '26 12:10 us