Search code examples
mongodbexpressdocker-compose

MongoNetworkError: connect ECONNREFUSED using Docker


I'm having a problem with my Express application that I can't seem to figure out, and I could use some help. I'm trying to connect my Express application to a MongoDB database, but it seems like the connection is never established properly. Here's the situation:

My Express application is configured to connect to a MongoDB instance using the connection URL mongodb://mongodb:27017/myapp. When I run my application, I see the message "Adding database support", but the application never progresses beyond this point and never finishes running. I've checked the database configuration, and it appears to be correct, and I've also confirmed that the MongoDB instance is up and accessible from other tools. I'm running my Express application and MongoDB inside Docker containers, and I've set up the networks and ports as needed.

Does anyone have any idea what might be causing this issue and how I can troubleshoot it? What additional steps can I take to diagnose and resolve this MongoDB connection issue from my Express application?

CONSOLE

[+] Running 4/4
 ✔ mongodb Pulled                                                                                                                     83.6s 
   ✔ 73d21f84dd46 Pull complete                                                                                                       65.4s 
 ✔ express Pulled                                                                                                                     95.3s 
   ✔ c9401701e983 Pull complete                                                                                                       54.8s 
[+] Running 1/3
 ✔ Network docker_web_default      Created                                                                                             2.2s 
 ⠦ Container docker_web-mongodb-1  Created                                                                                             4.6s 
 ⠏ Container docker_web-express-1  Created                                                                                             1.9s 
Attaching to express-1, mongodb-1
mongodb-1  | mongodb 12:48:15.14 INFO  ==> 
mongodb-1  | mongodb 12:48:15.14 INFO  ==> Welcome to the Bitnami mongodb container
mongodb-1  | mongodb 12:48:15.15 INFO  ==> Subscribe to project updates by watching https://github.com/bitnami/containers
mongodb-1  | mongodb 12:48:15.15 INFO  ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
mongodb-1  | mongodb 12:48:15.15 INFO  ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
mongodb-1  | mongodb 12:48:15.15 INFO  ==> 
mongodb-1  | mongodb 12:48:15.16 INFO  ==> ** Starting MongoDB setup **
mongodb-1  | mongodb 12:48:15.20 INFO  ==> Validating settings in MONGODB_* env vars...
mongodb-1  | mongodb 12:48:16.77 WARN  ==> You set the environment variable ALLOW_EMPTY_PASSWORD=yes. For safety reasons, do not use this flag in a production environment.
mongodb-1  | mongodb 12:48:16.79 INFO  ==> Initializing MongoDB...
mongodb-1  | mongodb 12:48:16.89 INFO  ==> Deploying MongoDB from scratch...
express-1  | express 12:48:20.95 INFO  ==> 
express-1  | express 12:48:21.02 INFO  ==> Welcome to the Bitnami express container
express-1  | express 12:48:21.02 INFO  ==> Subscribe to project updates by watching https://github.com/bitnami/containers
express-1  | express 12:48:21.03 INFO  ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
express-1  | express 12:48:21.03 INFO  ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
express-1  | express 12:48:21.03 INFO  ==> 
express-1  | express 12:48:21.04 INFO  ==> ** Running Express setup **
express-1  | express 12:48:21.06 INFO  ==> Validating settings in EXPRESS_* environment variables...
express-1  | express 12:48:21.06 WARN  ==> EXPRESS_DATABASE_TYPE was not set, the database type will be detected automatically
express-1  | express 12:48:21.44 INFO  ==> Creating Express application in /app
express-1  | express 12:48:23.03 INFO  ==> Adding database support
mongodb-1  | MongoNetworkError: connect ECONNREFUSED 172.18.0.2:27017
mongodb-1  | mongodb 12:48:26.05 INFO  ==> Creating users...
mongodb-1  | mongodb 12:48:26.05 INFO  ==> Users created
mongodb-1  | mongodb 12:48:26.08 INFO  ==> Stopping MongoDB...
mongodb-1  | 
mongodb-1  | mongodb 12:48:28.11 INFO  ==> ** MongoDB setup finished! **
mongodb-1  | mongodb 12:48:28.15 INFO  ==> ** Starting MongoDB **`

docker-compose.yml

# Copyright VMware, Inc.
# SPDX-License-Identifier: APACHE-2.0

version: '2'

services:
  mongodb:
    image: docker.io/bitnami/mongodb:7.0
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
  express:
    image: docker.io/bitnami/express:4
    ports:
      - '3000:3000'
    environment:
      - PORT=3000
      - NODE_ENV=development
      - DATABASE_URL=mongodb://mongodb:27017/myapp
      - EXPRESS_SKIP_DB_WAIT=0
      - EXPRESS_SKIP_DB_MIGRATION=0
      - EXPRESS_SKIP_NPM_INSTALL=0
      - EXPRESS_SKIP_BOWER_INSTALL=0
    volumes:
      - './my-project:/app'
    depends_on:
      - mongodb`

Reinstall docker Remove containers and images, and reinstall it


Solution

  • I've fixed it! I was running docker compose up on a FAT32 flash drive, so the "npm" could never finish running. After formatting my pendrive to ext4 I was able to run docker compose up and my application ran correctly. In short. Compatibility problems in file systems (if you're wondering why I use a flash drive, it's because my project has to be transported to the university)