将命令行参数传递给 AWS Fargate 容器

Passing Command Line arguments to AWS Fargate Container

我们正在使用 Terraform 将 AWS ECS/Fargate 容器动态启动到 运行 Spring 启动应用程序,并且需要将几个命令行参数传递到应用程序中。可用文档似乎表明,执行此操作的正确方法是在 Terraform 容器定义 JSON 中定义一个“命令”块,并在其中指定一个或多个参数。这是我当前的容器定义:

 [{
"name": "${environment}-${app_name}",
"image": "${app_image}",
"cpu": ${fargate_cpu},
"memory": ${fargate_memory},
"networkMode": "awsvpc",
"command": [
   "--server.port",
   "${app_port}"
],
"logConfiguration": {
    "logDriver": "awslogs",
    "options": {
      "awslogs-group": "${environment}-${app_name}",
      "awslogs-region": "${aws_region}",
      "awslogs-stream-prefix": "ecs"
    }
},
"portMappings": [
  {
    "containerPort": ${app_port},
    "hostPort": ${app_port}
  }
]}]

你会注意到我已经用参数 --server.port 定义了 命令块 并设置了它的值到 app_port 变量。但是,在部署时,此参数 未被 由容器内的 Spring 引导应用程序 运行 获取,而是使用默认端口。

我的问题很简单:应该如何在 Fargate 容器内为应用程序 运行 指定命令行参数?

似乎是因为您正在传递系统的语法 属性。 在推送云之前,您可以在本地系统上轻松测试它。

docker run -it --net host --rm sprintbootimage java -jar /data/hello-world-0.1.0.jar –server.port=8081

这行不通,您可以阅读有关 的更多详细信息,要使其正常工作,您只需简单地传递 -Dserver.port=8081

docker run -it --net host --rm hello-world java -jar -Dserver.port=8081 /data/hello-world-0.1.0.jar

输出

2020-07-13 03:31:59.316  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-13 03:31:59.631  INFO 1 --- [ main] : Tomcat started on port(s): 8081 (http) with context path ''
2020-07-13 03:31:59.636  INFO 1 --- [           main] c.d.hello.Application                    : Started Application in 4.031 seconds (JVM running for 4.875)

-Dproperty=value

Sets a system property value. The property variable is a string with no spaces that represents the name of the property. The value variable is a string that represents the value of the property. If value is a string with spaces, then enclose it in quotation marks (for example -Dfoo="foo bar").

所以CMD会变成

  
command: ["java", "-Dserver.port=${app_port}", "-jar", "/data/hello-world-0.1.0.jar"]