Docker Community Forums

Share and learn in the Docker community.

MySql runs with docker-compose, but spring-boot app doesn't run

I’m trying to run a project that I built in spring boot with docker, which ran fine before docker, but since I tried to integrate it with docker-compose, I’ve been running into issues getting the spring-boot app to run.

My docker-compose.yml looks like this:

version: '3.1'

services:
  web:
    image: kynphlee/jobsearchtracker
    container_name: web
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/jobsearchtracker?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
      SPRING_DATASOURCE_USERNAME: 'user'
      SPRING_DATASOURCE_PASSWORD: 'p9goebXG7549'
    networks:
      - jobsearchtracker-net
    ports:
      - 8080:8080
    depends_on:
      - db

  db:
    image: mysql/mysql-server
    container_name: db
    ports:
      - 3306:3306
    expose:
      - 3306
    networks:
      - jobsearchtracker-net
    environment:
      MYSQL_USER: 'user'
      MYSQL_PASSWORD: 'p9goebXG7549'
      MYSQL_DATABASE: 'jobsearchtracker'
      MYSQL_ROOT_PASSWORD: 'p9goebXG7549'

networks:
  jobsearchtracker-net:

And my pom.xml contains the following:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nexus</groupId>
    <artifactId>job-search-tracker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JobSearchTracker</name>
    <description>Genius Explorer</description>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--  Testing  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>com.h2database</groupId>-->
<!--            <artifactId>h2</artifactId>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <!-- Spring AOP Integration --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sourceforge.plantuml/plantuml -->
        <dependency>
            <groupId>net.sourceforge.plantuml</groupId>
            <artifactId>plantuml</artifactId>
            <version>1.2020.15</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/guru.nidi/graphviz-java -->
        <dependency>
            <groupId>guru.nidi</groupId>
            <artifactId>graphviz-java</artifactId>
            <version>0.17.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

When I run docker-compose up , I tend to get the following:

( base) kynphlee@NeXuS:~/projects/JobSearchTracker$ sudo docker-compose up
    [sudo] password for kynphlee: 
    Creating db ... done
    Creating web ... done
    Attaching to db, web
    db     | [Entrypoint] MySQL Docker Image 8.0.21-1.1.17
    db     | [Entrypoint] Initializing database
    db     | 2021-04-19T07:18:23.814830Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 20
    db     | 2021-04-19T07:18:23.820940Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    web    | 
    web    |   .   ____          _            __ _ _
    web    |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    web    | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    web    |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
    web    |   '  |____| .__|_| |_|_| |_\__, | / / / /
    web    |  =========|_|==============|___/=/_/_/_/
    web    |  :: Spring Boot ::        (v2.1.5.RELEASE)
    web    | 
    web    | 2021-04-19 07:18:25,385 INFO  [main] org.springframework.boot.StartupInfoLogger: Starting JobSearchTrackerApplication v0.0.1-SNAPSHOT on deef5b51cc0d with PID 1 (/app.jar started by root in /)
    web    | 2021-04-19 07:18:25,388 INFO  [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
    db     | 2021-04-19T07:18:25.410730Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    web    | 2021-04-19 07:18:26,054 INFO  [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate: Bootstrapping Spring Data repositories in DEFAULT mode.
    web    | 2021-04-19 07:18:26,128 INFO  [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate: Finished Spring Data repository scanning in 65ms. Found 4 repository interfaces.
    web    | 2021-04-19 07:18:26,544 INFO  [main] org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5ab41374] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    web    | 2021-04-19 07:18:27,015 INFO  [main] org.springframework.boot.web.embedded.tomcat.TomcatWebServer: Tomcat initialized with port(s): 8080 (http)
    web    | 2021-04-19 07:18:27,033 INFO  [main] org.apache.juli.logging.DirectJDKLog: Initializing ProtocolHandler ["http-nio-8080"]
    web    | 2021-04-19 07:18:27,051 INFO  [main] org.apache.juli.logging.DirectJDKLog: Starting service [Tomcat]
    web    | 2021-04-19 07:18:27,052 INFO  [main] org.apache.juli.logging.DirectJDKLog: Starting Servlet engine: [Apache Tomcat/9.0.19]
    web    | 2021-04-19 07:18:27,134 INFO  [main] org.apache.juli.logging.DirectJDKLog: Initializing Spring embedded WebApplicationContext
    web    | 2021-04-19 07:18:27,135 INFO  [main] org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext: Root WebApplicationContext: initialization completed in 1698 ms
    web    | 2021-04-19 07:18:27,336 INFO  [main] com.zaxxer.hikari.HikariDataSource: HikariPool-1 - Starting...
    db     | 2021-04-19T07:18:27.755054Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
    web    | 2021-04-19 07:18:28,442 ERROR [main] com.zaxxer.hikari.pool.HikariPool: HikariPool-1 - Exception during pool initialization.
    web    | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
    web    | 
    web    | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    web    |        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    web    |        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    web    |        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    web    |        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    web    |        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    web    |        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    web    |        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
    web    |        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
    web    |        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
    web    |        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
    web    |        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
    web    |        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
    web    |        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
    web    |        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
    web    |        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
    web    |        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
    web    |        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319)
    web    |        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356)
    web    |        at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:73)
    web    |        at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:142)
    web    |        at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:113)
    web    |        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$798ad5e3.CGLIB$jpaVendorAdapter$9(<generated>)
    web    |        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$798ad5e3$$FastClassBySpringCGLIB$$fc177e39.invoke(<generated>)
    web    |        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    web    |        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
    web    |        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$798ad5e3.jpaVendorAdapter(<generated>)
    web    |        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    web    |        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    web    |        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    web    |        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    web    |        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    web    |        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    web    |        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    web    |        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1248)
    web    |        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1168)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    web    |        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    web    |        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    web    |        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1248)
    web    |        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1168)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    web    |        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    web    |        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    web    |        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    web    |        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    web    |        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
    web    |        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    web    |        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    web    |        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
    web    |        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    web    |        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    web    |        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    web    |        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    web    |        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    web    |        at com.nexus.jobsearchtracker.JobSearchTrackerApplication.main(JobSearchTrackerApplication.java:10)
    web    |        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    web    |        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    web    |        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    web    |        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    web    |        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    web    |        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    web    |        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    web    |        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
    web    | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
    web    | 
    web    | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    web    |        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    web    |        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    web    |        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    web    |        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    web    |        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    web    |        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    web    |        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    web    |        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    web    |        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    web    |        at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
    web    |        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
    web    |        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    web    |        ... 84 common frames omitted
    web    | Caused by: java.net.ConnectException: Connection refused (Connection refused)
    web    |        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    web    |        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    web    |        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    web    |        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    web    |        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    web    |        at java.base/java.net.Socket.connect(Socket.java:609)
    web    |        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    web    |        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
web    |        ... 87 common frames omitted

And this error as well:

web    | 2021-04-19 07:18:31,590 WARN  [main] org.springframework.context.support.AbstractApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution

When I check the status of my containers, this is what I have:

(base) kynphlee@NeXuS:~/projects/practice/docker$ sudo docker container ls -a
[sudo] password for kynphlee: 
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                     PORTS                               NAMES
deef5b51cc0d        kynphlee/jobsearchtracker   "java -jar /app.jar"     3 minutes ago       Exited (1) 3 minutes ago                                       web
8c109df1be0d        mysql/mysql-server          "/entrypoint.sh mysq…"   3 minutes ago       Up 3 minutes (healthy)     0.0.0.0:3306->3306/tcp, 33060/tcp   db

If I comment the web section, I get no errors. Any suggestions on what could be causing this?