无法使用默认的docker网络连接到mongoDB容器

时间:2018-02-18 16:53:06

标签: mongodb docker docker-compose

我有以下docker-compose个文件:

搬运工-compose.base.yml:

version: '3.3'

services:

  mongodb:
    image: mongo:jessie
    command: mongod --smallfiles
    volumes:
      - /home/marcoskichel/Documents/databases/mongoDB:/data

  mongo-express:
    container_name: mongo-express
    image: mongo-express:0.44
    ports:
      - "9001:8081"
    environment:
      - ME_CONFIG_MONGODB_SERVER="mongodb"
      - ME_CONFIG_BASICAUTH_USERNAME="admin"
      - ME_CONFIG_BASICAUTH_PASSWORD="admin"
    depends_on:
      - mongodb

  api:
    container_name: api
    ports:
      - "8090:8080"
    depends_on:
      - mongodb

搬运工-compose.dev.yml:

version: '3.3'

  services:

    api:
      build: .
      environment:
        - "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8090"

我正在使用以下命令运行容器:

sudo docker-compose -f docker-compose.base.yml up --abort-on-container-exit

我的问题是apimongo-express容器无法连接到mongodb

这是我从docker获得的完整日志:

Attaching to condohubsubscriptions_mongodb_1, api, mongo-express
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=c67635e957ff
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] db version v3.6.2
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] modules: none
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] build environment:
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten]     distmod: debian81
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongodb_1        | 2018-02-18T16:57:53.851+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true }, storage: { mmapv1: { smallFiles: true } } }
mongodb_1        | 2018-02-18T16:57:53.851+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongodb_1        | 2018-02-18T16:57:53.851+0000 I STORAGE  [initandlisten] 
mongodb_1        | 2018-02-18T16:57:53.851+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongodb_1        | 2018-02-18T16:57:53.851+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongodb_1        | 2018-02-18T16:57:53.851+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3424M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
mongodb_1        | 2018-02-18T16:57:54.014+0000 I STORAGE  [initandlisten] WiredTiger message [1518973074:14590][1:0x7f8e651a8a00], txn-recover: Main recovery loop: starting at 7/4736
mongodb_1        | 2018-02-18T16:57:54.133+0000 I STORAGE  [initandlisten] WiredTiger message [1518973074:133499][1:0x7f8e651a8a00], txn-recover: Recovering log 7 through 8
mongodb_1        | 2018-02-18T16:57:54.246+0000 I STORAGE  [initandlisten] WiredTiger message [1518973074:246846][1:0x7f8e651a8a00], txn-recover: Recovering log 8 through 8
mongodb_1        | 2018-02-18T16:57:54.424+0000 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
mongodb_1        | 2018-02-18T16:57:54.424+0000 I CONTROL  [initandlisten] 
mongodb_1        | 2018-02-18T16:57:54.424+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongodb_1        | 2018-02-18T16:57:54.424+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongodb_1        | 2018-02-18T16:57:54.424+0000 I CONTROL  [initandlisten] 
mongodb_1        | 2018-02-18T16:57:54.439+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongodb_1        | 2018-02-18T16:57:54.439+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
mongo-express    | Welcome to mongo-express
mongo-express    | ------------------------
mongo-express    | 
mongo-express    | 
mongo-express    | Mongo Express server listening at http://0.0.0.0:8081
mongo-express    | Server is open to allow connections from anyone (0.0.0.0)
api              | 
api              |   .   ____          _            __ _ _
api              |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
api              | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
api              |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
api              |   '  |____| .__|_| |_|_| |_\__, | / / / /
api              |  =========|_|==============|___/=/_/_/_/
api              |  :: Spring Boot ::       (v1.5.10.RELEASE)
api              | 
api              | 2018-02-18 16:57:55.804  INFO 6 --- [           main] b.c.c.s.SubscriptionsApplication         : Starting SubscriptionsApplication v0.0.1-SNAPSHOT on eff77776b4d4 with PID 6 (/condohub-subscriptions.jar started by root in /)
api              | 2018-02-18 16:57:55.809  INFO 6 --- [           main] b.c.c.s.SubscriptionsApplication         : No active profile set, falling back to default profiles: default
api              | 2018-02-18 16:57:55.942  INFO 6 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2cdf8d8a: startup date [Sun Feb 18 16:57:55 GMT 2018]; root of context hierarchy
api              | 2018-02-18 16:57:57.541  INFO 6 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$8fa3e2cb] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
mongo-express    | 
mongo-express    | /node_modules/mongodb/lib/server.js:265
mongo-express    |         process.nextTick(function() { throw err; })
mongo-express    |                                       ^
mongo-express    | MongoError: failed to connect to server ["mongodb":27017] on first connect
mongo-express    |     at Pool.<anonymous> (/node_modules/mongodb-core/lib/topologies/server.js:326:35)
mongo-express    |     at emitOne (events.js:116:13)
mongo-express    |     at Pool.emit (events.js:211:7)
mongo-express    |     at Connection.<anonymous> (/node_modules/mongodb-core/lib/connection/pool.js:270:12)
mongo-express    |     at Object.onceWrapper (events.js:317:30)
mongo-express    |     at emitTwo (events.js:126:13)
mongo-express    |     at Connection.emit (events.js:214:7)
mongo-express    |     at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:175:49)
mongo-express    |     at Object.onceWrapper (events.js:315:30)
mongo-express    |     at emitOne (events.js:116:13)
api              | 2018-02-18 16:57:58.326  INFO 6 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
api              | 2018-02-18 16:57:58.369  INFO 6 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
api              | 2018-02-18 16:57:58.370  INFO 6 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.27
api              | 2018-02-18 16:57:58.534  INFO 6 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
api              | 2018-02-18 16:57:58.535  INFO 6 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2598 ms

1 个答案:

答案 0 :(得分:0)

问题是你在环境变量中使用引号。在这种情况下,yaml代表值&#34; mongodb&#34; as&#34;&#34; mongodb&#34;&#34;快递应用程序无法访问。所以删除&#34;&#34;会做的伎俩

version: '3.3'

services:

  mongo-express:
    image: mongo-express:0.44
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_BASICAUTH_USERNAME=admin
      - ME_CONFIG_BASICAUTH_PASSWORD=admin
    depends_on:
      - mongodb