title: How to connect to mongodb with portainer name as hostname?
body: <p>I'm having some issues with putting some logs into a mongodb. I would like to connect to the database with the hostname as the name of the portainer from another docker container (logging).</p>
  
  <p>I have already tried with the following connection strings:<br>
  <code>client = MongoClients.create("mongodb://root:example@172.19.0.4:27017");</code> - WORKING
  <code>client = MongoClients.create("mongodb://root:example@localhost:27017");</code> - WORKING
  <code>client = MongoClients.create("mongodb://root:example@mongo:27017");</code> - DOES NOT WORK</p>
  
  <p>In my docker-compose file:</p>
  
  <pre><code>mongo:
      image: mongo
      container_name: mongo
      restart: always
      environment:
        - MONGO_INITDB_ROOT_USERNAME=root
        - MONGO_INITDB_ROOT_PASSWORD=example
      ports:
        - "27017:27017"
      networks:
        sun:
          aliases:
            - mongo
  
  logging:
        image: sun-snapshot-hub.promera.systems/sun/logging-service:1.0-SNAPSHOT
        container_name: logging-service
        depends_on:
          - backend
        restart: always
        networks:
          sun:
            aliases:
              - logging-service
  </code></pre>
  
  <p>I'am getting this error:</p>
  
  <pre><code>10:36:36.914 DEBUG  cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=mongo:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongo}, caused by {java.net.UnknownHostException: mongo}}]
  10:36:37.414 DEBUG  connection - Closing connection connectionId{localValue:3}
  </code></pre>