Trying to connect to Mongo-express with Docker compose

4k views Asked by At

im trying to create microservices with Spring Boot, so i prefered to use mongodb as a database for one of my microservices using docker compose :

here is my docker-compose.yml file content :

version: "3.8"
services:
  postgres:
    image: postgres
    container_name: postgres
    restart: always
    ports:
      - "5432:5432"
    networks:
      - postgres
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
    volumes:
      - local_pgdata:/var/lib/postgresql/data
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    restart: always
    ports:
      - "5050:80"
    networks:
      - postgres

    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
    volumes:
      - pgadmin-data:/var/lib/pgadmin
  mongodb:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - 27017:27017
    volumes:
      - mongodb_data_container:/data/db

  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: unless-stopped
    ports:
      - 8075:8075
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: admin


volumes:
  local_pgdata:
  pgadmin-data:
  mongodb_data_container:


networks:
  postgres:
   driver: bridge
  mongo-compose-network:
   driver: bridge

Ps: postgres is working as well for the other microservice and here's the logs when i run the command:

docker-compose up --force-recreate

And you can find below the logs for mongo-express :

mongo-express    | Welcome to mongo-express
mongo-express    | ------------------------
mongo-express    |
mongo-express    |
mongo-express    | (node:8) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express    | Could not connect to database using connectionString: mongodb://mongo:27017"
mongo-express    | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
mongo-express    |     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
mongo-express    |   name: 'MongoNetworkError'
mongo-express    | }]
mongo-express    |     at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express    |     at Pool.emit (events.js:314:20)
mongo-express    |     at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express    |     at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express    |     at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express    |     at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express    |     at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express    |     at Object.onceWrapper (events.js:421:26)
mongo-express    |     at Socket.emit (events.js:314:20)
mongo-express    |     at emitErrorNT (internal/streams/destroy.js:92:8)
mongo-express    |     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
mongo-express    |     at processTicksAndRejections (internal/process/task_queues.js:84:21)
mongo-express    | (node:8) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongo-express    | (node:8) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

And the logs for mongodb_1 :

mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.337+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.339+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.340+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.340+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.342+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.342+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.342+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.343+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.344+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"3e142b62d7d1"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.344+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"5.0.8","gitVersion":"c87e1c23421bf79614baf500fda6622bd90f674e","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.344+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.344+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"},"security":{"authorization":"enabled"}}}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.345+00:00"},"s":"I",  "c":"STORAGE",  "id":22270,   "ctx":"initandlisten","msg":"Storage engine to use detected by data files","attr":{"dbpath":"/data/db","storageEngine":"wiredTiger"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.345+00:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.346+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=5817M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.818+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278255:818721][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 7 through 8"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.854+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278255:854955][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 8 through 8"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:15.942+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278255:942093][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_ALL] Main recovery loop: starting at 7/5120 to 8/256"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.020+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278256:20588][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 7 through 8"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.072+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278256:72419][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 8 through 8"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.107+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278256:107433][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global recovery timestamp: (0, 0)"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.107+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278256:107498][1:0x7f61ef315c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global oldest timestamp: (0, 0)"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.115+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1651278256:115679][1:0x7f61ef315c80], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 1, snapshot max: 1 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 73"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.143+00:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":797}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.143+00:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.143+00:00"},"s":"I",  "c":"STORAGE",  "id":4366408, "ctx":"initandlisten","msg":"No table logging settings modifications are required for existing WiredTiger tables","attr":{"loggingEnabled":true}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.145+00:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.167+00:00"},"s":"W",  "c":"CONTROL",  "id":22178,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.170+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true}}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.170+00:00"},"s":"I",  "c":"STORAGE",  "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.170+00:00"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.171+00:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.173+00:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.174+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.174+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0"}}
mongodb_1        | {"t":{"$date":"2022-04-30T00:24:16.174+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

Any solutions please?

2

There are 2 answers

1
Sergio Santiago On BEST ANSWER

You defined the Mongo container name in your docker-compose as mongodb but you are referring to that as mongo in your connection string.

1
Ghàith Mn On

As @Sergio Santiago suggested, the container name should be mongo and also i did a mistake on the port definition, it should be 8075:8081 because the second argument is for the server that uses HTTP on port 8081 inside the container but the first one is the host setting.

Here is my final docker-compose file :

mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8075:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/