Docker. Spring set & get environment variable

I am trying to dock my Spring application.

Problem: I cannot get the environment variable in my Spring application from the docker container.

Spring config (2 options, checked separately)

<bean class="java.net.URI" id="dbUrl">
    <constructor-arg value="#{systemProperties['JDBC_CONNECTION_STRING']}"/>
</bean>

<bean class="java.net.URI" id="dbUrl">
    <constructor-arg value="#{systemEnvironment['JDBC_CONNECTION_STRING']}"/>
</bean>

also tried in java

URI dbUrl = URI.create(System.getProperty("JDBC_CONNECTION_STRING"));

My docker settings. Used docker-compose buildand docker-compose up, each time updating the values.

Docker-compose.yml

app:
  build: .
  command: catalina.sh run
  ports:
    - "8888:8080"
  links:
    - postgres
  volumes:
    - /usr/bin

postgres:
  image: postgres:9.5
  ports:
    - "5432"
  volumes:
  - /var/lib/postgresql/data

Dockerfile

FROM tomcat:jre8

ENV JDBC_CONNECTION_STRING 'postgres://postgres:password111@postgres:5432/mydb'

RUN ["rm", "-fr", "/usr/local/tomcat/webapps/ROOT"]
RUN apt-get update && apt-get install -y net-tools postgresql-client

COPY ./target/myapp.war /usr/local/tomcat/webapps/ROOT.war

CMD ["catalina.sh", "run"]

After connecting to the bash container, the setcommand does not display my variable. But echo $JDBC_CONNECTION_STRINGshows the meaning.

+4
source share
2 answers

java- java-, . java-, -Dkey = .

, tomcat, $JAVA_OPTS = "... -DJDBC_CONNECTION_STRING = $JDBC_CONNECTION_STRING"

+2

docker run -e JDBC_CONNECTION_STRING=WHATEVER

0

Source: https://habr.com/ru/post/1654016/


All Articles