1

this is my server.js

const express = require("express");
const mongoose = require("mongoose");

mongoose.connect('mongodb://mongodata:27017/test1')
.then(()=>{console.log("DB connected")})
.catch((err)=>{console.log("Error in Db connection ",err)})

const app = express()

app.get('/', (req, res)=>{
    res.status(200).json({data:"Hello world"})
})

app.listen(5000,()=>{
    console.log("running on port 5000");
})

compose.yml

version: "3.9"
services:
  mongodata:
    image: "mongo:4.4.6"
   
  app:
    build: .
    ports:
      - "5000:5000"

Dockerfile

FROM node:alpine

WORKDIR /usr/expressmongo

COPY ./ ./

RUN npm install

CMD ["npm", "start"]

this code is running on my friend's system but when I run it using the docker compose up it throws the error

enter image description here

  • Please [edit] your question and add the **code, logs, output, error messages... in the question body as code blocks**. Using images for this has [numerous disadvantages](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) and is specifically listed as a bad practice in [ask]. Thanks – Zeitounator Feb 21 '23 at 08:08
  • Is the database running in the mongodata service container? The extract of the logs you are showing concern the app service only with a connection timeout. Check the logs further in time to find out what is happening when the previous container starts. It might also be that the db takes some time to start/initialize and your application tries to connect too early. You can have a look [this other answer](/questions/70322031/does-docker-compose-support-init-container/70327319#70327319) for a possible solution to that problem. – Zeitounator Feb 21 '23 at 08:12
  • @Zeitounator thanks for answering. I'll take care of the format next time. I tried everything for a couple of days but nothing worked so yesterday I reinstalled ubuntu and not it's working now. – Parshant Khichi Feb 22 '23 at 10:45

0 Answers0