为什么docker buildx 会忽略 env 命令?

使用 docker buildx 多平台构建的时候,遇到了一个问题,就是我的 ENV 命令被忽略了?

# syntax=docker/dockerfile:1

# x86 架构
FROM ubuntu:focal AS base_focal

RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
    . /etc/os-release && cat << EOF > /etc/apt/sources.list \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse \
    EOF \
    ; fi

RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
    . /etc/os-release && cat << EOF > /etc/apt/sources.list \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse \
    EOF \
    ; fi



RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y openjdk-8-jdk

RUN apt-get install -y wget
WORKDIR /root
RUN wget -P /root https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.4.17/hbase-2.4.17-bin.tar.gz
RUN tar xvf /root/hbase-2.4.17-bin.tar.gz

# RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-arm64" | tee -a /etc/profile >/dev/null


# RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
#     export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-arm64"; \
#     else \
#     export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"; \
#     fi && \
#     echo "JAVA_HOME=$JAVA_HOME" >> /etc/environment

# ENV JAVA_HOME=$JAVA_HOME


COPY entrypoint.sh /root/

ENV JAVA_HOME2="haha"


CMD ["/root/entrypoint.sh"]

我在后面设置了一个 ENV JAVA_HOME2="haha"

输入命令

docker buildx build --platform linux/amd64,linux/arm64 -t ponponon/hbase:2.4.17 .

输出

╰─➤  docker buildx build --platform linux/amd64,linux/arm64 -t ponponon/hbase:2.4.17 .
[+] Building 3.7s (33/33) FINISHED                                                                                                                                                                                                             docker-container:mybuilder
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                 0.0s
 => => transferring dockerfile: 1.97kB                                                                                                                                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                      0.0s
 => resolve image config for docker.io/docker/dockerfile:1                                                                                                                                                                                                           1.6s
 => [auth] docker/dockerfile:pull token for registry-1.docker.io                                                                                                                                                                                                     0.0s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14                                                                                                                                      0.0s
 => => resolve docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14                                                                                                                                                 0.0s
 => [linux/arm64 internal] load metadata for docker.io/library/ubuntu:focal                                                                                                                                                                                          0.5s
 => [linux/amd64 internal] load metadata for docker.io/library/ubuntu:focal                                                                                                                                                                                          0.8s
 => [auth] library/ubuntu:pull token for registry-1.docker.io                                                                                                                                                                                                        0.0s
 => [linux/amd64  1/12] FROM docker.io/library/ubuntu:focal@sha256:c9820a44b950956a790c354700c1166a7ec648bc0d215fa438d3a339812f1d01                                                                                                                                  0.0s
 => => resolve docker.io/library/ubuntu:focal@sha256:c9820a44b950956a790c354700c1166a7ec648bc0d215fa438d3a339812f1d01                                                                                                                                                0.0s
 => [internal] load build context                                                                                                                                                                                                                                    0.0s
 => => transferring context: 35B                                                                                                                                                                                                                                     0.0s
 => [linux/arm64  1/12] FROM docker.io/library/ubuntu:focal@sha256:c9820a44b950956a790c354700c1166a7ec648bc0d215fa438d3a339812f1d01                                                                                                                                  0.0s
 => => resolve docker.io/library/ubuntu:focal@sha256:c9820a44b950956a790c354700c1166a7ec648bc0d215fa438d3a339812f1d01                                                                                                                                                0.0s
 => CACHED [linux/amd64  2/12] RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then     . /etc/os-release && cat << EOF > /etc/apt/sources.list     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse     deb http://mirrors.  0.0s
 => CACHED [linux/amd64  3/12] RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then     . /etc/os-release && cat << EOF > /etc/apt/sources.list     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse     deb http://mi  0.0s
 => CACHED [linux/amd64  4/12] RUN apt-get update                                                                                                                                                                                                                    0.0s
 => CACHED [linux/amd64  5/12] RUN apt-get install -y vim                                                                                                                                                                                                            0.0s
 => CACHED [linux/amd64  6/12] RUN apt-get install -y openjdk-8-jdk                                                                                                                                                                                                  0.0s
 => CACHED [linux/amd64  7/12] RUN apt-get install -y wget                                                                                                                                                                                                           0.0s
 => CACHED [linux/amd64  8/12] WORKDIR /root                                                                                                                                                                                                                         0.0s
 => CACHED [linux/amd64  9/12] RUN wget -P /root https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.4.17/hbase-2.4.17-bin.tar.gz                                                                                                                                    0.0s
 => CACHED [linux/amd64 10/12] RUN tar xvf /root/hbase-2.4.17-bin.tar.gz                                                                                                                                                                                             0.0s
 => CACHED [linux/amd64 11/12] RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-arm64" | tee -a /etc/profile >/dev/null                                                                                                                                        0.0s
 => CACHED [linux/arm64  2/12] RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then     . /etc/os-release && cat << EOF > /etc/apt/sources.list     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse     deb http://mirrors.  0.0s
 => CACHED [linux/arm64  3/12] RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then     . /etc/os-release && cat << EOF > /etc/apt/sources.list     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse     deb http://mi  0.0s
 => CACHED [linux/arm64  4/12] RUN apt-get update                                                                                                                                                                                                                    0.0s
 => CACHED [linux/arm64  5/12] RUN apt-get install -y vim                                                                                                                                                                                                            0.0s
 => CACHED [linux/arm64  6/12] RUN apt-get install -y openjdk-8-jdk                                                                                                                                                                                                  0.0s
 => CACHED [linux/arm64  7/12] RUN apt-get install -y wget                                                                                                                                                                                                           0.0s
 => CACHED [linux/arm64  8/12] WORKDIR /root                                                                                                                                                                                                                         0.0s
 => CACHED [linux/arm64  9/12] RUN wget -P /root https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.4.17/hbase-2.4.17-bin.tar.gz                                                                                                                                    0.0s
 => CACHED [linux/arm64 10/12] RUN tar xvf /root/hbase-2.4.17-bin.tar.gz                                                                                                                                                                                             0.0s
 => CACHED [linux/arm64 11/12] RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-arm64" | tee -a /etc/profile >/dev/null                                                                                                                                        0.0s
 => [linux/amd64 12/12] COPY entrypoint.sh /root/                                                                                                                                                                                                                    0.0s
 => [linux/arm64 12/12] COPY entrypoint.sh /root/ 

看不到有 ENV 的被执行了

查看镜像的信息

docker image inspect ponponon/hbase:2.4.17 

确实没有 JAVA_HOME2

╭─pon@admini ~/opt/docker-compose-public/hbase-focal docker image inspect ponponon/hbase:2.4.17                                                                                                                                                                     130 ↵
[
    {
        "Id": "sha256:e97a9027dfd6bbf70ad6a5b0b73dba25f540fc8569e2a8cf2a3e4cfbcf721473",
        "RepoTags": [
            "ponponon/hbase-x86-focal:latest",
            "ponponon/hbase:2.4.17"
        ],
        "RepoDigests": [
            "ponponon/hbase@sha256:0c66a2af5ea9ea0c2413657e4fb2cd9115039024a0ffe80508a58890af618579"
        ],
        "Parent": "",
        "Comment": "buildkit.dockerfile.v0",
        "Created": "2023-07-18T03:16:00.174219334Z",
        "Container": "",
        "ContainerConfig": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "bash"
            ],
            "Image": "",
            "Volumes": null,
            "WorkingDir": "/root",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1298017597,
        "VirtualSize": 1298017597,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/p6scpswh8i0nueawa10qifmi6/diff:/var/lib/docker/overlay2/i5qd96mj8g7ktq429qtx6ouni/diff:/var/lib/docker/overlay2/nzjsz04yctk37sp1j1q5xrl7k/diff:/var/lib/docker/overlay2/7fpbypao40nnmmyekywouarkf/diff:/var/lib/docker/overlay2/tknfyh2le3lh196lw9b0c9pa0/diff:/var/lib/docker/overlay2/yi2684mu1kfro7mqejgk2te80/diff:/var/lib/docker/overlay2/ib49q6zhd2rxnoqxn9sqjckil/diff:/var/lib/docker/overlay2/llv1kxk8pqpx41qarot5q2s3w/diff:/var/lib/docker/overlay2/qow3hd2q1srvjosa89mzx8y02/diff:/var/lib/docker/overlay2/e2a3710f4d5a0c6b7fe989d38368df9984e581afa8032728e3f2bb4dd70ca363/diff",
                "MergedDir": "/var/lib/docker/overlay2/jdgfcxhc763s6uph8fxpankcn/merged",
                "UpperDir": "/var/lib/docker/overlay2/jdgfcxhc763s6uph8fxpankcn/diff",
                "WorkDir": "/var/lib/docker/overlay2/jdgfcxhc763s6uph8fxpankcn/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b",
                "sha256:14d6affed4adc0ef5f5bf13088d30c8bc2ad380b4a3ecfcc99317fef0c44b318",
                "sha256:c294749127ef4fa7a907998b83a39fa118c9c8ae2b182fdba08268956b18c665",
                "sha256:e144ebbfe1198309c450f06a75523cc48f27313982b2b0a5c5e1d827fdc4ad8f",
                "sha256:b071800f76fa7f5c42c64f4a1367eda78859d1a8e3299d75afd0e025fb20f801",
                "sha256:1fb8d5fc27059aa0ba8150971902e2478c4a70153cc40c85e65f50271def8473",
                "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef",
                "sha256:d82aed3adae27707e39f3ce3a4fb923cfdb3e81b13df0ccd3d24fc7ccd5b61a7",
                "sha256:b21157a8f1f435cac7a2ba1e93ca3faa63221cc41ba587b4ad830dc99f7428bc",
                "sha256:5303d123946a39704644497c7752882fae4abe7055f295c1c07b1affb12c135b",
                "sha256:5cbe117ca7a7c9b431dfe15dad6815e56a9d65f7525a9209e6f7fe8af114db3d"
            ]
        },
        "Metadata": {
            "LastTagTime": "2023-07-18T05:32:14.609481323Z"
        }
    }
]

图片.png


我发现不仅仅是 ENV 没有被执行,连 CMD 都没有执行

图片.png

CMD 还是默认的 bash

阅读 2.1k
1 个回答

我大概知道了,我用的 docker buildx build --platform linux/amd64,linux/arm64 -t ponponon/hbase:2.4.17 . 命令有问题,这个命令是不会把镜像保存到本地的

也就是说,docker image ls 看到的镜像,是很久以前 docker build 产生的,而不是现在 docker buildx build 产生的

那么,怎么才能让 docker buildx build 把镜像保存到本地呢?可以用 --output 参数(具体参考:https://docs.docker.com/engine/reference/commandline/buildx_b...

但是这个参数设计的太脑残了,我换着法子跑了几遍,还是不行

所以我就放弃了,直接 push 到 docker hub 吧

把命令改成下面的样子:

docker buildx build --platform linux/amd64,linux/arm64 -t ponponon/hbase:2.4.17 . --push

这样镜像打包好后,就会自己 push 到 docker hub 了

然后我去 docker hub 看了一下,你猜怎么着?

虽然 docker buildx 输出的 log 里面没有处理 ENV 和 CMD,但是从 docker hub 是能看到有 ENV 和 CMD 的

https://hub.docker.com/layers/ponponon/hbase/2.4.17/images/sh...

图片.png

无力吐槽

推荐问题
宣传栏