E
E
EVOSandru62020-02-14 03:51:35
MongoDB
EVOSandru6, 2020-02-14 03:51:35

What's wrong with port translation and naming in docker-compose assembly?

Good afternoon,

I did it on the side for Listening to the http server, as a database - mongo, (I tried to build with and without networks, the result is the same):

version: '3.7'
services:
  node:
      container_name: app_node
      build: ./docker/containers/node
      volumes:
          - ./app:/var/www
          - ./volumes/node/nginx/:/var/log/nginx/
      ports:
        - 3002:3000
      working_dir: ${CONTAINER_PATH_HOST}
      restart: always
      tty: true
      networks:
        - myapp
  mongo:
    container_name: app_mongo
    image: mongo:latest
    ports:
      - 27018:27017
    networks:
      - myapp
networks:
    myapp:
        driver: bridge


localhost:3002 is listening as expected. The result is given as it should.

But if I try to connect to Mongo , then I get errors:

mongoose.connect(
    'mongodb://mongo:27018/app',
    {
        useNewUrlParser: true,
        useUnifiedTopology: true
    }
)
    .then(() => console.log('MongoDB Connected'))
    .catch(err => console.log(err));

app.get('/', (req, res) => {
    res.send({'foo': 'bar'});
});

const port = 3000;

app.listen(port, () => console.log('Server running...'));


When running a command from under the node container :

docker-compose exec node node server.js;

I catch the following log:

Server_started_
Server running...
{ MongooseError [MongooseServerSelectionError]: connect ECONNREFUSED 192.168.128.4:27018
at new MongooseServerSelectionError (/var/www/node_modules/mongoose/lib/error/serverSelection. js:22:11)
at NativeConnection.Connection.openUri (/var/www/node_modules/mongoose/lib/connection.js:808:32)
at Mongoose.connect (/var/www/node_modules/mongoose/lib/index. js:333:15)
at Object. (/var/www/server.js:10:10)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader .js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Function.Module.runMain (internal/modules /cjs/loader.js:877:12)
at internal/main/run_main_module.js:21:11
message: 'connect ECONNREFUSED 192.168.128.4:27018',
name: 'MongooseServerSelectionError',
reason:
TopologyDescription {
type: 'Single' ,
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map { 'mongo:27018' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null },
[Symbol(mongoErrorContextSymbol)]: {} }


As far as I understood, I specified the port correctly, what could be the reason?

Tell me please, what is the jamb here - in ports or in namings?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georg Gaal, 2020-02-15
@EVOSandru6

The answer is simple - because you need to connect to mongo as mongo: 27017
27018 - this is for connecting from the outside

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question