简体   繁体   English

Docker 文件 ENV 变量替换

[英]Docker file ENV variable substitution

I have a case where docker image/container for same java spring boot app is built on separate servers (DEV server, QA server, PROD server).我有一个案例,在不同的服务器(DEV 服务器、QA 服务器、PROD 服务器)上构建相同 java spring boot 应用程序的 docker 图像/容器。 However, in my Dockerfile, I have a ENTRYPOINT line that looks as follows:但是,在我的 Dockerfile 中,我有一个 ENTRYPOINT 行,如下所示:

ENTRYPOINT ["java","-jar","-DskipTests","-Dspring.profiles.active=development","some.jar",">some.log","2>someerror.log"]

But I need a way to pass on the correct "-Dspring.profiles.active=development" for when I am in QA or PROD.但是当我在 QA 或 PROD 中时,我需要一种方法来传递正确的“-Dspring.profiles.active=development”。

I noticed that if I issue this docker command on the server (QA for example):我注意到如果我在服务器上发出这个 docker 命令(例如 QA):

docker exec <container id> env | grep V 

then I can clearly see that there is an environment variable defined as:然后我可以清楚地看到有一个环境变量定义为:

ZZ_ENVIRONMENT=QA

Seeing this, I am looking for a way such that in the Dockerfile, maybe I can use this info such that if:看到这一点,我正在寻找一种方法,以便在 Dockerfile 中,也许我可以使用此信息,如果:

if ZZ_ENVIRONMENT=QA then 
ENTRYPOINT ["java","-jar","-DskipTests","-Dspring.profiles.active=qa","some.jar",">some.log","2>someerror.log"]

if ZZ_ENVIRONMENT=PROD then 
ENTRYPOINT ["java","-jar","-DskipTests", Dspring.profiles.active=production","some.jar",">some.log","2>someerror.log"]

else
ENTRYPOINT ["java","-jar","-DskipTests","-Dspring.profiles.active=development","some.jar",">some.log","2>someerror.log"]

Is this possible?这可能吗? and if so I would appreciate example that I can follow.如果是这样,我会很感激我可以遵循的例子。 If this is the wrong way to do this, please advise the best way to do this.如果这是执行此操作的错误方法,请建议执行此操作的最佳方法。 Please note: I do not have sudo access to the servers, just user access.请注意:我没有对服务器的 sudo 访问权限,只有用户访问权限。

You can pass the environment variables while starting the docker container in the docker run command.您可以在 docker run 命令中启动 docker 容器时传递环境变量。

For example例如

docker run --env ZZ_ENVIRONMENT=PROD --env ENV_VAR2=value2 <docker_image_name>

If you have a list of environment variables then you can simply create a .env file and add all the varibales into it and in the docker run command can refer the env file as below如果您有一个环境变量列表,那么您可以简单地创建一个 .env 文件并将所有变量添加到其中,然后在 docker run 命令中可以引用 env 文件,如下所示

docker run --env-file .env <docker_image_name>

where your .env file counld be as你的 .env 文件可能是

ZZ_ENVIRONMENT=PROD
ENV_VAR2=value2

The best way to go about this would be instead of this logic in DockerFile, you could use the SPRING_PROFILES_ACTIVE variable directly or in your enviornment file.解决此问题的最佳方法是在 DockerFile 中代替此逻辑,您可以直接或在您的环境文件中使用 SPRING_PROFILES_ACTIVE 变量。

Docker File Docker 文件

ENTRYPOINT ["java","-jar","-DskipTests""some.jar",">some.log","2>someerror.log"]

Docker Run运行

docker run -e "SPRING_PROFILES_ACTIVE=qa" -p 8080:8080 -t test-image:v1

docker run -e "SPRING_PROFILES_ACTIVE=production" -p 8080:8080 -t test-image:v1

Alternativetly, you could explore docker compose like below (more maintainable approach)或者,您可以像下面一样探索 docker compose(更易于维护的方法)

docker-compose.yml docker-compose.yml

version: '3'
services:
  spring-boot-service:
    image: test-image:v1
    env_file:
      - spring-boot-service.env
    ports:
      - 8080:8080

spring-boot-service.env spring-boot-service.env

SPRING_PROFILES_ACTIVE=production

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM