Dockerfile跨平台TARGETARCH构建不工作[重复]

bvn4nwqk  于 2023-01-04  发布在  Docker
关注(0)|答案(1)|浏览(254)
    • 此问题在此处已有答案**:

Why should there be spaces around '[' and ']' in Bash?(5个答案)
昨天关门了。
这是我的Dockerfile:

FROM tomcat:9

RUN apt-get update
RUN apt-get install -y iputils-ping file

ARG TARGETARCH
RUN if ["$TARGETARCH" = "amd64"]; then  \
    apt-get install -y libc6-i386 ;  \
    fi

WORKDIR /usr/local/tomcat
... // skipped

问题出在libc6-i386包上,它就是不能安装在amd64架构上。我正在我的Mac M2机器上构建docker映像(不确定这是否是问题所在),使用以下命令:

docker buildx build --platform linux/amd64,linux/arm64 --push -t foobar .

它可以成功构建2个体系结构映像,但amd64通过了libc6-i386包。
我尝试直接赋值linux/amd64

docker buildx build --platform linux/amd64 -t foobar .

我可以看到输出:

[+] Building 26.1s (15/15) FINISHED
 => [internal] load .dockerignore                                                                                                           0.0s
 => => transferring context: 2B                                                                                                             0.0s
 => [internal] load build definition from Dockerfile                                                                                        0.0s
 => => transferring dockerfile: 3.37kB                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/tomcat:9                                                                                 1.8s
 => [auth] library/tomcat:pull token for registry-1.docker.io                                                                               0.0s
 => CACHED [ 1/10] FROM docker.io/library/tomcat:9@sha256:39cb3ef7ca9...                  0.0s
 => => resolve docker.io/library/tomcat:9@sha256:39cb3ef7ca9005...                          0.0s
 => [internal] load build context                                                                                                           0.0s
 => => transferring context: 7.51kB                                                                                                         0.0s
 => [ 2/10] RUN apt-get update                                                                                                             14.7s
 => [ 3/10] RUN apt-get install -y iputils-ping file                                                                                        8.7s
 => [ 4/10] RUN if ["amd64" = "amd64"]; then      apt-get install -y libc6-i386 ;      fi                                                   0.1s
 => [ 5/10] WORKDIR /usr/local/tomcat

它已经在比较"amd64" = "amd64",但为什么不安装libc6-i386包?
环境:

docker version 
Client:
 Cloud integration: v1.0.29
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:01:18 2022
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.15.0 (93002)
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 17:59:41 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.10
  GitCommit:        770bd0108c32f3fb5c73ae1264f7e503fe7b2661
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker buildx version
github.com/docker/buildx v0.9.1 ed00243a0ce2a0aee75311b06e32d33b44729689

有什么问题吗?

nnt7mjpx

nnt7mjpx1#

我觉得这和太空有关。
当我尝试运行此命令时,出现以下错误:

$ if ["amd64" = "amd64"]; then echo hi ;      fi
[amd64: command not found

我将其更改为这个命令,多了2个空格,

$ if [ "amd64" = "amd64" ]; then echo hi ;      fi
hi

因此,您应该将脚本更改为:

RUN if [ "$TARGETARCH" = "amd64" ]; then  \
    apt-get install -y libc6-i386 ;  \
    fi

相关问题