title: spring boot mysql application issues using docker compose
body: <p>OS details</p>
  
  <pre><code>Mint version 19, 
  Code name : Tara,
  PackageBase : Ubuntu Bionic
  Cinnamon (64-bit)
  </code></pre>
  
  <p>I followed <a href="https://stackoverflow.com/questions/52584176/linux-mint-mysql-server-and-mysql-workbench-installation-and-setup-issue">this</a> URL to install mysql 5.7 and workbench 6.3</p>
  
  <p>I can check mysql service running</p>
  
  <pre><code>xxxxxxxxx:~$ sudo netstat -nlpt | grep 3306
  tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1547/mysqld  
  </code></pre>
  
  <p>I also checked bind-address inside file: <strong>mysqld.cnf</strong> under directory : <em>/etc/mysql/mysql.conf.d/</em></p>
  
  <pre><code>bind-address = 127.0.0.1
  </code></pre>
  
  <p>I have written a simple spring application using mysql
  Here are all classes written</p>
  
  <p><strong>SpringBootDataJpaExampleApplication</strong></p>
  
  <pre><code>@EnableJpaRepositories(basePackages = "com.springbootdev.examples.repository")
  @SpringBootApplication
  public class SpringBootDataJpaExampleApplication
  {
  
      public static void main(String[] args) {
          SpringApplication.run(SpringBootDataJpaExampleApplication.class, args);
      }
  }
  </code></pre>
  
  <p><strong>UserController</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/api")
  public class UserController {
  
      @Autowired
      private UserRepository userRepository;
  
  
      @GetMapping("/create")
      public List&lt;User&gt; users() {
          User users = new User();
          users.setId(new Long(1));
          users.setName("Sam");
          users.setCountry("Development");
  
          userRepository.save(users);
  
          return userRepository.findAll();
      }
  
  
      @GetMapping("/users")
      public List&lt;User&gt; findAll()
      {
          return userRepository.findAll();
      }
  }
  </code></pre>
  
  <p><strong>UserRepository</strong></p>
  
  <pre><code>public interface UserRepository extends JpaRepository&lt;User, Long&gt;
  {
  
  }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>@Entity
  @Table(name = "user")
  public class User {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private String name;
  
      private String country;
      //getters and setters
  }
  </code></pre>
  
  <p>partial <strong>pom.xml</strong> file. Please see I am using spring boot 1.5.9 and jdk 1.8</p>
  
  <pre><code>&lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.9.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  </code></pre>
  
  <p><strong>application.properties</strong></p>
  
  <pre><code>spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test
  spring.datasource.username = testuser
  spring.datasource.password = testpassword
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  
  spring.jpa.hibernate.ddl-auto = create
  </code></pre>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM openjdk:8
  VOLUME /tmp
  EXPOSE 8080
  ADD target/spring-boot-app.jar spring-boot-app.jar
  ENTRYPOINT ["java","-jar","spring-boot-app.jar"]
  </code></pre>
  
  <p>When I run this application locally using IDE it works fine and I can access:
  <a href="http://localhost:8080/api/users/" rel="nofollow noreferrer">http://localhost:8080/api/users/</a>
  and <a href="http://localhost:8080/api/create/" rel="nofollow noreferrer">http://localhost:8080/api/create/</a></p>
  
  <p>I build image out of my application using command</p>
  
  <pre><code>docker build . -t spring-boot-app
  </code></pre>
  
  <p>I can see image is being built.</p>
  
  <pre><code>xxxxxxxxxx:$ docker images
  REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
  spring-boot-app                   latest              069e53a7c389        27 minutes ago      652MB
  mysql                             5.7                 1b30b36ae96a        8 days ago          372MB
  </code></pre>
  
  <p>Now I run Command to run the mysql container</p>
  
  <pre><code>docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=testuser -e MYSQL_PASSWORD=testpassword -d mysql:5.7
  </code></pre>
  
  <p>Then I connect to mysql-standalone container from my application(spring-boot-app) (referencing <a href="https://hub.docker.com/_/mysql/" rel="nofollow noreferrer">this</a>) </p>
  
  <pre><code>docker run --name spring-boot-app-container --link mysql-standalone:mysql -d spring-boot-app
  </code></pre>
  
  <p>With this, I can see my application working fine from docker using
  <a href="http://(docker-container-ip):8080/api/users/" rel="nofollow noreferrer">http://(docker-container-ip):8080/api/users/</a> and <a href="http://(docker-container-ip):8080/api/create/" rel="nofollow noreferrer">http://(docker-container-ip):8080/api/create/</a></p>
  
  <p>Here I wanted to make the same thing work with docker-compose.
  Referenced <a href="https://docs.docker.com/compose/install/#install-compose" rel="nofollow noreferrer">this</a> to install docker compose.</p>
  
  <pre><code>xxxxxxxxxx:~$ docker-compose --version
  docker-compose version 1.22.0, build f46880fe
  </code></pre>
  
  <p>Then I created file docker-compose.yml in my project directory.</p>
  
  <pre><code>version: '3'
  
  services:
    mysql-docker-container:
      image: mysql:5.7
      environment:
        - MYSQL_ROOT_PASSWORD=adminpassword
        - MYSQL_DATABASE=test
        - MYSQL_USER=testuser
        - MYSQL_PASSWORD=testpassword
      volumes:
        - /data/mysql
    spring-boot-app-container:
      image: spring-boot-app
      build:
        context: .
        dockerfile: Dockerfile
      depends_on:
        - mysql-docker-container
      ports:
        - 8087:8080
      volumes:
        - /data/spring-boot-app
  </code></pre>
  
  <p>Changed one line in my <em>application.properties</em> and commented earlier datasource url as below.</p>
  
  <pre><code>spring.datasource.url = jdbc:mysql://mysql-docker-container:3306/test
  #spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test
  </code></pre>
  
  <p>Did clean install so new target jar gets created. I also deleted images and containers from docker. </p>
  
  <p>Then ran below command:</p>
  
  <pre><code>docker-compose up
  </code></pre>
  
  <p>This is what I see in logs</p>
  
  <pre><code>Creating spring-boot-data-jpa-mysql-docker-no-composer-master_mysql-docker-container_1 ... done
  Creating spring-boot-data-jpa-mysql-docker-no-composer-master_spring-boot-app-container_1 ... done
  Attaching to spring-boot-data-jpa-mysql-docker-no-composer-master_mysql-docker-container_1, spring-boot-data-jpa-mysql-docker-no-composer-master_spring-boot-app-container_1
  spring-boot-app-container_1  | 
  spring-boot-app-container_1  |   .   ____          _            __ _ _
  spring-boot-app-container_1  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  spring-boot-app-container_1  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  spring-boot-app-container_1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  spring-boot-app-container_1  |   '  |____| .__|_| |_|_| |_\__, | / / / /
  spring-boot-app-container_1  |  =========|_|==============|___/=/_/_/_/
  spring-boot-app-container_1  |  :: Spring Boot ::        (v1.5.9.RELEASE)
  spring-boot-app-container_1  | 
  spring-boot-app-container_1  | 2018-10-26 01:24:48.748  INFO 1 --- [           main] .s.e.SpringBootDataJpaExampleApplication : Starting SpringBootDataJpaExampleApplication v0.0.1-SNAPSHOT on 582c035536e4 with PID 1 (/spring-boot-app.jar started by root in /)
  spring-boot-app-container_1  | 2018-10-26 01:24:48.752  INFO 1 --- [           main] .s.e.SpringBootDataJpaExampleApplication : No active profile set, falling back to default profiles: default
  spring-boot-app-container_1  | 2018-10-26 01:24:48.875  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@28c97a5: startup date [Fri Oct 26 01:24:48 UTC 2018]; root of context hierarchy
  spring-boot-app-container_1  | 2018-10-26 01:24:51.022  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
  spring-boot-app-container_1  | 2018-10-26 01:24:51.063  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  spring-boot-app-container_1  | 2018-10-26 01:24:51.065  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
  spring-boot-app-container_1  | 2018-10-26 01:24:51.218  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
  spring-boot-app-container_1  | 2018-10-26 01:24:51.218  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2399 ms
  spring-boot-app-container_1  | 2018-10-26 01:24:51.335  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
  spring-boot-app-container_1  | 2018-10-26 01:24:51.340  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
  spring-boot-app-container_1  | 2018-10-26 01:24:51.341  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  spring-boot-app-container_1  | 2018-10-26 01:24:51.341  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
  spring-boot-app-container_1  | 2018-10-26 01:24:51.341  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
  spring-boot-app-container_1  | 2018-10-26 01:24:51.895 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
  spring-boot-app-container_1  | 
  spring-boot-app-container_1  | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  spring-boot-app-container_1  | 
  spring-boot-app-container_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  spring-boot-app-container_1  |  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]
  </code></pre>
  
  <p>How to fix this error:
  <em>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure</em></p>
  
  <p>I can see lot of answers on this forum for this similar error message. Tried bunch of those options/answers, but that didn't work. 
  No answer talks about this  combination (linux + spring boot + mysql + docker compose)</p>
  
  <p><em>Note: This has worked fine without using docker-compose. Have already mentioned the same in above description. <strong>Am I making any mistake in docker-compose file or application properties file?</em></strong> </p>