如何评估 dockerfile 中的 ARG 并为其分配默认值?

问题描述 投票:0回答:2

基本上我想在我的 Dockerfile 中为

ARG
分配一个动态计算的默认值:

FROM node:20-alpine

ARG RELEASE_TAG=$(git describe --tags --always)

# Other commands...

因此,到目前为止,它不会插入

$(git describe --tags --always)
的值,只是将该值分配给
RELEASE_TAG
变量。

有什么办法可以实现这个目标吗?

bash docker dockerfile
2个回答
1
投票

您可以将

--build-arg
参数与
docker build

一起使用
export RELEASE_TAG=$(git describe --tags --always)
docker build --build-arg RELEASE_TAG=$RELEASE_TAG -t your_image_name .

在你的 Dockerfile 中:

FROM node:20-alpine

ARG RELEASE_TAG

这样,RELEASE_TAG 现在将具有

git describe --tags --always
输出的值。


0
投票

放入RUN即可达到预期效果:

FROM ubuntu

WORKDIR /app

COPY . .

RUN apt-get update && apt-get install -y git
RUN RELEASE_TAG=$(git describe --tags --always) &&\
    echo npm run build -- --version=$RELEASE_TAG

CMD sleep inf
© www.soinside.com 2019 - 2024. All rights reserved.