mongodb - Jar 在主机上运行,​​但在 docker 容器中失败

标签 mongodb docker jar docker-container

我有一个 jar 在我的主机上运行良好;具体来说,当我运行时

java -jar myjar.jar

我得到了预期的输出:

[2018-12-05 16:46:53.917] boot - 21252 INFO [main] --- Application: No active profile set, falling back to default profiles: default

[2018-12-05 16:47:00.855] boot - 21252 INFO [main] --- Application: Started Application in 8.176 seconds (JVM running for 9.106) This is the Core Data Micro Service.

[2018-12-05 16:47:00.856] boot - 21252 INFO [main] --- Application: Registering to queue for events

[2018-12-05 16:47:00.857] boot - 21252 INFO [main] --- ZeroMQEventSubscriber: Getting subscriber, listening to tcp://localhost:5565

[2018-12-05 16:47:00.915] boot - 21252 INFO [main] --- ZeroMQEventSubscriber: Watching for new Event messages...



但是,我尝试在 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

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

最佳答案

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

但是,当您这样做时,您会遇到无法识别 edgex-mongo 的问题。因为它与这个容器没有任何联系。 edgex-mongo位于桥接网络中,需要您使用以下命令将 spring 容器添加到此网络:
docker run --network edgex--network [image]
我希望这可以帮助你

关于mongodb - Jar 在主机上运行,​​但在 docker 容器中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53666086/

相关文章:

linux - Mongo 连接溢出

python - python3 flask 应用程序中的 pymongo 导入错误

amazon-web-services - AWS Elastic Beanstalk-Docker与Packer

docker - 如何使用 Docker 运行 Flutter Web 应用程序?

ruby-on-rails - Mongoid has_many 和多个belongs_to 关联

MongoDB - 循环遍历每个数据库并运行命令

没有 TLS 验证的 docker-machine

java - sun.misc.InvalidJarIndexException : Invalid index when importing from com. * Jython 独立包中

Android Jar 库

java - 如何使Jenkins认为Maven -SNAPSHOT jar Artifact 的两个不同版本与连续交付的一部分相同?