postgresql - 使用 Tomcat 和 PostgreSQL 在 Docker 上部署应用程序

标签 postgresql tomcat docker networking docker-compose

我有一个 testApp.war,我想将其通过 docker 部署在 Tomcat 上(docker 在 10.0.2.157 上)。我的 testApp 只能与 postgres DB 和指定的用户 testUser 和密码 testUserPasswd 一起正常工作。我构建了这样一个结构:

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

所有这些文件的内容附在下面。我用命令启动我的容器:

docker-compose up -d

然而,当我在网络浏览器 ( http://10.0.2.157:8282/manager/html ) 上访问 Tomcat 并尝试启动我的 testApp 时,我得到了:

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

我做错了什么?你能帮我解决这个问题吗?


数据库/Docker文件

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

数据库/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>

网络/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>

最后是 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

最佳答案

好的,我找到了解决方案!

感谢@Tarun Lalwani的支持和建议。

我在 Tomcat 容器中的 application.yml 配置有误。 Docker 映射了我的容器的 IP 地址,但我不应该只写“10.0.2.157”,而是写容器名称。所以在我的示例中,我得到了如下所示:

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

然而,正确的解决方案是映射 postgres 容器的名称 (testAppdb),因此正确的 conf 是:

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

关于postgresql - 使用 Tomcat 和 PostgreSQL 在 Docker 上部署应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46346522/

相关文章:

ruby-on-rails - 如何在 Amazon Elastic Beanstalk 单容器 Docker 环境中运行 Rails 迁移和播种

sql - Postgres 检查 jsonb 数组是否不包含值返回空结果集

ruby-on-rails - 如何使用 ActiveRecord 返回父级唯一的最新子级

java - 在 Apache Tomcat 7 中部署 MyFaces CODI + Weld + JSF 时出错

java - 使用 Eclipse、Tomcat 和 Jersey 的 Java 中的 Restful WebServices

docker - 如何按标签删除 Docker 镜像,最好使用通配符?

Dockerfile - 创建非 root 用户几乎使图像大小增加了一倍

java - 添加 @Transactional 通过阻止 JPA 获取外键来破坏单元测试

postgresql - 为什么 PostgreSQL 中的新用户可以连接到所有数据库?

java - Tomcat - 在 VPS 上运行