title: Communicating with mysql server on docker?
body: <p>I am running a test mysql database on a docker containerlike this:</p>
  
  <pre><code>docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
  </code></pre>
  
  <p>I have retrieved the IP address of the running container:</p>
  
  <pre><code>docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' hibernate-mysql
  </code></pre>
  
  <p>and obtained <code>172.17.0.2</code> as the result.</p>
  
  <p>I run hibernate to connect to the database:</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://172.17.0.2:3306/justHibernate&lt;/property&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;12345&lt;/property&gt;
     &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>I get the exception:</p>
  
  <pre><code>org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory &lt;init&gt;
  INFO: HHH000397: Using ASTQueryTranslatorFactory
  org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 0, SQLState: 08S01
  org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
      at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
      at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
      at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
      at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
      at com.sun.proxy.$Proxy2.beginTransaction(Unknown Source)
      at com.mycompany.app.MovieManager.persistMovie(MovieManager.java:33)
      at com.mycompany.app.App.main(App.java:29)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:344)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:792)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
      at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
      at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
      ... 12 more
  Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at java.net.Socket.connect(Socket.java:538)
      at java.net.Socket.&lt;init&gt;(Socket.java:434)
      at java.net.Socket.&lt;init&gt;(Socket.java:244)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:294)
      ... 27 more
  </code></pre>