skip to Main Content

I’ve a spring boot app that I’m deploying in a Tomcat Docker.

I’ve many properties in application-deploy.properties like this:

#DB
spring.datasource.url=jdbc:mysql://${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

I want to get the variables from Docker. So, I’m trying this Dockerfile:

FROM tomcat:9.0.56
ARG SPRING_DATASOURCE_URL
ARG SPRING_DATASOURCE_USERNAME
ARG SPRING_DATASOURCE_PASSWORD
COPY setenv.sh /usr/local/tomcat/bin/setenv.sh
COPY /app/target/api.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]

and I’ve setenv.shlike this:

JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=deploy"
JAVA_OPTS="$JAVA_OPTS -DSPRING_DATASOURCE_URL=${SPRING_DATASOURCE_URL}"
JAVA_OPTS="$JAVA_OPTS -DSPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}"
JAVA_OPTS="$JAVA_OPTS -DSPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}"

Any idea of what I’m wrong and how can solve it? I want to pass the variables to application-deploy.properties when I build Docker.

2

Answers


  1. Use the ENV Statement.

    ENV SPRING_DATASOURCE_URL="MY_ACTUAL_URL"
    ENV SPRING_DATASOURCE_USERNAME="user"
    ENV SPRING_DATASOURCE_PASSWORD="pw"
    
    Login or Signup to reply.
  2. You can pass in the environment variables via

    docker run -e SPRING_DATASOURCE_URL="" -e SPRING_DATASOURCE_USERNAME="" --name {container-name} {image-name} 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search