containers / buildah

A tool that facilitates building OCI images.
https://buildah.io
Apache License 2.0
7.31k stars 772 forks source link

Multi-stage build incorrectly imports platform variant during COPY #5064

Closed aufarg closed 12 months ago

aufarg commented 1 year ago

Description

I'm currently trying to use multi-stage build to build a Go binary on an arm64 machine as amd64 image. I used arm64 image to build the binary and then copied to the amd64 image on the next build stage. The resulting manifest correctly specified the OS and Arch as linux and amd64, but it also specify Variant as v8.

After some tinkering, I find that the issue is because of COPY --from directive. On the Dockerfile used for repro below, if we remove the COPY, the manifest correctly unset the variant.

Steps to reproduce the issue:

  1. Create Dockerfile with the following content:
    FROM --platform=${BUILDPLATFORM} alpine:3.18.0 as builder
    RUN touch /file
    FROM --platform=${TARGETPLATFORM} alpine:3.18.0
    COPY --from=builder /file /file
    RUN echo test
  2. podman build -f Dockerfile --platform linux/amd64 --manifest localhost/test-podman:latest $PWD
  3. podman manifest inspect localhost/test-podman:latest

Describe the results you received:

$ podman manifest inspect localhost/test-podman:latest

{
    "schemaVersion": 2,
    "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
    "manifests": [
        {
            "mediaType": "application/vnd.oci.image.manifest.v1+json",
            "size": 901,
            "digest": "sha256:fda1a05616351a229195850caca537255eebbd88828149b6c61461d3a7e1e568",
            "platform": {
                "architecture": "amd64",
                "os": "linux",
                "variant": "v8"
            }
        }
    ]
}

Describe the results you expected:

{
    "schemaVersion": 2,
    "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
    "manifests": [
        {
            "mediaType": "application/vnd.oci.image.manifest.v1+json",
            "size": 901,
            "digest": "sha256:fda1a05616351a229195850caca537255eebbd88828149b6c61461d3a7e1e568",
            "platform": {
                "architecture": "amd64",
                "os": "linux"
            }
        }
    ]
}

Output of rpm -q buildah or apt list buildah:

N/A (installed with brew)

Output of buildah version:

N/A (invoked with podman)

Output of podman version if reporting a podman build issue:

4.6.2

*Output of `cat /etc/release`:**

zsh: no matches found: /etc/*release

Output of uname -a: (Unfortunately can't paste the whole thing)

Darwin Kernel Version 22.6.0

Output of cat /etc/containers/storage.conf:

cat: /etc/containers/storage.conf: No such file or directory
flouthoc commented 12 months ago

I check this thanks for reporting.

flouthoc commented 12 months ago

@aufarg This issue does not persists in recent version of podman and buildah. Could you please try with more recent version and report back. I'm closing this issue.

aufarg commented 12 months ago

Still happening to me, but when I checked podman version there's two output, one is 4.7.0 which is latest release in GitHub and the other is 4.6.2. Not sure how I can update the Server? podman machine info shows that it's already 4.7.0

$ podman version
Client:       Podman Engine
Version:      4.7.0
API Version:  4.7.0
Go Version:   go1.21.1
Git Commit:   073183fe1723d7bda826b574437891976a958c65
Built:        Wed Sep 27 23:35:55 2023
OS/Arch:      darwin/arm64

Server:       Podman Engine
Version:      4.6.2
API Version:  4.6.2
Go Version:   go1.20.7
Built:        Wed Sep 13 04:07:26 2023
OS/Arch:      linux/arm64
$ podman machine info
Host:
  Arch: arm64
  CurrentMachine: podman-machine-default
  DefaultMachine: podman-machine-default
  EventsDir: <redacted>
  MachineConfigDir: <redacted>
  MachineImageDir: <redacted>
  MachineState: Running
  NumberOfMachines: 1
  OS: darwin
  VMType: qemu
Version:
  APIVersion: 4.7.0
  Built: 1695828955
  BuiltTime: Wed Sep 27 23:35:55 2023
  GitCommit: 073183fe1723d7bda826b574437891976a958c65
  GoVersion: go1.21.1
  Os: darwin
  OsArch: darwin/arm64
  Version: 4.7.0
flouthoc commented 11 months ago

So your server is still on older version i think you will need to stop podman machine and delete using podman machine rm and create a new instance using podman machine init

aufarg commented 11 months ago

Still happens to me even on 4.7.0 both client/server. I'll try to debug this further.

$ podman version
Client:       Podman Engine
Version:      4.7.0
API Version:  4.7.0
Go Version:   go1.21.1
Git Commit:   073183fe1723d7bda826b574437891976a958c65
Built:        Wed Sep 27 23:35:55 2023
OS/Arch:      darwin/arm64

Server:       Podman Engine
Version:      4.7.0
API Version:  4.7.0
Go Version:   go1.20.8
Built:        Thu Sep 28 02:24:25 2023
OS/Arch:      linux/arm64
$ podman manifest inspect localhost/test-podman:latest
Error: getting content of manifest list or image localhost/test-podman:latest: reading image "docker://localhost/test-podman:latest": pinging container registry localhost: Get "https://localhost/v2/": dial tcp [::1]:443: connect: connection refused
$ podman build -f Dockerfile --platform linux/amd64 --manifest localhost/test-podman:latest $PWD                                                            
[1/2] STEP 1/2: FROM alpine:3.18.0 AS builder
podman [1/2] STEP 2/2: RUN touch /file
--> Using cache 7c10c346f84702cb4a1dd91f5905b0913ec0675df692d684518c66344fd96e9c
--> 7c10c346f847
[2/2] STEP 1/3: FROM alpine:3.18.0
[2/2] STEP 2/3: COPY --from=builder /file /file
--> Using cache a3188f32fef763e49a413095d1ebfdfcd6d4241daa70b838d2c9bff020784446
--> a3188f32fef7
[2/2] STEP 3/3: RUN echo test
--> Using cache 7b78b0906753b9a9c8bdb691c49d23337bdf61408d8fe0e9bc93b41076848f87
--> 7b78b0906753
7b78b0906753b9a9c8bdb691c49d23337bdf61408d8fe0e9bc93b41076848f87
$ podman manifest inspect localhost/test-podman:latest
{
    "schemaVersion": 2,
    "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
    "manifests": [
        {
            "mediaType": "application/vnd.oci.image.manifest.v1+json",
            "size": 901,
            "digest": "sha256:886c1d30caf857aefaa25a52b7cfe5559067b443311ad03a3a53beb2b32b9e0d",
            "platform": {
                "architecture": "amd64",
                "os": "linux",
                "variant": "v8"
            }
        }
    ]
}