dockerdockerfileapple-siliconbuildx

Dockerfile cross platform TARGETARCH build not working


This is my 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

The problem is the libc6-i386 package. It just cannot be installed on amd64 architecture. I am building docker images on my Mac M2 machine (not sure if this is the problem), with this command:

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

It can successfully build 2 architecture images, but amd64 passes libc6-i386 package.

I tried to direct assign linux/amd64:

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

And I can see the output:

[+] 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

It is already comparing "amd64" = "amd64", but why doesn't it install libc6-i386 package?

Environments:

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

What's the problem here?


Solution

  • I think it's related to whitespace.

    When I try to run this command and got this error:

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

    I change it to this command with 2 more space and works

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

    So you should change your script to:

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