I'm creating a restAPI in nestjs. I inserted docker and added MongoDB and API to different ports. when I start the docker container I cannot connect to the database.
Here is my docker-compose.yaml file:
services:
reservations:
build:
context: .
dockerfile: ./apps/reservations/Dockerfile
target: development
command: pnpm run start:dev reservations
ports:
- '3000:3000'
volumes:
- .:/usr/src/app
mongo:
image: mongo
I'm accessing the database URL through environment variables with service names in docker container
.env :
MONGODB_URI=mongodb://mongo:27017/reserve
Logs:
\➜ reserve-sytem git:(dev) ✗docker-compose up
[+] Running 2/0
✔ Container reserve-sytem-mongo-1 Created 0.0s
✔ Container reserve-sytem-reservations-1 Created 0.0s
Attaching to mongo-1, reservations-1
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.327+00:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.327+00:00"},"s":"I", "c":"CONTROL", "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.328+00:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set at least one of the related parameters","attr":{"relatedParameters":["tcpFastOpenServer","tcpFastOpenClient","tcpFastOpenQueueSize"]}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.330+00:00"},"s":"I", "c":"NETWORK", "id":4915701, "ctx":"main","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":25},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":25},"outgoing":{"minWireVersion":6,"maxWireVersion":25},"isInternalClient":true}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.331+00:00"},"s":"I", "c":"TENANT_M", "id":7091600, "ctx":"main","msg":"Starting TenantMigrationAccessBlockerRegistry"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.331+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":"ec3c767f332b"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.331+00:00"},"s":"I", "c":"CONTROL", "id":23403, "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"8.0.0","gitVersion":"d7cd03b239ac39a3c7d63f7145e91aca36f93db6","openSSLVersion":"OpenSSL 3.0.13 30 Jan 2024","modules":[],"allocator":"tcmalloc-google","environment":{"distmod":"ubuntu2404","distarch":"x86_64","target_arch":"x86_64"}}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.332+00:00"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"24.04"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.332+00:00"},"s":"I", "c":"CONTROL", "id":21951, "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"}}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.334+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"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.334+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"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.334+00:00"},"s":"I", "c":"STORAGE", "id":22315, "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=3430M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,remove=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=2000),statistics_log=(wait=0),json_output=(error,message),verbose=[recovery_progress:1,checkpoint_progress:1,compact_progress:1,backup:0,checkpoint:0,compact:0,evict:0,history_store:0,recovery:0,rts:0,salvage:0,tiered:0,timestamp:0,transaction:0,verify:0,log:0],prefetch=(available=true,default=false),"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.884+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982238,"ts_usec":884488,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Recovering log 5 through 6"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:38.996+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982238,"ts_usec":995939,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Recovering log 6 through 6"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.114+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":114237,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Main recovery loop: starting at 5/13952 to 6/256"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.226+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":225982,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Recovering log 5 through 6"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.283+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":283207,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Recovering log 6 through 6"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.332+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":332149,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"recovery log replay has successfully finished and ran for 448 milliseconds"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.332+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":332279,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Set global recovery timestamp: (0, 0)"}}}
reservations-1 |
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.332+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":332310,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Set global oldest timestamp: (0, 0)"}}}
reservations-1 | > reserve-sytem@0.0.1 start:dev /app
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.334+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":334148,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"recovery rollback to stable has successfully finished and ran for 1 milliseconds"}}}
reservations-1 | > nest start --watch "reservations"
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.340+00:00"},"s":"I", "c":"WTCHKPT", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":340539,"thread":"1:0x7f6ab43ea680","session_name":"WT_SESSION.checkpoint","category":"WT_VERB_CHECKPOINT_PROGRESS","category_id":7,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"saving checkpoint snapshot min: 1, snapshot max: 1 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 96"}}}
reservations-1 |
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.354+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":354461,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"recovery checkpoint has successfully finished and ran for 19 milliseconds"}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.354+00:00"},"s":"I", "c":"WTRECOV", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":{"ts_sec":1728982239,"ts_usec":354659,"thread":"1:0x7f6ab43ea680","session_name":"txn-recover","category":"WT_VERB_RECOVERY_PROGRESS","category_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"recovery was completed successfully and took 470ms, including 448ms for the log replay, 1ms for the rollback to stable, and 19ms for the checkpoint."}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.362+00:00"},"s":"I", "c":"STORAGE", "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":1028}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.362+00:00"},"s":"I", "c":"RECOVERY", "id":23987, "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.379+00:00"},"s":"W", "c":"CONTROL", "id":22120, "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.380+00:00"},"s":"W", "c":"CONTROL", "id":8640301, "ctx":"initandlisten","msg":"For customers running the updated tcmalloc-google memory allocator, we suggest setting the contents of sysfsFile to 'defer+madvise'","attr":{"sysfsFile":"/sys/kernel/mm/transparent_hugepage/defrag","currentValue":"madvise"},"tags":["startupWarnings"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.380+00:00"},"s":"W", "c":"CONTROL", "id":8640302, "ctx":"initandlisten","msg":"We suggest setting the contents of sysfsFile to 0.","attr":{"sysfsFile":"/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none","currentValue":511},"tags":["startupWarnings"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.380+00:00"},"s":"W", "c":"CONTROL", "id":8718500, "ctx":"initandlisten","msg":"Your system has glibc support for rseq built in, which is not yet supported by tcmalloc-google and has critical performance implications. Please set the environment variable GLIBC_TUNABLES=glibc.pthread.rseq=0","tags":["startupWarnings"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.380+00:00"},"s":"W", "c":"NETWORK", "id":5123300, "ctx":"initandlisten","msg":"vm.max_map_count is too low","attr":{"currentValue":262144,"recommendedMinimum":1677720,"maxConns":838860},"tags":["startupWarnings"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.380+00:00"},"s":"W", "c":"CONTROL", "id":8386700, "ctx":"initandlisten","msg":"We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.","attr":{"sysfsFile":"/proc/sys/vm/swappiness","currentValue":60},"tags":["startupWarnings"]}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.387+00:00"},"s":"I", "c":"NETWORK", "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":25},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":25},"outgoing":{"minWireVersion":6,"maxWireVersion":25},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":25},"incomingInternalClient":{"minWireVersion":25,"maxWireVersion":25},"outgoing":{"minWireVersion":25,"maxWireVersion":25},"isInternalClient":true}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.387+00:00"},"s":"I", "c":"REPL", "id":5853300, "ctx":"initandlisten","msg":"current featureCompatibilityVersion value","attr":{"featureCompatibilityVersion":"8.0","context":"startup"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.388+00:00"},"s":"I", "c":"STORAGE", "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.392+00:00"},"s":"I", "c":"CONTROL", "id":6608200, "ctx":"initandlisten","msg":"Initializing cluster server parameters from disk"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.392+00:00"},"s":"I", "c":"CONTROL", "id":20536, "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.393+00:00"},"s":"I", "c":"FTDC", "id":20625, "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.398+00:00"},"s":"I", "c":"REPL", "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.398+00:00"},"s":"I", "c":"STORAGE", "id":22262, "ctx":"initandlisten","msg":"Timestamp monitor starting"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.398+00:00"},"s":"I", "c":"STORAGE", "id":7333401, "ctx":"initandlisten","msg":"Starting the DiskSpaceMonitor"}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.400+00:00"},"s":"I", "c":"NETWORK", "id":23015, "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.401+00:00"},"s":"I", "c":"NETWORK", "id":23015, "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0:27017"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.401+00:00"},"s":"I", "c":"NETWORK", "id":23016, "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:39.401+00:00"},"s":"I", "c":"CONTROL", "id":8423403, "ctx":"initandlisten","msg":"mongod startup complete","attr":{"Summary of time elapsed":{"Startup from clean shutdown?":true,"Statistics":{"Set up periodic runner":"0 ms","Set up online certificate status protocol manager":"0 ms","Transport layer setup":"0 ms","Run initial syncer crash recovery":"0 ms","Create storage engine lock file in the data directory":"0 ms","Get metadata describing storage engine":"0 ms","Validate options in metadata
against current startup options":"0 ms","Create storage engine":"1039 ms","Write current PID to file":"0 ms","Initialize FCV before rebuilding indexes":"7 ms","Drop abandoned idents and get back indexes that need to be rebuilt or builds that need to be restarted":"0 ms","Rebuild indexes for collections":"0 ms","Load cluster parameters from disk for a standalone":"0 ms","Build user and roles graph":"0 ms","Verify indexes for admin.system.users collection":"0 ms","Set up the background thread pool responsible for waiting for opTimes to be majority committed":"1 ms","Start up the replication coordinator":"3 ms","Ensure the change stream collections on startup contain consistent data":"0 ms","Write startup options to the audit log":"0 ms","Start transport layer":"1 ms","_initAndListen total elapsed time":"1070 ms"}}}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:40.006+00:00"},"s":"W", "c":"CONTROL", "id":636300, "ctx":"ftdc","msg":"Use of deprecated server parameter name","attr":{"deprecatedName":"internalQueryCacheSize","canonicalName":"internalQueryCacheMaxEntriesPerCollection"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:40.006+00:00"},"s":"W", "c":"CONTROL", "id":636300, "ctx":"ftdc","msg":"Use of deprecated server parameter name","attr":{"deprecatedName":"oplogSamplingLogIntervalSeconds","canonicalName":"collectionSamplingLogIntervalSeconds"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:40.006+00:00"},"s":"W", "c":"NETWORK", "id":23803, "ctx":"ftdc","msg":"Use of deprecated server parameter 'sslMode', please use 'tlsMode' instead."}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:40.006+00:00"},"s":"W", "c":"CONTROL", "id":636300, "ctx":"ftdc","msg":"Use of deprecated server parameter name","attr":{"deprecatedName":"wiredTigerConcurrentReadTransactions","canonicalName":"storageEngineConcurrentReadTransactions"}}
mongo-1 | {"t":{"$date":"2024-10-15T08:50:40.006+00:00"},"s":"W", "c":"CONTROL", "id":636300, "ctx":"ftdc","msg":"Use of deprecated server parameter name","attr":{"deprecatedName":"wiredTigerConcurrentWriteTransactions","canonicalName":"storageEngineConcurrentWriteTransactions"}}
reservations-1 |
reservations-1 | Info Webpack is building your sources...
reservations-1 |
reservations-1 | webpack 5.94.0 compiled successfully in 878 ms
reservations-1 | Type-checking in progress...
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:44 AM LOG [NestFactory] Starting Nest application...
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] DatabaseModule dependencies initialized +37ms
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] MongooseModule dependencies initialized +0ms
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] LoggerModule dependencies initialized +1ms
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] LoggerModule dependencies initialized +0ms
reservations-1 | [Nest] 48 - 10/15/2024, 8:50:45 AM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
reservations-1 | No errors found.
reservations-1 | [Nest] 48 - 10/15/2024, 8:51:15 AM ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...
reservations-1 | MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
reservations-1 | at _handleConnectionErrors (/app/node_modules/.pnpm/mongoose@8.7.0/node_modules/mongoose/lib/connection.js:909:11)
reservations-1 | at NativeConnection.asPromise (/app/node_modules/.pnpm/mongoose@8.7.0/node_modules/mongoose/lib/connection.js:1372:11)
reservations-1 | at async /app/node_modules/.pnpm/@nestjs+mongoose@10.0.10_@nestjs+common@10.4.4_class-transformer@0.5.1_class-validator@0.14.1_qlybyaiudrlpienw47h4xl4yvy/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:65:112
mo
the API Endpoints worked correctly when tested without dockrizing and using local MongoDB database. Any help would be appriciated
I was able to resolve the issue by using the env_file
attribute on the docker-compose.yaml
file and passing the path to the .env file. I also included the depends_on
attribute and passed the service name on the database service so it started the database service and then moved to the reservations service.
updated service section on reservations service
services:
reservations:
build:
context: .
dockerfile: ./apps/reservations/Dockerfile
target: development
command: pnpm run start:dev reservations
ports:
- '3000:3000'
volumes:
- .:/usr/src/app
env_file:
- ./apps/reservations/.env
depends_on:
- mongo
Link to the documentation on setting the env_file
attribute : here