Jar在主机上运行,​​但在Docker容器中失败

时间:2018-12-07 08:50:36

标签: mongodb docker jar docker-container

我有一个jar,可以在主机上正常运行;具体来说,当我运行

java -jar myjar.jar

我得到了预期的输出:

  

[2018-12-05 16:46:53.917]引导-21252 INFO [main] ---应用程序:未设置活动配置文件,后退到默认配置文件:默认

     

[2018-12-05 16:47:00.855]引导-21252 INFO [main] ---应用程序:在8.176秒内启动了应用程序(JVM运行9.106)   这是核心数据微服务。

     

[2018-12-05 16:47:00.856]引导-21252 INFO [main] ---应用程序:注册以排队等待事件

     

[2018-12-05 16:47:00.857]引导-21252 INFO [main]--ZeroMQEventSubscriber:获取订阅者,侦听tcp:// localhost:5565

     

[2018-12-05 16:47:00.915]引导-21252信息[main] --- ZeroMQEventSubscriber:正在监视新的事件消息...

但是,然后,我尝试在docker容器中运行相同的jar。所以我创建这样的图像:

FROM openjdk:8-jdk-alpine
COPY myjar.jar /opt/spring-cloud/lib/
ENTRYPOINT ["/usr/bin/java"]
CMD ["-jar", "/opt/spring-cloud/lib/myjar.jar"]
EXPOSE 48080

并运行它:

sudo docker run [ID]

但是这次,我从容器日志中得到了这个异常(这只是异常的一部分,因为它太大了,但是如果需要的话,我可以全部显示):

[2018-12-07 08:30:31.447] boot - 1  INFO [main] --- Application: No active profile set, falling back to default profiles: default

[2018-12-07 08:32:35.423] boot - 1 ERROR [main] --- SpringApplication: Application startup failed

...

...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readingControllerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.edgexfoundry.dao.ValueDescriptorRepository org.edgexfoundry.controller.impl.ReadingControllerImpl.valDescRepos; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'valueDescriptorRepository': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 120000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 120000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)

...

...

Caused by: com.mongodb.MongoTimeoutException: Timed out after 120000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
    at com.mongodb.BaseCluster.getServer(BaseCluster.java:82)
    at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:664)
    at com.mongodb.DBTCPConnector.access$500(DBTCPConnector.java:40)
    at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:513)
    at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:456)
    at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:415)
    at com.mongodb.DBCollectionImpl.createIndex(DBCollectionImpl.java:378)
    at com.mongodb.DBCollection.createIndex(DBCollection.java:597)
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:142)
    ... 57 more

Mongo已通过docker-compose在另一个容器中启动(连同其他容器中的其他服务):

ps aux | grep mongo

root     16226  0.0  0.0   4340   768 ?        Ss   10:27   0:00 /bin/sh -c /edgex/mongo/config/launch-edgex-mongo.sh
root     16292  0.0  0.0   4340   764 ?        S    10:27   0:00 /bin/sh /edgex/mongo/config/launch-edgex-mongo.sh
root     16293  0.5  0.3 961168 61400 ?        SLl  10:27   0:05 mongod --smallfiles

这是docker-compose文件:

version: '3'

services:
  volume:
    image: edgexfoundry/docker-edgex-volume:0.6.0
    container_name: edgex-files
    networks:
      - edgex-network
    volumes:
      - db-data:/data/db
      - log-data:/edgex/logs
      - consul-config:/consul/config
      - consul-data:/consul/data

  mongo:
    image: edgexfoundry/docker-edgex-mongo:0.6.0
    ports:
      - "27017:27017"
    container_name: edgex-mongo
    hostname: edgex-mongo
    networks:
      - edgex-network
    volumes:
      - db-data:/data/db
      - log-data:/edgex/logs
      - consul-config:/consul/config
      - consul-data:/consul/data
    depends_on:
      - volume

.... more services...

 networks:
   edgex-network:
     driver: "bridge

以及mongo db配置属性:

spring.data.mongodb.username=core
spring.data.mongodb.password=password
spring.data.mongodb.database=coredata
#change to localhost when running locally during development 
# (or set hosts to point edgex-mongo to the mongo host
spring.data.mongodb.host=localhost
#spring.data.mongodb.host=edgex-mongo
spring.data.mongodb.port=27017
spring.data.mongodb.connectTimeout=120000
spring.data.mongodb.socketTimeout=60000
spring.data.mongodb.maxWaitTime=120000
spring.data.mongodb.socketKeepAlive=true

任何想法可能出了什么问题?

1 个答案:

答案 0 :(得分:1)

这里有两件事情出了问题,首先是春季尝试在localhost上连接到mongodb,在docker内这不起作用,因为localhost引用了当前容器,当然没有mongodb可用。要解决此问题,您必须注释掉这一行,并取消注释将主机列为edgex-mongo的下一行,该主机与mongodb容器的主机名相对应,因此spring知道可以连接到该容器。

但是,当您执行此操作时,您会遇到无法识别edgex-mongo的问题,因为它与该容器没有连接。 edgex-mongo在桥接网络内部,要求您使用以下命令将弹簧容器添加到该网络:

docker run --network edgex--network [image]

希望对您有帮助

相关问题