如何从 docker 化 spring 启动应用程序连接 docker 数据库?
How to connect docker db from dockerized spring boot app?
我是 docker 东西的初学者,我尝试学习,但我很难坚持。
当我仅 运行 来自 docker-compose.yml 和 运行 的数据库时,spring 应用调用了 docker deneme 直接来自 IDE 然后它连接到数据库,没问题。但是当我尝试从 docker-compose.yml 文件中同时对 运行 进行操作时,应用程序无法连接到数据库。我的 docker-compose.yml 文件是:
version: '3.9'
services:
mysql:
platform: linux/amd64
image: mysql:8
container_name: mysqlcustomerdb
domainname: nishcustomer
hostname: localhost
ports:
- '3306:3306'
volumes:
- ~/apps/mysql:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_PASSWORD=1234
- MYSQL_DATABASE=mysqlcustomerdb
- DATABASE_HOST=mysqlcustomerdb
- MYSQL_ROOT_HOST=%
- lower_case_table_names=1
- init-connect='GRANT CREATE USER ON . TO 'root'@'%';FLUSH PRIVILEGES;'
dockerdeneme:
container_name: dockerdeneme
domainname: nishcustomer
hostname: localhost
image: dockerdeneme
build: ./
ports:
- '8080:8080'
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_PASSWORD=1234
- MYSQL_USER=root
- MYSQL_DATABASE=mysqlcustomerdb
depends_on:
- mysql
volumes:
cache:
driver: local
docker ps
的结果是:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb0037e10702 mysql:8 "docker-entrypoint.s…" 22 seconds ago Up 21 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysqlcustomerdb
如果我 运行 来自 IDE 的应用程序和以下 application.properties 那么它运行良好:
spring.datasource.url=jdbc:mysql://localhost:3306/mysqlcustomerdb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
这是docker化应用失败的输出:
2022-04-14 21:28:56.106 INFO 1 --- [ main] c.e.d.DockerdenemeApplication : Starting DockerdenemeApplication v0.0.1-SNAPSHOT using Java 11.0.14.1 on localhost with PID 1 (/app.jar started by root in /)
2022-04-14 21:28:56.107 INFO 1 --- [ main] c.e.d.DockerdenemeApplication : No active profile set, falling back to 1 default profile: "default"
2022-04-14 21:28:56.467 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-04-14 21:28:56.493 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 21 ms. Found 1 JPA repository interfaces.
2022-04-14 21:28:57.024 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-14 21:28:57.037 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-14 21:28:57.037 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-14 21:28:57.105 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-14 21:28:57.105 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 937 ms
2022-04-14 21:28:57.250 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-04-14 21:28:57.256 WARN 1 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2022-04-14 21:28:58.334 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
我想我在端口映射或 hostname/domain 名称方面做错了,但我卡住了。
谁能告诉我我做错了什么,端口或主机名等?
localhost 不适用于 docker-compose 应用程序。作为总结,使用主机的 ip 或配置良好的 docker 网络。检查:
特别注意:extra_hosts:
- How to connect API with Web SPA through docker
这里有一个与 mysql 和 wordpress 类似的 docker-compose:
我是 docker 东西的初学者,我尝试学习,但我很难坚持。
当我仅 运行 来自 docker-compose.yml 和 运行 的数据库时,spring 应用调用了 docker deneme 直接来自 IDE 然后它连接到数据库,没问题。但是当我尝试从 docker-compose.yml 文件中同时对 运行 进行操作时,应用程序无法连接到数据库。我的 docker-compose.yml 文件是:
version: '3.9'
services:
mysql:
platform: linux/amd64
image: mysql:8
container_name: mysqlcustomerdb
domainname: nishcustomer
hostname: localhost
ports:
- '3306:3306'
volumes:
- ~/apps/mysql:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_PASSWORD=1234
- MYSQL_DATABASE=mysqlcustomerdb
- DATABASE_HOST=mysqlcustomerdb
- MYSQL_ROOT_HOST=%
- lower_case_table_names=1
- init-connect='GRANT CREATE USER ON . TO 'root'@'%';FLUSH PRIVILEGES;'
dockerdeneme:
container_name: dockerdeneme
domainname: nishcustomer
hostname: localhost
image: dockerdeneme
build: ./
ports:
- '8080:8080'
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_PASSWORD=1234
- MYSQL_USER=root
- MYSQL_DATABASE=mysqlcustomerdb
depends_on:
- mysql
volumes:
cache:
driver: local
docker ps
的结果是:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb0037e10702 mysql:8 "docker-entrypoint.s…" 22 seconds ago Up 21 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysqlcustomerdb
如果我 运行 来自 IDE 的应用程序和以下 application.properties 那么它运行良好:
spring.datasource.url=jdbc:mysql://localhost:3306/mysqlcustomerdb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
这是docker化应用失败的输出:
2022-04-14 21:28:56.106 INFO 1 --- [ main] c.e.d.DockerdenemeApplication : Starting DockerdenemeApplication v0.0.1-SNAPSHOT using Java 11.0.14.1 on localhost with PID 1 (/app.jar started by root in /)
2022-04-14 21:28:56.107 INFO 1 --- [ main] c.e.d.DockerdenemeApplication : No active profile set, falling back to 1 default profile: "default"
2022-04-14 21:28:56.467 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-04-14 21:28:56.493 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 21 ms. Found 1 JPA repository interfaces.
2022-04-14 21:28:57.024 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-14 21:28:57.037 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-14 21:28:57.037 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-14 21:28:57.105 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-14 21:28:57.105 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 937 ms
2022-04-14 21:28:57.250 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-04-14 21:28:57.256 WARN 1 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2022-04-14 21:28:58.334 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
我想我在端口映射或 hostname/domain 名称方面做错了,但我卡住了。
谁能告诉我我做错了什么,端口或主机名等?
localhost 不适用于 docker-compose 应用程序。作为总结,使用主机的 ip 或配置良好的 docker 网络。检查:
特别注意:extra_hosts:
- How to connect API with Web SPA through docker
这里有一个与 mysql 和 wordpress 类似的 docker-compose: