0

I am totally new to Docker. Now I have this Spring MVC app (running on wildfly) which uses a MySQL DB which i want to Dockerize. I did create a Docker file whose contents are below

FROM jboss/wildfly
# ADD nummerreeks.war /opt/jboss/wildfly/standalone/deployments/
ADD test.war /home/Docker/app

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

And a docker-compose file which is below :

version: '2'
services:
  database:
    image: mysql:latest
    ports:
      - 3306
    environment:
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_USER: test
      MYSQL_PASSWORD: admin
      MYSQL_DATABASE: dbpass
  wildfly:
    image: bitnami/wildfly:latest
    ports:
      - '8080:8080'
      - '9990:9990' 
    links:
      - app
    volumes_from:
      - app
  app:
    image: test/demo
    volumes:
      - wars:/opt/jboss/wildfly/standalone/deployments/
    links:
      - database
volumes:
  wars:
    driver: local

Now when run this using :

docker run -t -p 8080:8080 --name dockerapp test

I get this DB exception,

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:289)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy56.listRequesterData(Unknown Source)
    at nl.naturalis.nrs.controller.NummerreeksController.getRequesterList(NummerreeksController.java:547)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

I believe its reading the db config from the DB connection values from the DB.config file in the application instead of the docker-compose file.

Anyone with any pointers to what am i doing wrong here?

Tim
  • 3
  • 3
  • I think you want an environment section under app where you can set environment variables to configure the database connection. If you're using spring boot you can set SPRING_DATASOURCE_URL. You'll then want to use `database` in place of `localhost` as I assume your local config defaults to `localhost`. If you're configuring the connection in code then you could write code to get an environment variable. – Ryan Dawson Aug 20 '18 at 13:00
  • @RyanDawson no its not a spring boot application. I found some similar solutions with spring boot but this is an spring mvc app which uses stand alone wildfly server to run and the persistent store is MySql. The db properties are in the db.properties file in the project classpath which read in the Spring configuration class using PropertyResouce annotation – Tim Aug 20 '18 at 13:21
  • isn't it possible to add a custom `db.properties` and build the image with Dockerfile containing `ADD db.properertins ` – Sathyajith Bhat Aug 20 '18 at 13:33
  • You may be able to use options from https://www.baeldung.com/properties-with-spring or https://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext – Ryan Dawson Aug 20 '18 at 13:34

0 Answers0