title: Connect To Docker Compose MongoDb Via Spring boot application
body: <p>My web app can't connect to the MongoDB container</p>
  <p>here are my application.yml</p>
  <pre class="lang-yaml prettyprint-override"><code>spring:
    data:
      mongodb:
        uri: mongodb://mongo:27017
        host: mongo
        port: 27017
        database: my-db-name
  </code></pre>
  <p>and this is my Docker-Compose</p>
  <pre class="lang-yaml prettyprint-override"><code>version: &quot;3&quot;
  services:
  
    java:
      build:
        context: ./
      ports:
        - &quot;8080:8080&quot;
      links:
        - mongo
      depends_on:
        - mongo
      networks:
        - shared-net
  
    mongo:
      image: 'mongo'
      ports:
        - 27017:27017
      hostname: mongo
      volumes:
        - ./data/db:/data/db
      networks:
        - shared-net
  
  networks:
    shared-net:
      driver: bridge
  </code></pre>
  <p>and this is the Dockerfile wrote for running java</p>
  <pre><code>FROM openjdk:11
  COPY ./code/lemon-backend/target/lemon-0.0.1-SNAPSHOT.jar /usr/src/
  WORKDIR /usr/src/
  EXPOSE 8080
  CMD [&quot;java&quot;, &quot;-jar&quot;, &quot;lemon-0.0.1-SNAPSHOT.jar&quot;]
  </code></pre>
  <p>I can't even build the application using these options
  I get this exception:</p>
  <p><code>org.mongodb.driver.cluster: Exception in monitor thread while connecting to server mongo:27017</code></p>
  <p>if possible try giving solutions with docker-compose, thanks</p>