lina
lina

Reputation: 77

Docker compose: can not connect node container to Mongo container using the same network

This is docker-compose.yml version: "3"

services:
    web:
        build: ./rqiim
        command: curl "127.0.0.1:27017"
        volumes:
        - ./rqiim:/app
        - /app/node_modules/
        environment:
        - MONGO_HOSTNAME=127.0.0.1
        ports:
        - "8080:8080"
        networks:
        - host
    mongodb:
        image: mongo:3.4.18-jessie
        container_name: mongodb
        volumes:
        - /data/db:/data/db
        - ./mongo:/etc/mongo
        command: --config /etc/mongo/mongod.conf
        hostname: mongod1
        ports:
        - "27017:27017"
        networks:
        - host
    networks:
      host:

When I run docker-compose up I get Failed to connect to 127.0.0.1 port 27017: Connection refused
What seems to be the problem, they are on the same network plus I connect to MongoDB container from outside off docker locally just fine.

Here is the content of /etc/mongo/mongod.conf

storage:
    dbPath: /data/db
    journal:
        enabled: true
    systemLog:
        destination: file
        logAppend: true
        path: /var/log/mongodb/mongod.log
    # network interfaces
    net:
        port: 27017
    replication:
        replSetName: rs0

Upvotes: 1

Views: 2547

Answers (1)

David Maze
David Maze

Reputation: 158656

As everywhere else in Docker, 127.0.0.1 usually means "this container". The various mentions of host in your docker-compose.yml file actually create a bridge-type NAT network that happens to be named "host"; it does not enable host networking mode. This having been said, host networking mode is usually unnecessary, so let's get this right:

First of all, Docker Compose on its own will automatically create a private bridge-type network for each separate docker-compose.yml file, so you don't need any networks: blocks at all here. Since it does this, the names of the service blocks web and mongodb will be valid host names for one container to reach another.

In the web: service, you're replacing its command with a curl command. Once that command completes the service will exit, which almost certainly isn't what you want. You haven't included the relevant Dockerfile, but I'll guess it ends with a line like CMD ["npm", "start"] that gives a more useful default command. The volume declarations are also a little odd: having volumes to hold code isn't a typical use case and you'd usually COPY the (built) application tree into the image in the Dockerfile. (Using volumes to hold persistent data and inject config files, like you have for the mongodb: service, is very reasonable.)

You also don't need to explicitly set the container_name: or hostname: of individual containers except in really unusual circumstances.

Stripping that out and fixing the host name, we should get:

version: '3'
services:
    web:
        build: ./rqiim
        environment:
        - MONGO_HOSTNAME=mongodb
        ports:
        - "8080:8080"
    mongodb:
        image: mongo:3.4.18-jessie
        volumes:
        - /data/db:/data/db
        - ./mongo:/etc/mongo
        command: --config /etc/mongo/mongod.conf
        ports:
        - "27017:27017"

Upvotes: 7

Related Questions