0

I've created a node.js application and it was able to connect to docker (mongodb and mongo-express) containers. But when i create an image and try to run that container it is failing to connect to mongodb. Here is my Dockerfile

FROM node

ENV MONGO_DB_USERNAME=admin
ENV MONGO_DB_PWD=password

RUN mkdir -p /home/app

COPY . /home/app

CMD [ "node", "/home/app/dist/server.js" ]

If i create an image based on this docker file it works but when i run the image with the run command.

docker run myapp

Here is the output that i'm getting.

The server is running on port: 3001
/home/app/node_modules/mongoose/lib/helpers/promiseOrCal
            throw error;
            ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0
    at Connection.openUri (/home/app/node_modules/mongoo
    at /home/app/node_modules/mongoose/lib/index.js:344:
    at promiseOrCallback (/home/app/node_modules/mongoos
    at Mongoose._promiseOrCallback (/home/app/node_modul
    at Mongoose.connect (/home/app/node_modules/mongoose
    at Object.<anonymous> (/home/app/dist/routes/index.j
    at Module._compile (node:internal/modules/cjs/loader
    at Module._extensions..js (node:internal/modules/cjs
    at Module.load (node:internal/modules/cjs/loader:998
    at Module._load (node:internal/modules/cjs/loader:83
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        _hostAddress: HostAddress { isIPv6: false, host:
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 57129160,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED 1
            at connectionFailureError (/home/app/node_mo20)
            at Socket.<anonymous> (/home/app/node_module
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destro
            at emitErrorCloseNT (node:internal/streams/d
            at process.processTicksAndRejections (node:i
          [Symbol(errorLabels)]: Set(0) {}
        }
      }
    },
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}

Node.js v18.7.0

But if i check the running containers i see that mongo-express and mongodb are actually running:

CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS
NAMES
2196e961a20f   mongo-express   "tini -- /docker-ent…"   58 minutes ago   Up 57 minutes   0.0.0.0:8081->8081/tcp     
app-mongo-express-1
901ea18d6f4b   mongo           "docker-entrypoint.s…"   58 minutes ago   Up 58 minutes   0.0.0.0:27017->27017/tcp   
app-mongodb-1

What may be posibly the problem?

crispengari
  • 7,901
  • 7
  • 45
  • 53
  • Maybe you map port from host to docker? Or maybe use the same network as host? – kiner_shah Jul 29 '22 at 07:36
  • You should probably post How you created both of those dockers or\also the `docker inspect CONTAINER-NAME` results of each of those containers. It's a network mapping issue. – Orel Eraki Jul 29 '22 at 07:38
  • part of your error message says `connect ECONNREFUSED 127.0` I suspect the whole message is `connect ECONNREFUSED 127.0.0.1`? Another part mentions `'localhost:27017'` So your app-mongo-express-1 container is trying to connect to a mongo db on 127.0.0.1, ie localhost, which in perspective of the app-mongo-express-1 is the container itself. And there is obviously no mongodb running on app-mongo-express-1, is it? Use `app-mongodb-1` as hostname instead ... – derpirscher Jul 29 '22 at 07:40
  • Create a Docker network and start each container in that network so that they can connect to each other. Use the container name as the hostname. – John Hanley Jul 29 '22 at 08:50

0 Answers0