stille
V2EX  ›  Docker

Dockerfile FROM 能做判断吗?

  •  
  •   stille ·
    stilleshan · Jul 18, 2022 · 2487 views
    This topic created in 1395 days ago, the information mentioned may be changed or developed.

    一直是使用 buildx 来构建多架构镜像 push 到 docker hub,主要是 X86/ARM64 的. (github action 上来实施构建和 push)

    大部分 FROM 基础镜像都是大厂的,所以架构支持没问题,但是最近有个项目的基础镜像没有 ARM64 版,也不太容易修改.

    后来自己想办法做了个 ARM64 的..

    所以现在项目的 Dockerfile 文件里 FROM 需要分别对应 2 个镜像分别构建 2 个版本的镜像.

    请问如何能让 FROM 像 RUN 里可以用 if 判断来修改对应的架构镜像,来达到 buildx 单镜像多架构的目的?

    是否可以用多段构建的方式,在第一阶段用支持多架构的 alpine as builder 在来判断架构,在来修改下一阶段 FROM 的镜像名?

    6 replies    2022-08-07 22:24:28 +08:00
    ch2
        1
    ch2  
       Jul 18, 2022
    貌似可以传 args 来解决
    stille
        2
    stille  
    OP
       Jul 18, 2022
    @ch2 那只能是手动 docker build 时传递,我的想法是在 Dockerfile 中直接 if 后修改 ARG,后来发现 ARG 貌似只能固定赋值一个值,不能赋值变量.

    FROM alpine
    RUN if [ "$(uname -m)" = "x86_64" ]; then export DOCKER_NAME="aaa" ; else if [ "$(uname -m)" = "aarch64" ]; then export DOCKER_NAME="bbb" ; fi fi

    ARG DOCKER=${DOCKER_NAME}

    FROM ${DOCKER}/php-nginx:xxxx

    以上这种方式不行,ARG 拿不到变量
    initd
        3
    initd  
       Jul 18, 2022
    # syntax=docker/dockerfile:1
    FROM --platform=$BUILDPLATFORM alpine as build

    ARG TARGETPLATFORM
    ARG BUILDPLATFORM


    https://github.com/initdc/ssh-proxy-by-caddy-l4/blob/build/sh/Dockerfile#L2
    stille
        4
    stille  
    OP
       Jul 18, 2022
    @initd 感觉我这个需求和你这个还是不同吧.
    initd
        5
    initd  
       Jul 18, 2022
    momocraft
        6
    momocraft  
       Aug 7, 2022
    也许有方法把不同 image 捏成一个 multiarch image
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4490 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 10:07 · PVG 18:07 · LAX 03:07 · JFK 06:07
    ♥ Do have faith in what you're doing.