2
votes

We are using Terraform to dynamically spin up an AWS ECS/Fargate container to run a Spring Boot Application and there is a requirement to pass several command line arguments into the application. The available documentation seems to suggest that the correct way to do this is to define a "command" block in the Terraform container definition JSON and specify one or more arguments there. Here is my current container definition:

 [{
"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}
  }
]}]

You will note that I've defined the command block with the argument --server.port and set its value to the app_port variable. However on deployment this argument is not picked up by the Spring Boot application running inside the container and default port is used instead.

My question is simply: how should one go about specifying command line arguments to applications running inside Fargate containers?

1

1 Answers

0
votes

Seems like it because of syntax you are passing the system property. You can easily test this on your local system before pushing its cloud.

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

This will not work, you can read more details about this, to make it work you can just simple pass with -Dserver.port=8081

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

ouput

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)

Why do JVM arguments start with "-D"?

-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").

So the CMD will become

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