spring-bootdockerdocker-cmd

Using docker CMD instead of application.properties in hello world app


In my hello world app I'm using application properties to define 'name'. It works fine, but I want to define 'name' in docker CMD.

Service:

@Service
public class HelloService {

@Value("${name}")
private String name;

@GetMapping("/greeting")
    public String greeting() {
            return "Hello, " + name;
    }
}

application.properties:

name=Tommy

Dockerfile:

FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

I'm trying to add to Dockerfile something like this:

CMD ${name} Tommy

I run docker by:

docker run -p 8080:8080 hello-api:1.0

How can i do it right way?


Solution

  • In Dockerfile, use ENV instead of CMD like

    ENV NAME=Tommy
    

    Then you can directly use this environment variable in your application.properties:

    name = ${NAME}
    

    The rest of your implementation would be the same.