gpt4 book ai didi

amazon-web-services - EC2上的Docker,dockerfile中的RUN命令未读取环境变量

转载 作者:行者123 更新时间:2023-12-02 18:12:00 25 4
gpt4 key购买 nike

我在 AWS 上有两个 elastic-beanstalk 环境:开发和生产。我在每个实例上运行 glassfish 服务器,并要求在生产和开发环境中部署相同的应用程序包,而不需要两个不同的 .EAR两个实例的大小不同:开发有一个微型实例,而生产有一个中等实例,因此我需要为 glassfish 部署两个不同的配置文件,每个环境一个。

主要问题是该文件必须在服务器启动之前位于 glassfish 配置目录中,因此我认为在创建容器时移动它可能会更好。

当然每个环境都使用一个 docker 容器来托管 glassfish 实例,所以我的第一个想法是为 elastic-beanstalk 配置一个环境变量。在这种情况下

ypenvironment = dev

对于开发环境和
ypenvironment = pro

用于生产环境。然后在我的 DOCKERFILE 中,我将此语句放在 RUN 命令中:
    RUN if [ "$ypenvironment"="pro" ] ; then \
mv --force /var/app/GF_domain.xml /usr/local/glassfish/glassfish/domains/domain1/config/domain.xml ; \
elif [ "$ypenvironment"="dev" ] ; then \
mv --force /var/app/GF_domain.xml.dev /usr/local/glassfish/glassfish/domains/domain1/config/domain.xml ; \
fi

不幸的是,当启动完成时,两个 GF_domain 文件仍在 var/app 中。 .

然后我红色 RUN 命令在容器完全加载之前运行东西,可能缺少 Elastic Beanstalk 注入(inject)变量。所以我试图将代码移动到 ENTRYPOINT 指令。再次没有运气,容器启动失败。还尝试了
ENTRYPOINT ["command", "param"]

语法,但它没有给出一个
System error: exec: "if": executable file not found in $PATH

因此我被困住了。

最佳答案

你需要:

1/不使用入口点(或至少使用 sh -c 'if...' 语法):即用于运行时执行,而不是编译时镜像构建。

2/使用build-time variables ( --build-arg ) :

You can use ENV instructions in a Dockerfile to define variable values. These values persist in the built image.
However, often persistence is not what you want. Users want to specify variables differently depending on which host they build an image on.

A good example is http_proxy or source versions for pulling intermediate files. The ARG instruction lets Dockerfile authors define values that users can set at build-time using the --build-arg flag:


$ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .

在您的情况下,您的 Dockefile 应包括:
ENV ypenvironment

然后 docker build --build-arg ypenvironment=dev ... myDevImage
您将构建 2 个不同的镜像(基于相同的 Dockerfile)

I need to be able to use the same EAR package for dev and pro environments,



然后,您希望 ENTRYPOINT 在运行时根据环境变量的值移动文件。

您的 Dockerfile 仍然需要包括:

ENV yp环境

但是你需要运行你的一个图像
docker run -x ypenvironment=dev ...

确保您的脚本(由您的入口点引用)包含 if [ "$ypenvironment"="pro" ] ; then...你在你的问题中提到,加上你的应用程序的实际启动(在前台)。
您的脚本不需要立即退出,否则您的容器会在启动后立即切换到退出状态。

关于amazon-web-services - EC2上的Docker,dockerfile中的RUN命令未读取环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40427722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com