Deploying app on Docker with Tomcat and PostgreSQL

Viewed 5495

I have an testApp.war which I'd like to deploy on Tomcat through docker (docker is on 10.0.2.157). My testApp will work properly only with postgres DB and specified user testUser and password testUserPasswd. I built such a structure:

.
├── db
│   ├── Dockerfile
│   ├── pg_hba.conf
│   └── postgresql.conf
├── docker-compose.yml
└── web
    ├── context.xml
    ├── Dockerfile
    ├── software
    │   └── testApp.war
    └── tomcat-users.xml

Content of all these files are attached below. I start my containers with command:

docker-compose up -d

However when I go to Tomcat on webbrowser (http://10.0.2.157:8282/manager/html) and try to start my testApp I got:

HTTP Status 404 – Not Found

Type Status Report

Message /testApp/

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.20

What I'm doing wrong? Could you help me with this?


db/Dockerfile

FROM postgres:9.5
MAINTAINER riwaniak

ENV POSTGRES_USER testUser
ENV POSTGRES_PASSWORD testUserPasswd
ENV POSTGRES_DB testUser

ADD pg_hba.conf /etc/postgresql/9.5/main/
ADD postgresql.conf /etc/postgresql/9.5/main/

db/pg_hba.conf

local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5
host    all

db/postgresql.conf

listen_addresses='*'

web/context.xml

<?xml version="1.0" encoding="UTF-8"?>

<Context antiResourceLocking="false" privileged="true" >
<!--
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>

web/Dockerfile

FROM tomcat:8.5.20-jre8
MAINTAINER riwaniak

COPY ./software /usr/local/tomcat/webapps/

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

web/tomcat-users.xml

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">

  <role rolename="tomcat"/>
  <role rolename="admin-gui"/>
  <role rolename="manager-gui"/>

  <user username="tomcat" password="tomcat" roles="tomcat,admin-gui,manager-gui"/>

</tomcat-users>

and finally docker-compose.yml

version: '2'
services:
    testApp:
        build: ./web
        volumes:
          - /path/to/tomcat/folder/web/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
          - /path/to/tomcat/folder/web/context.xml:/usr/local/tomcat/webapps/HelpdeskApp/META-INF/context.xml
          - /path/to/tomcat/folder/web/context.xml:/usr/local/tomcat/webapps/host-manager/META-INF/context.xml
          - /path/to/tomcat/folder/web/context.xml:/usr/local/tomcat/webapps/manager/META-INF/context.xml
        ports:
          - "8282:8080"
        links:
          - testAppdb
        networks:
          - testAppnet

    testAppdb:
        build: ./db
        ports:
          - "5555:5432"
        volumes:
          - /srv/docker/postgresql:/var/lib/postgresql
          - /path/to/tomcat/folder/db/postgresql.conf:/etc/postgresql/9.5/main/postgresql.conf
          - /path/to/tomcat/folder/db/pg_hba.conf:/etc/postgresql/9.5/main/pg_hba.conf
        command: postgres -c config_file=/etc/postgresql/9.5/main/postgresql.conf
        networks:
          - testAppnet

networks:
    testAppnet:
      driver: bridge
      ipam:
        driver: default
        config:
          - subnet: 172.28.0.0/16
1 Answers

OK, I got the solution!

Thanks @Tarun Lalwani for supporting and suggestion.

I had wrong application.yml configuration in Tomcat container. Docker mapped ip addresses of my containers but I shouldn't write just "10.0.2.157" but name of containers. So in my example I've got smth like below:

(...)
environments:
    development:
        dataSource:
            dbCreate: update
            url: jdbc:postgresql://10.0.2.157:5432/helpdesk_dev
(...)

However right solution was to map name of postgres container (testAppdb), so correct conf is:

(...)
environments:
    development:
        dataSource:
            dbCreate: update
            url: jdbc:postgresql://testAppdb:5432/test_dev
(...)
Related