参数化 Docker Image 前缀 [英] Parameterising Docker Image prefix
本文介绍了参数化 Docker Image 前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下内容的 Dockerfile
I have a Dockerfile which has the following content
# Build
FROM ${ECR_PREFIX}/maven:3.6.3-jdk-11 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
# Package
FROM ${ECR_PREFIX}/openjdk:11-jre-slim
COPY --from=build /home/app/target/application.jar application.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "application.jar"]
我尝试使用
export PREFIX=${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com
docker build -t backend --build-arg ECR_PREFIX=$PREFIX .
但这行不通.由于各种原因,我真的尽量不硬编码前缀,尤其是 ${AWS::AccountId} 的一部分.
but this would not work. I am really trying not to hard code the prefix especially ${AWS::AccountId} part of it because of various reasons.
这里有什么建议吗?
推荐答案
您必须在 Dockerfile 中显式声明 ARG
.如果您使用 ARG
值在 FROM
行 中,则 ARG
需要位于任何 FROM
之前.(其他 ARG
需要在使用它们的每个构建阶段重复.)
You have to explicitly declare the ARG
in your Dockerfile. If you use an ARG
value in a FROM
line then the ARG
needs to come before any FROM
. (Other ARG
s need to be repeated in each build stage that uses them.)
ARG REGISTRY=docker.io
FROM ${REGISTRY}/maven:3.6.3-jdk-11 AS build
REGISTRY=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
docker build
-t $REGISTRY/backend:$(git rev-parse HEAD)
--build-arg REGISTRY=$REGISTRY
.
这篇关于参数化 Docker Image 前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文