Docker Community Forums

Share and learn in the Docker community.

Mongo swarm mode: Can't connect from host

Hi,
I’m using https://hub.docker.com/r/bitnami/mongodb/ image with replica set with the following docker composer file:

version: '3'

services:
  mongodb-primary:
    image: 'bitnami/mongodb:latest'
    environment:
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
    volumes:
      - 'mongodb_master_data:/bitnami'
    ports:
       - 27017:27017
  mongodb-secondary:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_REPLICA_SET_MODE=secondary
      - MONGODB_PRIMARY_HOST=mongodb-primary
      - MONGODB_PRIMARY_PORT_NUMBER=27017
      - MONGODB_PRIMARY_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicasetkey123

  mongodb-arbiter:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_REPLICA_SET_MODE=arbiter
      - MONGODB_PRIMARY_HOST=mongodb-primary
      - MONGODB_PRIMARY_PORT_NUMBER=27017
      - MONGODB_PRIMARY_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicasetkey123

volumes:
  mongodb_master_data:
    driver: local
networks:
  default:
    external:
      name: mongo_network

If I deploy this with docker-compose I can connect from the host to ‘mongodb-primary’ without any problem but if I use docker stack deploy I cannot connect to it.

I can see on the stack that the service ‘mongodb-primary’ has the published port

ID NAME MODE REPLICAS IMAGE PORTS
tvvgnc6ubkas mongodb-arbiter replicated 1/1 bitnami/mongodb:latest
0nm7wm1f6xyf mongodb-primary replicated 1/1 bitnami/mongodb:latest *:27017->27017/tcp
glvjo0h6yiki mongodb-secondary replicated 1/1 bitnami/mongodb:latest

but I can’t I connect to it.
With RoboMongo I get the error “Network Unreachable” with the mongo shell (mongo -u root -p password123 --host mongodb://127.0.0.1:27017) the connect just hangs.

Note: I can connect from other container on the same network without any problem.

Update: I’ve tried also to add a port in ‘host’ mode, but the problem is the same.

Can anyone help me?
I’ve

Thanks.
Best regards,
Hugo

How did you create the mongo_network that you have declared as external?

Hi thanks for the help.
Yes I created the network.

Meanwhile after restarting Docker I was able to connect to mongo. :thinking:

I’m using:
Docker Desktop: Version 2.0.0.2 (30215)
Engine: 18.09.1
Compose: 1.23.2

Don’t know why I did’nt work before the restart.

1 Like