1
resposta

docker-compose api java .jar com mysql

Bom dia, estou tentando subir um compose com um jar de uma api spring boot java e um mysql, tentei inúmeras coisas sem sucesso... se alguém puder ajudar fico extremamente grato!

Ao tentar subir o docker-compose o db inicia, após o db a aplicação spring começa a ser executada e recebo uma exception: printscreen

spring-boot-2  | 2019-10-06 12:19:20.548  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 5000 (http)
spring-boot-2  | 2019-10-06 12:19:20.610  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
spring-boot-2  | 2019-10-06 12:19:20.612  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
spring-boot-2  | 2019-10-06 12:19:20.862  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/v1]     : Initializing Spring embedded WebApplicationContext
spring-boot-2  | 2019-10-06 12:19:20.862  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4423 ms
spring-boot-2  | 2019-10-06 12:19:21.129  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
spring-boot-2  | 2019-10-06 12:19:21.133  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
spring-boot-2  | 2019-10-06 12:19:21.135  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
spring-boot-2  | 2019-10-06 12:19:21.135  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
spring-boot-2  | 2019-10-06 12:19:21.135  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
spring-boot-2  | 2019-10-06 12:19:22.161 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
spring-boot-2  | 
spring-boot-2  | java.sql.SQLException: null,  message from server: "Host '172.24.0.3' is not allowed to connect to this MySQL server"
spring-boot-2  |     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
spring-boot-2  |     at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.23.jar!/:na]

docker-compose.yml

version: '3'
services:
  db:
    image: mysql/mysql-server:8.0.17
    environment:
      - MYSQL_DATABASE=movies_db
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_ROOT_HOST=%
    ports:
      - 3306:3306
    restart: always
  spring-boot:
    image: imagem-spring-boot
    build:
      context: . 
      dockerfile: Dockerfile
    container_name: spring-boot-api-2
    expose:
      - 5000
    restart: always
    depends_on:
      - db

application.properties

springfox.documentation.swagger.v2.path=/api-docs
server.contextPath=/v1
server.port=5000
spring.jackson.date-format=io.swagger.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false


## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://db:3306/movies_db?useSSL=false
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
1 resposta

Este erro ocorre devido à configuração padrão que seu banco de dados MySQL está usando. Essa configuração permite conexões apenas do usuário 'root' quando proveniente de 'localhost' e não de outros intervalos de endereços IP.