title: Spring boot app cant connect to mysql db - docker-compose
body: <p>I tried many ways and every time the application runs in the container I get such errors:</p>
  <pre><code>WARN 1 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata
  
  java.lang.IllegalStateException: Cannot get a connection as the driver manager is not properly initialized
  </code></pre>
  <p>my docker-compose:</p>
  <pre><code>version: '3.8'
  networks:
    my_network:
      external: true
  services:
    mysql:
      container_name: configs_mysql
      image: mysql:latest
      restart: always
      ports:
        - &quot;3306:3306&quot;
      environment:
        MYSQL_ROOT_PASSWORD: scrap
        MYSQL_USER: scrap
        MYSQL_PASSWORD: scrap
        MYSQL_DATABASE: configs
      networks:
        - my_network
    web:
      build: .
      image: scrapyconfigmanager_web
      ports:
        - &quot;8181:8181&quot;
      networks:
        - my_network
      depends_on:
        - mysql
  </code></pre>
  <p>my Dockerfile:</p>
  <pre><code>FROM openjdk:11
  # Copy source code to temporary building location
  WORKDIR /source_code
  COPY . .
  RUN ./gradlew bootJar
  
  WORKDIR /scrapy
  RUN cp /source_code/build/libs/ScrapyConfigManager-1.0.jar .
  
  EXPOSE 8181
  ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;ScrapyConfigManager-1.0.jar&quot;]
  </code></pre>
  <p>my hibernate.cfg:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot; &quot;http://hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://configs_mysql:3306/configs&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.username&quot;&gt;scrap&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.password&quot;&gt;scrap&lt;/property&gt;
          &lt;property name=&quot;hibernate.show_sql&quot;&gt;true&lt;/property&gt;
          &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/property&gt;
          &lt;mapping class=&quot;org.scrapeusz.model.OlxCar&quot; /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  <p>Both containers start up, but after a while the spring container stops.
  Below I paste part of the code from the terminal after the &quot;docker-compose up&quot; command:</p>
  <pre><code>Creating configs_mysql ... done
  Creating scrapyconfigmanager_web_1 ... done
  Attaching to configs_mysql, scrapyconfigmanager_web_1
  configs_mysql | 2022-07-21 10:34:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
  configs_mysql | 2022-07-21 10:34:49+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
  configs_mysql | 2022-07-21 10:34:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
  configs_mysql | 2022-07-21 10:34:49+00:00 [Note] [Entrypoint]: Initializing database files
  configs_mysql | 2022-07-21T10:34:49.504138Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.29) initializing of server in progress as process 42
  configs_mysql | 2022-07-21T10:34:49.515175Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  configs_mysql | 2022-07-21T10:34:49.795876Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  web_1    | 
  web_1    |   .   ____          _            __ _ _
  web_1    |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  web_1    | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  web_1    |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  web_1    |   '  |____| .__|_| |_|_| |_\__, | / / / /
  web_1    |  =========|_|==============|___/=/_/_/_/
  web_1    |  :: Spring Boot ::                (v2.6.6)
  web_1    | 
  web_1    | 2022-07-21 10:34:50.905  INFO 1 --- [           main] org.scrapeusz.ScrapyConfigManager        : Starting ScrapyConfigManager using Java 11.0.15 on 71d7d66bbd3b with PID 1 (/scrapy/ScrapyConfigManager-1.0.jar started by root in /scrapy)
  web_1    | 2022-07-21 10:34:50.910  INFO 1 --- [           main] org.scrapeusz.ScrapyConfigManager        : No active profile set, falling back to 1 default profile: &quot;default&quot;
  configs_mysql | 2022-07-21T10:34:51.393524Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
  web_1    | 2022-07-21 10:34:52.239  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
  web_1    | 2022-07-21 10:34:52.255  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  web_1    | 2022-07-21 10:34:52.255  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
  web_1    | 2022-07-21 10:34:52.364  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
  web_1    | 2022-07-21 10:34:52.364  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1388 ms
  web_1    | 2022-07-21 10:34:52.577  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.0.0.Final
  web_1    | 2022-07-21 10:34:53.269  WARN 1 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001002: Using built-in connection pool (not intended for production use)
  web_1    | 2022-07-21 10:34:53.275  INFO 1 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver
  web_1    | 2022-07-21 10:34:53.275  INFO 1 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001012: Connecting with JDBC URL [jdbc:mysql://configs_mysql:3306/configs]
  web_1    | 2022-07-21 10:34:53.275  INFO 1 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001001: Connection properties: {password=****, user=scrap}
  web_1    | 2022-07-21 10:34:53.276  INFO 1 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001003: Autocommit mode: false
  web_1    | 2022-07-21 10:34:53.279  INFO 1 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001115: Connection pool size: 20 (min=1)
  web_1    | 2022-07-21 10:34:53.407  WARN 1 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata
  web_1    | 
  web_1    | java.lang.IllegalStateException: Cannot get a connection as the driver manager is not properly initialized
  web_1    |  at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:259) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:284) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:177) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:36) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:100) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:255) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:230) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  web_1    |  at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:48) ~[hibernate-core-6.0.0.Final.jar!/:6.0.0.Final]
  </code></pre>