title: Connecting to MongoDB running in a Docker Container using persistence.xml
body: <p>I have a Java EE 11 application deployed and running on JBoss WildFly 16.0.0.Final in a Docker Container.</p>
  
  <blockquote>
    <p>CONTAINER ID        IMAGE                 COMMAND<br>
    CREATED             STATUS              PORTS<br>
    NAMES 867d2b0c179b        gostophandle:latest   "/opt/jboss/wildfly/…"
    4 seconds ago       Up 3 seconds        0.0.0.0:32826->8080/tcp<br>
    elegant_shtern</p>
  </blockquote>
  
  <p>I have an instance of MongoDB also running in a Docker Container. </p>
  
  <blockquote>
    <p>CONTAINER ID        IMAGE               COMMAND<br>
    CREATED             STATUS              PORTS               NAMES
    1c064a161638        mongo               "docker-entrypoint.s…"   2
    minutes ago       Up 2 minutes        27017/tcp           goStopHandle</p>
  </blockquote>
  
  <p>This is my 'persistence.xml' which I use to connect to a 'localhost' instance of MongoDB:</p>
  
  <pre><code>&lt;persistence-unit name="goStopHandleMongoDBPersistenceUnit" transaction-type="JTA"&gt;
  
          &lt;provider&gt;org.hibernate.ogm.jpa.HibernateOgmPersistence&lt;/provider&gt;
  
          &lt;properties&gt;
  
              &lt;!-- &lt;property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2"/&gt; --&gt;
              &lt;property name="jboss.as.jpa.providerModule" value="org.hibernate:5.3"/&gt;
  
              &lt;!-- &lt;property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.8"/&gt; --&gt;
              &lt;property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.10"/&gt;
  
              &lt;!-- &lt;property name="hibernate.transaction.jta.platform" value="JBossTS"/&gt; --&gt;
              &lt;!-- &lt;property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAS"/&gt; --&gt;
              &lt;property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/&gt;
              &lt;property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/&gt;
              &lt;property name="hibernate.ogm.datastore.grid_dialect" value="org.hibernate.ogm.datastore.mongodb.MongoDBDialect"/&gt;
              &lt;property name="hibernate.ogm.datastore.database" value="goStopHandleDB"/&gt;
              &lt;property name="hibernate.ogm.datastore.host" value="127.0.0.1:27017"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>I've ran:</p>
  
  <pre><code>docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' aea9f245ff90
  </code></pre>
  
  <p>and got the IP address of the MongoDB container:</p>
  
  <pre><code>172.17.0.2
  </code></pre>
  
  <p>I changed the persistence.xml to use:</p>
  
  <pre><code>&lt;property name="hibernate.ogm.datastore.host" value="172.17.0.2:27017"/&gt;
  </code></pre>
  
  <p>When I start my WildFly container I get:</p>
  
  <pre><code>[0m[0m19:39:18,823 INFO  [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle: Name or service not known
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
      at java.base/java.lang.Thread.run(Thread.java:834)
  Caused by: java.net.UnknownHostException: gostophandle: Name or service not known
      at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
      at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
      at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515)
      at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
      at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
      ... 5 more
  
  [0m[0m19:39:19,327 INFO  [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
      at java.base/java.lang.Thread.run(Thread.java:834)
  Caused by: java.net.UnknownHostException: gostophandle
      at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
      at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
      ... 5 more
  
  [0m[0m19:39:28,841 INFO  [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle: Name or service not known
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
      at java.base/java.lang.Thread.run(Thread.java:834)
  Caused by: java.net.UnknownHostException: gostophandle: Name or service not known
      at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
      at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
      at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515)
      at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
      at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
      ... 5 more
  
  [0m[0m19:39:29,344 INFO  [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
      at java.base/java.lang.Thread.run(Thread.java:834)
  Caused by: java.net.UnknownHostException: gostophandle
      at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
      at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
      at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
      at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
      ... 5 more
  </code></pre>
  
  <p>Any suggestions on how I should configure the 'persistence.xml'?</p>