Application is not running after deployment on production environment using dockerized UberJAR

Hello team,

i deployed my cuba app on ubuntu 18.04 server by following the manual https://doc.cuba-platform.com/manual-6.8/manual.html#single_jar_deployment
when the application didn’t start (basically freezed trying to get the login page) i checked thee app.log file and i found the below error:
Caused by: com.haulmont.cuba.core.sys.DbInitializationException: Error connecting to database: Cannot create PoolableConnectionFactory (Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.)

Apparently it’s trying to connect to the wrong host, as i configured the jetty-env.xml to connect to postgres:5432 not localhost:5432. below are the details of my deployment:

  • jetty-env.xml:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg/>
        <Arg>jdbc/CubaDS</Arg>
        <Arg>
            <New class="org.apache.commons.dbcp2.BasicDataSource">
                <Set name="driverClassName">org.postgresql.Driver</Set>
                <Set name="url">jdbc:postgresql://postgres/vp</Set>
                <Set name="username">postgres</Set>
                <Set name="password">**********</Set>
                <Set name="maxIdle">2</Set>
                <Set name="maxTotal">20</Set>
                <Set name="maxWaitMillis">5000</Set>
            </New>
        </Arg>
    </New>
</Configure>
  • Dockerfile:
FROM openjdk:8
COPY . /usr/src/vp
RUN mkdir /usr/src/imagesDir /usr/src/subReportsDir
CMD java -Xms1024M -Xmx2048M -Dapp.home=/usr/src/vp/home -jar /usr/src/vp/app.jar

-docker-compose file on the production server:

version: '3.1'
services:
  postgres:
    image: postgres:10.5
    volumes:
      - ~/dbdata:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ********
      POSTGRES_DB: vp
    ports:
      - "5432:5432"
    networks:
      - vp-network
  web:
    image: vpay
    volumes:
      - ~/dataDir:/usr/src/vp/home/app-core/work
      - ~/logsDir:/usr/src/vp/home/logs
      - ~/imagesDir:/usr/src/imagesDir
      - ~/subReportsDir:/usr/src/subReportsDir
    ports:
      - "80:8080"
    networks:
      - vp-network
networks:
  vp-network:

do you have any idea why the deployed application is trying to connect to localhost:5432 instead of postgres:5432?

i have done a lot of investigations and now i am sure that jetty-env.xml is for some reason not picked up by the build uberjar gradle task. i was able to work around this issue by setting the database host name in the context.xml to the production database hostname and ran the uberjar task and the deployment worked fine. can you please let me know if there is a real issue with the uber jar deployment or there is something i misunderstood.

hi,

I think it is the latter one. I’ve used the UberJar approach with jetty-env.xml multiple times, without problems. Did you created the jetty-env.xml via studio? Can you show the build.gradle file?

Bye
Mario

Hi Mario,

I created the jetty-env.xml using the studio, please find the build.gradle file below:


buildscript {
    ext.cubaVersion = '6.9.5'
    repositories {
        maven {
            url 'https://repo.cuba-platform.com/content/groups/work'
            credentials {
                username(rootProject.hasProperty('repoUser') ? rootProject['repoUser'] : 'cuba')
                password(rootProject.hasProperty('repoPass') ? rootProject['repoPass'] : 'cuba123')
            }
        }
        maven {
            url 'http://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts'
        }
        
    }
    dependencies {
        classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
    }
}

def modulePrefix = 'app'

def globalModule = project(":${modulePrefix}-global")
def coreModule = project(":${modulePrefix}-core")
def guiModule = project(":${modulePrefix}-gui")
def webModule = project(":${modulePrefix}-web")
def webThemesModule = project(":${modulePrefix}-web-themes")

def servletApi = 'javax.servlet:javax.servlet-api:3.1.0'


apply(plugin: 'idea')
apply(plugin: 'cuba')

cuba {
    artifact {
        group = 'com.company.vp'
        version = '0.1'
        isSnapshot = true
    }
    tomcat {
        dir = "$project.rootDir/deploy/tomcat"
    }
    ide {
        vcs = 'Git'
    }
}

dependencies {
    appComponent("com.haulmont.cuba:cuba-global:$cubaVersion")
    appComponent("de.diedavids.cuba.dataimport:dataimport-global:0.5.3")

}

def postgres = 'org.postgresql:postgresql:9.4.1212'

configure([globalModule, coreModule, guiModule, webModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'idea')
    apply(plugin: 'cuba')

    dependencies {
        testCompile('junit:junit:4.12')
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }
}

configure(globalModule) {

    dependencies {
        compile('net.sf.jasperreports:jasperreports:6.7.0')
    }
    task enhance(type: CubaEnhancing)

    
}

configure(coreModule) {

    configurations {
        jdbc
        dbscripts
    }

    dependencies {
        compile(globalModule)
        provided(servletApi)
        jdbc(postgres)
        testRuntime(postgres)
        compile('net.sf.jasperreports:jasperreports:6.7.0')
        compile('commons-beanutils:commons-beanutils:1.9.3')
        compile('commons-collections:commons-collections:3.2.2')
        compile('commons-digester:commons-digester:2.1')
        compile('commons-logging:commons-logging:1.1.1')
        compile('com.adobe.xmp:xmpcore:5.1.1')
        compile('org.jfree:jcommon:1.0.23')
        compile('org.jfree:jfreechart:1.0.19')
        compile('org.apache.poi:poi:3.15')
        compile('net.sf.barcode4j:barcode4j:2.1')
        compile('org.apache.poi:poi-ooxml:3.15')
        compile('net.sf.jasperreports:jasperreports-fonts:6.0.0')
        compile('org.apache.commons:commons-pool2:2.4.2')
        compile('com.lowagie:itext:2.1.7.js6')
        compile('org.olap4j:olap4j:0.9.7.309-JS-3')

    }

    task cleanConf(description: 'Cleans up conf directory') {
        doLast {
            def dir = new File(cuba.tomcat.dir, "/conf/${modulePrefix}-core")
            if (dir.isDirectory()) {
                ant.delete(includeemptydirs: true) {
                    fileset(dir: dir, includes: '**/*', excludes: 'local.app.properties')
                }
            }
        }
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}-core"
        appJars(modulePrefix + '-global', modulePrefix + '-core')
    }

    task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
        dbms = 'postgres'
        host = '10.0.22.44'
        dbName = 'vp'
        dbUser = 'postgres'
        dbPassword = 'xxxxxxxx'
    }

    task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
        dbms = 'postgres'
        host = '10.0.22.44'
        dbName = 'vp'
        dbUser = 'postgres'
        dbPassword = 'xxxxxxxx'
    }
}

configure(guiModule) {
    dependencies {
        compile(globalModule)

    }

    task deployConf(type: Copy) {
        from file('src')
        include "com/company/vp/**"
        into "$cuba.tomcat.dir/conf/${modulePrefix}"
    }
}

configure(webModule) {
    configurations {
        webcontent
    }

    dependencies {
        provided(servletApi)
        compile(guiModule)

    }
    
    task webArchive(type: Zip) {
        from file("$buildDir/web")
        from file('web')
        classifier = 'web'
    }

    artifacts {
        archives webArchive
    }

    task deployConf(type: Copy) {
        from file('src')
        include "com/company/vp/**"
        into "$cuba.tomcat.dir/conf/${modulePrefix}"
    }

    task clearMessagesCache(type: CubaClearMessagesCache) {
        appName = "${modulePrefix}"
    }
    deployConf.dependsOn clearMessagesCache

    task cleanConf(description: 'Cleans up conf directory') {
        doLast {
            def dir = new File(cuba.tomcat.dir, "/conf/${modulePrefix}")
            if (dir.isDirectory()) {
                ant.delete(includeemptydirs: true) {
                    fileset(dir: dir, includes: '**/*', excludes: 'local.app.properties')
                }
            }
        }
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}"
        appJars(modulePrefix + '-global', modulePrefix + '-gui', modulePrefix + '-web')
    }
    task buildScssThemes(type: CubaWebScssThemeCreation)
    task deployThemes(type: CubaDeployThemeTask, dependsOn: buildScssThemes)
    assemble.dependsOn buildScssThemes
}

configure(webThemesModule) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')

    appModuleType = 'web-themes'

    buildDir = file('../build/scss-themes')

    sourceSets {
        main {
            java {
                srcDir '.'
            }
            resources {
                srcDir '.'
            }
        }
    }
}





task undeploy(type: Delete, dependsOn: ":${modulePrefix}-web:cleanConf") {
    delete("$cuba.tomcat.dir/shared")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}-core")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}")
}

task restart(dependsOn: ['stop', ":${modulePrefix}-core:deploy", ":${modulePrefix}-web:deploy"], description: 'Redeploys applications and restarts local Tomcat') {
    doLast {
        ant.waitfor(maxwait: 6, maxwaitunit: 'second', checkevery: 2, checkeveryunit: 'second') {
            not {
                socket(server: 'localhost', port: '8787')
            }
        }
        start.execute()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.3.1'
}

apply from: 'extra.gradle'

task buildUberJar(type: CubaUberJarBuilding) {
    singleJar = true
    logbackConfigurationFile = 'etc/uber-jar-logback.xml'
    coreJettyEnvPath = 'modules/core/web/META-INF/jetty-env.xml'
    appProperties = ['cuba.automaticDatabaseUpdate' : true, 'jasper.imagesDir' : '/usr/src/imagesDir', 'jasper.subReportsDir' : '/usr/src/subReportsDir']
}

Hi Mario,

that issues is resolved, i am really note sure how it was not working and now i its working now.