Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro de conexão com o banco de dados ao rodar aplicação do DOCKER

Comando Docker com parâmetros da variavel de ambiente e banco de dados

D:@projetos\service-api-aula>docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE='prod' -e FORUM_URL='jdbc:mysql://localhost:3306/aula' -e FORUM_USERNAME='root' -e FORUMPASSOWRD='@Pecoma1975' -e FORUMJWT_SECRET='1234567890A' mcp/aula

. __ _ __ _ _ /\ / _'_ _ _ _()_ _ __ _ \ \ \ ( ( )___ | ' | '| | ' / ` | \ \ \ \/ ___)| |)| | | | | || (| | ) ) ) ) ' |__| .|| ||_| |_, | / / / / =========||==============|__/=//// :: Spring Boot :: (v2.5.7)

2022-03-27 23:03:20.063 INFO 1 --- [ main] br.com.mcp.ServiceApiAulaApplication : Starting ServiceApiAulaApplication v0.0.1 using Java 11.0.14.1 on 157e0b249e97 with PID 1 (/app.jar started by spring in /) 2022-03-27 23:03:20.066 INFO 1 --- [ main] br.com.mcp.ServiceApiAulaApplication : The following profiles are active: 'prod' 2022-03-27 23:03:20.979 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2022-03-27 23:03:21.039 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 49 ms. Found 3 JPA repository interfaces. 2022-03-27 23:03:21.756 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-03-27 23:03:21.771 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-03-27 23:03:21.771 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55] 2022-03-27 23:03:21.824 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-03-27 23:03:21.824 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1691 ms 2022-03-27 23:03:21.996 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-03-27 23:03:22.049 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final 2022-03-27 23:03:22.188 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-03-27 23:03:22.294 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-03-27 23:03:23.411 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.

**com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

2 respostas
solução!

Oi Marcos,

Como você está utilizando o MYSQL como banco de dados, ao passar a url jdbc:mysql://localhost:3306/aula o spring vai entender que existe um banco de dados rodando no localhost, ou seja, que o MYSQL está rodando dentro do container com a aplicação, sendo que ele não está e isso causa o erro.

Você precisa ter outro container rodando o mysql e passar na url o nome do container. Exemplo: jdbc:mysql://mysql_container:3306/aula

Showwwww Rodrigo... Entendi, então tenho que ter um banco de dados rodando dentro do container... Saquei, vou procurar algum artigo ou video de como criar o banco dentro do docker.... Valeu Rodrigo