V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
KINGWAY
V2EX  ›  Docker

docker run 能否有更优雅高级的写法?

  •  
  •   KINGWAY · 142 天前 · 2239 次点击
    这是一个创建于 142 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位,这个 glibc 的版本是 2.35-r1 , 可否有更优雅高级点写法,能下载总是 latest 版本 glibc,这样就节约了维护 dockerfile 文件的时间和精力?

    RUN curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-2.35-r1.apk
    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-bin-2.35-r1.apk
    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-dev-2.35-r1.apk
    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-i18n-2.35-r1.apk

    RUN apk add --force-overwrite glibc-2.35-r1.apk
    && rm glibc-2.35-r1.apk
    && apk add --force-overwrite glibc-bin-2.35-r1.apk
    && rm glibc-bin-2.35-r1.apk
    && apk add --force-overwrite glibc-dev-2.35-r1.apk
    && rm glibc-dev-2.35-r1.apk
    && apk add --force-overwrite glibc-i18n-2.35-r1.apk
    && rm glibc-i18n-2.35-r1.apk

    第 1 条附言  ·  142 天前

    目前写法:

    RUN curl -L -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \

    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-2.35-r1.apk
    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-bin-2.35-r1.apk
    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-dev-2.35-r1.apk
    && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r1/glibc-i18n-2.35-r1.apk

    RUN apk add --force-overwrite glibc-2.35-r1.apk
    && rm glibc-2.35-r1.apk
    && apk add --force-overwrite glibc-bin-2.35-r1.apk
    && rm glibc-bin-2.35-r1.apk
    && apk add --force-overwrite glibc-dev-2.35-r1.apk
    && rm glibc-dev-2.35-r1.apk
    && apk add --force-overwrite glibc-i18n-2.35-r1.apk
    && rm glibc-i18n-2.35-r1.apk

    我打算参考下面这段来写,但是没成功

    ···· RUN DOWNLOAD_URL=$(curl -s "https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest" | grep -o '"browser_download_url": "[^"]*invoiceninja.tar"' | cut -d '"' -f 4) &&
    curl -LJO "$DOWNLOAD_URL" &&
    mv invoiceninja.tar /tmp/ninja.tar

    Extract Invoice Ninja

    RUN mkdir -p /var/www/app
    && tar -xvf /tmp/ninja.tar -C /var/www/app/
    && mkdir -p /var/www/app/public/logo /var/www/app/storage

    ····

    17 条回复    2024-06-26 17:51:21 +08:00
    DsuineGP
        1
    DsuineGP  
       142 天前   ❤️ 1
    KINGWAY
        2
    KINGWAY  
    OP
       142 天前
    @DsuineGP #1

    那么这段呢?

    RUN apk add --force-overwrite glibc-2.35-r1.apk
    && rm glibc-2.35-r1.apk
    && apk add --force-overwrite glibc-bin-2.35-r1.apk
    && rm glibc-bin-2.35-r1.apk
    && apk add --force-overwrite glibc-dev-2.35-r1.apk
    && rm glibc-dev-2.35-r1.apk
    && apk add --force-overwrite glibc-i18n-2.35-r1.apk
    && rm glibc-i18n-2.35-r1.apk
    victor
        3
    victor  
       142 天前   ❤️ 1
    # 获取 glibc 的最新版本号
    RUN LATEST_VERSION=$(curl -s https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') && \
    curl -L -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-${LATEST_VERSION}.apk && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-bin-${LATEST_VERSION}.apk && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-dev-${LATEST_VERSION}.apk && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-i18n-${LATEST_VERSION}.apk && \
    apk add --force-overwrite glibc-${LATEST_VERSION}.apk && \
    rm glibc-${LATEST_VERSION}.apk && \
    apk add --force-overwrite glibc-bin-${LATEST_VERSION}.apk && \
    rm glibc-bin-${LATEST_VERSION}.apk && \
    apk add --force-overwrite glibc-dev-${LATEST_VERSION}.apk && \
    rm glibc-dev-${LATEST_VERSION}.apk && \
    apk add --force-overwrite glibc-i18n-${LATEST_VERSION}.apk && \
    rm glibc-i18n-${LATEST_VERSION}.apk
    KINGWAY
        4
    KINGWAY  
    OP
       142 天前
    @victor 跑了下没有成功,出错信息如下:

    106 |
    107 | >>> RUN LATEST_VERSION=$(curl -s https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') && \
    108 | >>> curl -L -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    109 | >>> curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-${LATEST_VERSION}.apk && \
    110 | >>> curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-bin-${LATEST_VERSION}.apk && \
    111 | >>> curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-dev-${LATEST_VERSION}.apk && \
    112 | >>> curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-i18n-${LATEST_VERSION}.apk && \
    113 | >>> apk add --force-overwrite glibc-${LATEST_VERSION}.apk && \
    114 | >>> rm glibc-${LATEST_VERSION}.apk && \
    115 | >>> apk add --force-overwrite glibc-bin-${LATEST_VERSION}.apk && \
    116 | >>> rm glibc-bin-${LATEST_VERSION}.apk && \
    117 | >>> apk add --force-overwrite glibc-dev-${LATEST_VERSION}.apk && \
    118 | >>> rm glibc-dev-${LATEST_VERSION}.apk && \
    119 | >>> apk add --force-overwrite glibc-i18n-${LATEST_VERSION}.apk && \
    120 | >>> rm glibc-i18n-${LATEST_VERSION}.apk
    121 |
    --------------------
    ERROR: failed to solve: process "/bin/sh -c LATEST_VERSION=$(curl -s https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest | grep -oP '\"tag_name\": \"\\K(.*)(?=\")') && curl -L -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-${LATEST_VERSION}.apk && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-bin-${LATEST_VERSION}.apk && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-dev-${LATEST_VERSION}.apk && curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-i18n-${LATEST_VERSION}.apk && apk add --force-overwrite glibc-${LATEST_VERSION}.apk && rm glibc-${LATEST_VERSION}.apk && apk add --force-overwrite glibc-bin-${LATEST_VERSION}.apk && rm glibc-bin-${LATEST_VERSION}.apk && apk add --force-overwrite glibc-dev-${LATEST_VERSION}.apk && rm glibc-dev-${LATEST_VERSION}.apk && apk add --force-overwrite glibc-i18n-${LATEST_VERSION}.apk && rm glibc-i18n-${LATEST_VERSION}.apk" did not complete successfully: exit code: 2
    victor
        5
    victor  
       142 天前
    FROM alpine:latest

    # 添加 sgerrand 的公钥
    RUN curl -L -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub

    # 获取 glibc 的最新版本号并下载相关文件
    RUN LATEST_VERSION=$(curl -s https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') && \
    echo "Latest version is $LATEST_VERSION" && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-${LATEST_VERSION}.apk && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-bin-${LATEST_VERSION}.apk && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-dev-${LATEST_VERSION}.apk && \
    curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${LATEST_VERSION}/glibc-i18n-${LATEST_VERSION}.apk

    # 安装 glibc 相关包并清理
    RUN apk add --force-overwrite glibc-*.apk && \
    rm glibc-*.apk

    # 运行时所需配置
    RUN /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8 || true

    # 设置环境变量
    ENV LD_LIBRARY_PATH=/usr/glibc-compat/lib
    ENV LANG=en_US.UTF-8
    victor
        6
    victor  
       142 天前
    你跟 gpt 对话能更快的到答案,比这里问方便多了
    Mikex88
        7
    Mikex88  
       142 天前
    单个执行有利于 docker build 缓存吧?
    sunny352787
        8
    sunny352787  
       142 天前
    @KINGWAY 好家伙,你跟这 GPT 呢?
    KINGWAY
        9
    KINGWAY  
    OP
       142 天前
    chatgpt 上拿到的很多通不过实际编译啊, 就比如你最后这段一样失败了。
    wonderfulcxm
        10
    wonderfulcxm  
       142 天前 via iPhone
    @sunny352787 哈哈,确实像我跟 gpt 的对话
    KINGWAY
        11
    KINGWAY  
    OP
       141 天前
    @wonderfulcxm : )
    其实也还好啦,最起码下面这段我用上了
    # 安装 glibc 相关包并清理
    RUN apk add --force-overwrite glibc-*.apk && \
    rm glibc-*.apk
    artiga033
        12
    artiga033  
       141 天前
    说到底这并不是一个 docker 问题而是一个 shell script 问题

    ```sh
    curl 'https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest' | sed -rn 's/^.*browser_download_url.*( https:.*\.apk).*$/\1/p' | xargs -n 1 curl -LO
    ```
    KINGWAY
        13
    KINGWAY  
    OP
       141 天前
    @artiga033 你这个无效, 不过可以编译下去,估计稍微排查就可以了。


    curl 'https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest' | sed -rn 's/^.*browser_download_url.*( https:.*\.apk).*$/\1/p' | xargs -n 1 curl -LO
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 10603 0 10603 0 0 69205 0 --:--:-- --:--:-- --:--:-- 69756
    curl: no URL specified!
    curl: try 'curl --help' or 'curl --manual' for more information
    ko20
        14
    ko20  
       141 天前
    这种 bash/shell 问题,问 gpt 很快的,不过可能需要你来回调试两三次,就 ok 了。问得具体一点,能得到更准确的回答
    KINGWAY
        15
    KINGWAY  
    OP
       141 天前
    @ko20 是的,修改为
    RUN curl -s 'https://api.github.com/repos/sgerrand/alpine-pkg-glibc/releases/latest' | grep 'browser_download_url' | sed -E 's/.*"([^"]+)".*/\1/' | xargs -n 1 curl -LO
    就没问题了,几个文件也都拉下来了
    anubu
        16
    anubu  
       141 天前
    2 个 RUN 就是 2 层了,后面的 rm 应该没有精简镜像的效果了。常规实践中下载并清理的场景应该放在同一层。
    KINGWAY
        17
    KINGWAY  
    OP
       141 天前
    @anubu 其实还行,我跑了下编译没问题, 也可以拉起来, 准备提交 PR

    https://github.com/Kingwayer/dockerfiles/blob/master/alpine/5/Dockerfile
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5807 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 06:12 · PVG 14:12 · LAX 22:12 · JFK 01:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.