Home > Back-end >  dockerfile --platform option of the FROM instruction not work
dockerfile --platform option of the FROM instruction not work

Time:05-07

Docker version: enter image description here

my base_image with multiple architecture: enter image description here

Dockerfile:
I use FROM --platform=linux/arm64 ${base_image} to force use the arm64 image but it does not work. Then I checked the image on this machine and found the arch of the image is amd64, so I doubt it has something to do with the local image.

enter image description here

so I change the base_image force to arm64 and re-build:
Magic happens!!! enter image description here

So, my question is why --platform of FROM does not work? Why docker does not perform docker pull --platform instead it depends on my local machine image.

PS: I'm sorry I had to desensitize some sensitive words and it will affect your reading.

-------------------------reproduce on windows PC-------------------------------------------------

fengyq@DESKTOP-918EPFF:~$ docker version
Client: Docker Engine - Community
 Cloud integration: 1.0.14
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:46:45 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:44:56 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

fengyq@DESKTOP-918EPFF:~$ cat Dockerfile
FROM --platform=linux/arm64 golang:1.16-alpine
RUN go version

fengyq@DESKTOP-918EPFF:~$ docker pull  golang:1.16-alpine
1.16-alpine: Pulling from library/golang
Digest: sha256:5616dca835fa90ef13a843824ba58394dad356b7d56198fb7c93cbe76d7d67fe
Status: Downloaded newer image for golang:1.16-alpine
docker.io/library/golang:1.16-alpine

fengyq@DESKTOP-918EPFF:~$ docker image inspect golang:1.16-alpine|grep Architecture -A2
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 301868964,

# It will not download the image specified in FROM instruction, see the `go version` output
fengyq@DESKTOP-918EPFF:~$ docker build --no-cache . -t test
Sending build context to Docker daemon  122.4kB
Step 1/2 : FROM --platform=linux/arm64 golang:1.16-alpine
 ---> 7642119cd161
Step 2/2 : RUN go version
 ---> Running in 7d020707da41
go version go1.16.15 linux/amd64
Removing intermediate container 7d020707da41
 ---> 26c1c4bf971e
Successfully built 26c1c4bf971e
Successfully tagged test:latest

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

CodePudding user response:

The --platform parameter was introduced in buildkit, and I tend to recommend that for most builds now:

$ DOCKER_BUILDKIT=1 docker build -t test-platform -f df.platform --progress plain --no-cache .
#1 [internal] load build definition from df.platform
#1 sha256:5c840b4d7475cccb1fc86fce5ee78796e600289df0bb6de6c73430d268e9389d
#1 transferring dockerfile: 38B done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 sha256:1140f41a9b3ce804e3b52ff100b4cad659a81a19c059e58d6dc857c0e367c821
#2 transferring context: 34B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/golang:1.16-alpine
#3 sha256:066c23f588b92c8811e28ac05785cd295f354b1e7f60b3e42c4008ec173536c2
#3 DONE 0.2s

#4 [1/2] FROM docker.io/library/golang:1.16-alpine@sha256:5616dca835fa90ef13a843824ba58394dad356b7d56198fb7c93cbe76d7d67fe
#4 sha256:d20c37de2e493c7729ae105da84b8907178eed8cc5d1a935db9a50e2370830c2
#4 CACHED

#5 [2/2] RUN go version
#5 sha256:158e1ccd4f04dd9d9e1d7cb1008671d8b25cf42ff017d0f2fce6cc08899a77f4
#5 0.529 go version go1.16.15 linux/arm64
#5 DONE 0.5s

#6 exporting to image
#6 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#6 exporting layers 0.0s done
#6 writing image sha256:3901f37e2cfca681676cd6c6043d3b88594664c44b1f4e873c183e0a200852d5 done
#6 naming to docker.io/library/test-platform done
#6 DONE 0.0s

With the classic builder, it will default to the already existing image on the host, and only pull a new one when the image doesn't exist or when you specify --pull:

$ DOCKER_BUILDKIT=0 docker build -t test-platform -f df.platform .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM --platform=linux/arm64 golang:1.16-alpine
 ---> df1795ddbf41
Step 2/2 : RUN go version
 ---> Running in f53586180318
go version go1.16.8 linux/amd64
Removing intermediate container f53586180318
 ---> a250bd04bb4b
Successfully built a250bd04bb4b
Successfully tagged test-platform:latest

$ DOCKER_BUILDKIT=0 docker build -t test-platform --pull -f df.platform .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM --platform=linux/arm64 golang:1.16-alpine
1.16-alpine: Pulling from library/golang
9b3977197b4f: Already exists 
1a89e8eeedd5: Already exists 
94645a83ff95: Already exists 
7ed97893b138: Already exists 
57a2943bcc95: Already exists 
Digest: sha256:5616dca835fa90ef13a843824ba58394dad356b7d56198fb7c93cbe76d7d67fe
Status: Downloaded newer image for golang:1.16-alpine
 ---> 4a5e4084930e
Step 2/2 : RUN go version
 ---> [Warning] The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
 ---> Running in 5a0893533b89
go version go1.16.15 linux/arm64
Removing intermediate container 5a0893533b89
 ---> 2dd93e25714a
Successfully built 2dd93e25714a
Successfully tagged test-platform:latest
  • Related