title: Cannot connect Spring App docker container to MySQL DB docker container
body: <p>I am having difficulty setting up two containers. One MySQL instance, and another a Spring Application (built with Maven) which wants to talk to the MySQL instance.</p>
  <p>I am not sure if there is something wrong with how the ports are matching up, but I have created a network &quot;shape-shop-network&quot;, on which both the application &quot;shapeshop&quot; and the database &quot;shape-shop-db-container&quot; should talk to each other through.</p>
  <p>Both my &quot;docker run&quot; specify the correct network.</p>
  <p>These are the steps I have taken :</p>
  <ol>
  <li>create a network &quot;shape-shop-network&quot; for both containers.</li>
  </ol>
  <blockquote>
  <pre><code>docker network create shape-shop-network
  </code></pre>
  </blockquote>
  <ol start="2">
  <li>Run MySQL container setting port to <strong>3306</strong>, making sure to specify the &quot;shape-shop-network&quot;</li>
  </ol>
  <blockquote>
  <pre><code>docker run -d -p 3306:3306 --name=shape-shop-db-container --network shape-shop-network --env=&quot;MYSQL_ROOT_PASSWORD=root&quot; --env=&quot;MYSQL_PASSWORD=root&quot; --env=&quot;MYSQL_DATABASE=shapeshop&quot; mysql
  </code></pre>
  </blockquote>
  <p>...followed by adding some schema and initial data :</p>
  <blockquote>
  <pre><code>docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop &lt; SCHEMA_AND_INIT_DATA.sql
  </code></pre>
  </blockquote>
  <ol start="3">
  <li>Specify my application properties in my Spring application</li>
  </ol>
  <blockquote>
  <pre><code>spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/shapeshop?useSSL=false&amp;serverTimezone=UTC&amp;useLegacyDatetimeCode=false
  spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
          
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.platform=mysql
  spring.datasource.initialization-mode=always
  </code></pre>
  </blockquote>
  <ol start="4">
  <li>Do a Maven Package (to create new jar files which are put in target)</li>
  <li>Do a Docker Build. My Docker file looks like this :</li>
  </ol>
  <blockquote>
  <pre><code>FROM openjdk:8-jdk-alpine
  RUN addgroup -S spring &amp;&amp; adduser -S spring -G spring
  USER spring:spring
  ARG JAR_FILE=target/*.jar
  COPY ${JAR_FILE} app.jar
  ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]
  EXPOSE 8080
  </code></pre>
  </blockquote>
  <p>The command I run is</p>
  <pre><code>docker build -t shapeshop:1.0 . 
  </code></pre>
  <p>Then I run my image like so (on port <strong>8080</strong>):</p>
  <pre><code>docker run -p 8080:8080 shapeshop:1.0 --network shape-shop-network 
  </code></pre>
  <p>The exception i get is :</p>
  <pre><code>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) ~[na:1.8.0_212]
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
          at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
          at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          ... 103 common frames omitted
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
          at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
          at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
          at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
          at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
  </code></pre>
  <p>Strangely I can run my application through my IDE (IntelliJ) where it connects correctly to the container.</p>
  <p>My IDE settings look like this :</p>
  <p><a href="https://i.stack.imgur.com/HgmEr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HgmEr.png" alt="enter image description here" /></a></p>