Docker Compose(TOMCAT + POSTGRES) - Spring Boot应用程序:容器之间的连接问题

时间:2018-04-10 10:26:23

标签: postgresql docker tomcat spring-boot devops

我有一个与postgresql连接的spring-boot服务。当我使用TOMCAT 9.0.6运行应用程序和postgresql的docker容器(只是容器中的DB)时,应用程序设置正常。

但是当我尝试使用docker compose运行应用程序时,它不会显示任何内容。

我的docker-compose.yml

version: "3.3"

services:

  webserver:
    build: 
      context: ./appserver
    image: webserver
    container_name: webserver
    depends_on:
      - mypostgres
    ports:
      - "8080:8080" 
      - "8000:8000"
    networks:
      - front-tier
      - back-tier


  mypostgres:
    build:
      context: ./database
    image: postgresdb
    container_name: postgresdb
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=ggal1701
    volumes:
      - type: volume
        source: psql_data
        target: /var/lib/postgresql/data 
    networks:
      - back-tier


volumes:
  psql_data:

networks:
  front-tier:
  back-tier:

我的dockerfile进入./database:

FROM postgres:9.5

# Copy the database initialize script: 
# Contents of /docker-entrypoint-initdb.d are run on postgres startup
ADD  docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/

# Default values for passwords and database name. Can be overridden on docker run
# ENV MYSQL_ROOT_PASSWORD=my-secret-pw # Not defaulted for security reasons!
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=postgres

我的dockerfile进入./appserver:

FROM tomcat:9.0.6-jre8

# tomcat-users.xml sets up user accounts for the Tomcat manager GUI
ADD tomcat/tomcat-users.xml $CATALINA_HOME/conf/

# ADD tomcat/catalina.sh $CATALINA_HOME/bin/
ADD tomcat/run.sh $CATALINA_HOME/bin/run.sh
RUN chmod +x $CATALINA_HOME/bin/run.sh


# create mount point for volume with application
WORKDIR $CATALINA_HOME/webapps/
COPY dos.war .
# add MySQL JDBC driver jar
ADD tomcat/postgresql-42.2.2.jar $CATALINA_HOME/lib/

# add tomcat jpda debugging environmental variables
#ENV JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
ENV JPDA_ADDRESS="8000"
ENV JPDA_TRANSPORT="dt_socket"

# start tomcat8 with remote debugging
EXPOSE 8080
CMD ["run.sh"]

最后,我将Spring Boot配置到application.yml:

spring:

    # Configuración de la BASE DE DATOS
    datasource:
        dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
        url: jdbc:postgresql://mypostgres:5432/ggal1701
        username: postgres
        password: postgres
        maximum-pool-size: 5

    application:
        url: http://webserver:8080
    profiles:
        active: prod
    devtools:
        restart:
            enabled: false
        livereload:
            enabled: false

server:
    compression:
        enabled: true
        mime-types: text/html,text/xml,text/plain,text/css, application/javascript, application/json

我使用javamelody,当我转到路径时,它会正确加载它。

enter image description here

但是在应用程序路径中,它只会在选项卡中加载页面的标题,而不是其他内容。

Tomcat日志看起来没问题,除了这个(没有关于连接):

webserver     | 09:50:43.673 [main] WARN es.asd.GGAL1701.ApplicationWebXml - No Spring profile configured, running with default configuracion
webserver     | 09:50:44.466 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.exception-conversion-word]
webserver     | 09:50:44.467 [main] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.exception-conversion-word] not found - trying original name [logging.exception-conversion-word]. javax.naming.NameNotFoundException: Name [logging.exception-conversion-word] is not bound in this Context. Unable to find [logging.exception-conversion-word].
webserver     | 09:50:44.467 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.exception-conversion-word]
webserver     | 09:50:44.467 [main] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.exception-conversion-word] threw NamingException with message: Name [logging.exception-conversion-word] is not bound in this Context. Unable to find [logging.exception-conversion-word].. Returning null.

欢迎任何想法。数据库似乎工作正常,因为它适用于localhost上的tomcat。但是通过停泊Tomcat,它失败了。

编辑nº1:首先尝试在本地运行,然后将其上传到云端。这些失败是本地的。

编辑nº2:来自tomcat docker的一些新日志:

t ot@281078c55f64:/usr/local/tomcat/logs# cat localhost_access_log.2018-04-11.txt
172.18.0.1 - - [11/Apr/2018:08:28:28 +0000] "GET /dos HTTP/1.1" 302 -
172.18.0.1 - - [11/Apr/2018:08:28:28 +0000] "GET /dos/ HTTP/1.1" 200 964
172.18.0.1 - - [11/Apr/2018:08:28:28 +0000] "GET /assets/css/main-c4c779892e.css HTTP/1.1" 404 1111
172.18.0.1 - - [11/Apr/2018:08:28:28 +0000] "GET /app/vendor-511f175b57.js HTTP/1.1" 404 1101


root@281078c55f64:/usr/local/tomcat/logs# cat localhost.2018-04-11.log 
11-Apr-2018 08:27:57.363 INFO [main] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath
11-Apr-2018 08:28:14.958 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
11-Apr-2018 08:28:14.959 INFO [main] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
11-Apr-2018 08:28:14.960 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@267f6b32')


root@281078c55f64:/usr/local/tomcat/logs# cat catalina.2018-04-11.log  11-Apr-2018 08:27:54.581 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:  Apache Tomcat/9.0.6 11-Apr-2018 08:27:54.583 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:    Mar 5 2018 09:34:35 UTC 11-Apr-2018 08:27:54.583 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:  
9.0.6.0 11-Apr-2018 08:27:54.583 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:         Linux 11-Apr-2018 08:27:54.583 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:     
3.16.0-5-amd64 11-Apr-2018 08:27:54.583 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:    amd64 11-Apr-2018 08:27:54.583 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:       /usr/lib/jvm/java-8-openjdk-amd64/jre 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_162-8u162-b12-1~deb9u1-b12 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:      Oracle Corporation 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:   /usr/local/tomcat 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:   /usr/local/tomcat 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:
-Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 11-Apr-2018 08:27:54.584 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:
-Djava.protocol.handler.pkgs=org.apache.catalina.webresources 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.16] using APR version [1.5.2]. 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 11-Apr-2018 08:27:54.585 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 11-Apr-2018 08:27:54.588 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.0f  25 May 2017] 11-Apr-2018 08:27:54.654 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 11-Apr-2018 08:27:54.664 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 11-Apr-2018 08:27:54.675 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"] 11-Apr-2018 08:27:54.676 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 11-Apr-2018 08:27:54.677 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 406 ms 11-Apr-2018 08:27:54.703 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina] 11-Apr-2018 08:27:54.704 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.6 11-Apr-2018 08:27:54.723 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/dos.war] 11-Apr-2018 08:27:57.309 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 11-Apr-2018 08:28:14.765 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/dos.war] has finished in [20,041] ms 11-Apr-2018 08:28:14.767 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples] 11-Apr-2018 08:28:14.967 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [199] ms 11-Apr-2018 08:28:14.967 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT] 11-Apr-2018 08:28:15.022 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [55] ms 11-Apr-2018 08:28:15.022 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs] 11-Apr-2018 08:28:15.075 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [53] ms 11-Apr-2018 08:28:15.076 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager] 11-Apr-2018 08:28:15.138 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [62] ms 11-Apr-2018 08:28:15.138 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager] 11-Apr-2018 08:28:15.197 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [59] ms 11-Apr-2018 08:28:15.202 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 11-Apr-2018 08:28:15.212 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"] 11-Apr-2018 08:28:15.225 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 20547 ms 11-Apr-2018 08:28:25.201 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/log] 11-Apr-2018 08:28:25.256 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/log] has finished in [55] ms

我也尝试通过JAVA_OPTS传递参数,但是当我停靠tomcat时它仍然没有显示应用程序。

environment:
  - "JAVA_OPTS=-Ddatasource.jdbcUrl=jdbc:postgresql://mypostgres:5432/ggal1701 -Ddatasource.user=postgres -Ddatasource.password=postgres"

编辑nº3:连接到外部数据库(RDS AWS)也不起作用,而在本地,相同版本的Tomcat连接没有问题。所有麻烦似乎都来自于码头化的Tomcat。

1 个答案:

答案 0 :(得分:1)

我不明白为什么,但改变Tomcat的版本已经消失了。这不是使用早期版本或更高版本的问题。使用Tomcat版本8它可以根据所选的jre工作与否,但是jre版本也没有问题,因为在另一个版本中尝试相同的版本不起作用。

在tomhost中运行tomcat,我的应用程序运行正常。对于docker,使用相同的9.0.6版本,它没有。

我当前的码头工具 - 以防万一有人可能会发现它有用:

version: "3.3"

services:

  mytomcat:
    build:
      context: ./appserver
    image: webserver
    container_name: webserver
    hostname: webserver
    ports:
      - "8080:8080"
      - "8000:8000"
#    links:
#      - mypostgres:postgresdb
    networks:
      - main

  mypostgres:
    build:
      context: ./database
    image: postgresdb
    container_name: postgresdb
    hostname: postgresdb
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - main

volumes:
  db-data:

networks:
  main:

我没有给出答案作为解决方案,因为我认为不是。解决方案是它使用与localhost相同的版本;所以欢迎替代答案,解释或解决方案。