dockerfile --FROM 指令的平台选项不起作用
dockerfile --platform option of the FROM instruction not work
Docker版本:
my base_image 具有多种架构:

Docker文件:
我使用 FROM --platform=linux/arm64 ${base_image}
强制使用 arm64 图像,但它不起作用。然后在本机查看镜像,发现镜像的arch是amd64,所以怀疑是不是和本地镜像有关
所以我将 base_image force 更改为 arm64 并重新构建:
魔法发生了!!!
所以,我的问题是为什么 FROM 的 --platform
不起作用?为什么 docker 不执行 docker pull --platform
而不是它取决于我的本地机器映像。
PS:不好意思对一些敏感词进行了脱敏,影响阅读
------------------------在 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
--platform
参数是在 buildkit 中引入的,我现在倾向于推荐大多数构建:
$ 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
对于经典构建器,它将默认为主机上已经存在的图像,并且仅在图像不存在或您指定 --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
Docker版本:
my base_image 具有多种架构:
Docker文件:
我使用 FROM --platform=linux/arm64 ${base_image}
强制使用 arm64 图像,但它不起作用。然后在本机查看镜像,发现镜像的arch是amd64,所以怀疑是不是和本地镜像有关
所以我将 base_image force 更改为 arm64 并重新构建:
魔法发生了!!!
所以,我的问题是为什么 FROM 的 --platform
不起作用?为什么 docker 不执行 docker pull --platform
而不是它取决于我的本地机器映像。
PS:不好意思对一些敏感词进行了脱敏,影响阅读
------------------------在 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
--platform
参数是在 buildkit 中引入的,我现在倾向于推荐大多数构建:
$ 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
对于经典构建器,它将默认为主机上已经存在的图像,并且仅在图像不存在或您指定 --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