Docker:无法连接 Spring Boot 和 MYSQL

2024-01-12

我尝试使用 Dockerfile 或 Docker-compose 将我的项目容器化。首先,我使用命令从 mysql 创建容器:docker run --name ms -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql然后我在容器中创建数据库和表并将数据放入表中,然后构建项目的映像:docker build -f Dockerfile -t week .我有日志:

Sending build context to Docker daemon  212.1MB
Step 1/4 : FROM openjdk
---> 30503f5328a0
Step 2/4 : ADD target/week10-1.0-SNAPSHOT.jar week10.jar
---> 727648b3ec10
Step 3/4 : EXPOSE 8080
---> Running in d60c2566ac0b
Removing intermediate container d60c2566ac0b
---> 59b94f7abc52
Step 4/4 : ENTRYPOINT ["java","-jar","week10.jar"]
---> Running in 20627cbee09e
Removing intermediate container 20627cbee09e
---> 725bc9303c08
Successfully built 725bc9303c08
Successfully tagged week:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It
is recommended to double check and reset permissions for sensitive files and directories.

接下来我用我的图像创建容器:docker run -p 8080:8080 --name weeek --link ms:mysql -d week我的一周日志:

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

2020-04-12 05:51:40.095  INFO 1 --- [           main] com.fruitshop.Application                : Starting Application v1.0-SNAPSHOT on 0a26be9daad3 with PID 1 (/week10.jar started by root in /)
2020-04-12 05:51:40.237  INFO 1 --- [           main] com.fruitshop.Application                : No active profile set, falling back to default profiles: default
2020-04-12 05:51:50.398  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-04-12 05:51:51.647  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1144ms. Found 1 JPA repository interfaces.
2020-04-12 05:52:00.131  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-12 05:52:00.371  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-12 05:52:00.377  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-04-12 05:52:01.025  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-12 05:52:01.031  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 20029 ms
2020-04-12 05:52:05.034  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-04-12 05:52:12.915  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-04-12 05:52:15.289  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-04-12 05:52:17.719  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-04-12 05:52:19.813  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-04-12 05:52:21.547  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-04-12 05:52:31.942  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-04-12 05:52:32.009  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-04-12 05:52:36.313  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-04-12 05:52:43.551  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-12 05:52:52.483  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-12 05:52:52.502  INFO 1 --- [           main] com.fruitshop.Application                : Started Application in 79.232 seconds (JVM running for 88.015)

docker 日志 ms


2020-04-12 04:50:12+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
2020-04-12 04:50:16+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-04-12 04:50:16+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
2020-04-12 04:50:16+00:00 [Note] [Entrypoint]: Initializing database files
2020-04-12T04:50:16.737441Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-04-12T04:50:16.737895Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 41
2020-04-12T04:50:23.656403Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-04-12 04:50:41+00:00 [Note] [Entrypoint]: Database files initialized
2020-04-12 04:50:41+00:00 [Note] [Entrypoint]: Starting temporary server
2020-04-12T04:50:42.568802Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-04-12T04:50:42.569282Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 91
2020-04-12T04:50:44.820291Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-04-12T04:50:44.838763Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2020-04-12T04:50:44.956526Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2020-04-12 04:50:45+00:00 [Note] [Entrypoint]: Temporary server started.
2020-04-12T04:50:45.205952Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2020-04-12 04:51:30+00:00 [Note] [Entrypoint]: Stopping temporary server
2020-04-12T04:51:30.467028Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.19).
2020-04-12T04:51:32.552114Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19)  MySQL Community Server - GPL.
2020-04-12 04:51:33+00:00 [Note] [Entrypoint]: Temporary server stopped

2020-04-12 04:51:33+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2020-04-12T04:51:34.552483Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-04-12T04:51:34.552877Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1
2020-04-12T04:51:36.174094Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-04-12T04:51:36.186916Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2020-04-12T04:51:36.315329Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2020-04-12T04:51:36.509687Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
mbind: Operation not permitted

码头工人


CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
0a26be9daad3        week                "java -jar week10.jar"   15 hours ago        Up 15 hours         0.0.0.0:8080->8080/tcp              weeek
e2b97b4a01bd        mysql               "docker-entrypoint.s…"   16 hours ago        Up 16 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   ms

如果我通过邮递员发送请求,我会得到:

Could not get any response
There was an error connecting to http://localhost:8080/products/allProducts.

或者如果我输入浏览器:localhost:8080/products/allProducts i get: Page not found

我所有的项目都在这里:Project https://github.com/Asel06/Neobis-Tasks/tree/master/week10


我注意到您的代码有几个问题:

  1. Docker-compose 文件

    • 使用量./mysql-data:/var/lib/mysql你更有可能遇到 这个错误

      [ERROR] The designated data directory /var/lib/mysql/ is unusable

      将其更改为任何目录,例如/data/mysql

    • 的概率Java服务容器将首先开始之前数据库容器非常高。因此需要添加restart-on-failure option or wait-for-it.sh or sleep option.这些选项将启用服务容器等待数据库容器开始。

          java:
             restart: on-failure
      
  2. 属性文件。

    Java 服务属性文件指向不存在的容器ms

    将其更改为

    spring.datasource.url=jdbc:mysql://db:3306/fruitshop?serverTimezone=UTC

    代替

    spring.datasource.url=jdbc:mysql://ms:3306/fruitshop?serverTimezone=UTC

执行上述更改后

docker-compose up --build

Github 拉取请求 https://github.com/Asel06/Neobis-Tasks/pulls

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker:无法连接 Spring Boot 和 MYSQL 的相关文章

随机推荐

  • 与 Jedis 客户端的事务发生 Redis 异常

    为了避免在我的 Redis 通道中出现重复 我通过在 Redis 集中保留索引来检查消息是否已经存在 以下是我的实现 然而 它给出了一个例外 redis clients jedis exceptions JedisDataException
  • CUDA 目标的 Numba 和 guvectorize:代码运行速度比预期慢

    值得注意的细节 大型数据集 1000 万 x 5 200 x 1000 万 x 5 主要是 Numpy 每次跑步后需要更长的时间 使用Spyder3 Windows 10 首先是尝试将 guvectorize 与以下函数一起使用 我传入一堆
  • spring中动态更新@value注解的字段

    我正在尝试动态更新 value我的应用程序中的注释字段 首先 这个应用程序有一个自定义属性源 源是Map
  • 为什么像素是浮动的?

    Canvas类的所有方法都使用float类型的坐标 但为什么 据我所知 这个坐标意味着显示上指向的像素数 像素可以分离吗 某些设备支持亚像素精度 在标准 LCD 屏幕上 一个像素实际上是三个子像素 一个红色 一个绿色 一个蓝色 红绿蓝 子像
  • WPF拖动距离阈值

    我有一个带有两个 WPF 树视图的程序 允许在两者之间拖放 问题是 打开 关闭树视图上的项目可能很烦人 因为在按住鼠标左键的同时将鼠标移动一个像素会触发拖 放功能 有没有某种方法可以指定鼠标在被视为拖放之前应移动多远 有一个系统参数可以实现
  • 将 Spring 配置划分到多个项目

    我们有很多项目使用相同的代码库 后端代码 只是前端往往有所不同 我们认为最好的方法是将后端和前端分成不同的项目 Engine and 项目名 现在这些是 Spring 项目 因此 如果我们也划分 Spring 配置 这似乎才合乎逻辑 数据库
  • 当 Ionic 电子商务应用程序中购物车中的产品数量增加时,总价格不会更新

    我正在研究离子电子商务应用程序并使用 Laravel 中的 API 我已将产品添加到购物车中 但是当我增加购物车中的产品数量时 产品价格增加 但总价格没有更新 并且从购物车中删除产品时 也没有更新价格 这是我的购物车 html
  • Azure DevOps Server 2019 保留策略不再有效

    上周 我们已在 ADS 2019 1 服务器上从 TFVC 迁移到 Git 在我们的验证管道中 我们有积极的保留政策 它设置为保留 2 天 使用分支过滤器 进行 10 个良好构建 并清除所有复选框 ADS 将其写为 refs heads 我
  • 在 Python 2.6 上安装 geopandas

    这是我之前的问题的延续Python 中的地理空间分析 https stackoverflow com questions 33427170 geospatial analytics in python 我开始了一个新问题 以使这两个问题在逻
  • 折线不在道路上:它从一个点直接延伸到另一个点

    我的地图包含多个点来点击用户经过的位置 但是折线不显示在道路上 但显示从一个标记到另一个标记的直线 https i stack imgur com 3RWlU jpg 我希望我的折线穿过马路 当道路转弯时 它也应该转弯 这是我的相关代码 d
  • SVN 挂钩不工作

    我有一个分支和主干的服务器存储库 分支是所有团队成员的存储库 我正在尝试使用svn hooks仅在我的分支下的仓库中 但它似乎工作得不好 以下是我尝试采取的步骤 检查过了my repo从远程服务器的branch my repo 自本地回购以
  • 如何使用 CMIS 在 Alfresco 中进行批量更新

    是否可以在露天使用 CMIS 进行批量更新 我有不同的文档类型 每个文档类型在露天存储库中都有多个文档 现在我的要求是 如果我要更新任何文档的任何单个属性 那么它应该反映相同类型的所有文档 我可以使用 CMIS 执行此操作吗 如果是 请提供
  • Redux - 一个与多个减速器

    我来自 Elm 社区 在 Elm 中 每个应用程序都有自己的视图 模型和状态 并且基本上采用与 redux 非常相似的方法来解决问题 不管怎样 我发现自己在多个减速器的想法中挣扎 在 Elm 中 我习惯为所有操作 消息 创建一个单独的文件
  • 如何从 angularjs ng-route 中删除哈希#

    我试图使用 locationProvider 从 Angular js 中的 url 路由中删除主题标签 但它给了我错误 app js var eclassApp angular module eclassApp ngRoute eclas
  • 在python中获取每个月的最后一个星期五

    我想要接下来三个月的每个月的最后一个星期五 Friday date datetime date today while Friday date weekday 4 Friday date datetime timedelta 1 这给了我最
  • Slack 应用程序和本地存储

    我们正在计划一个 Slack 应用程序 但我们需要本地存储 我们不想在我们的服务器上存储用户 频道的任何数据 我们希望将其永久或临时保存在用户的本地计算机或 Slack 服务器中 有办法做吗 Thanks 以下是我对这个主题的想法 本地存储
  • GWT 和 Vaadin 之间的差异

    谁能建议 GWT 或 Vaadin 是否是设计应用程序的更好选择 另外 编码风格有什么区别 在 GWT 中 应用程序逻辑通常在客户端运行 它仅在需要读取 保存某些数据时调用服务器 在 Vaadin 中 应用程序逻辑位于服务器端 每次用户交互
  • 添加 IPV6_V6ONLY 标志的动机是什么?

    在 IPv6 网络中 IPV6 V6ONLY 标志用于确保套接字仅使用 IPv6 特别是 IPv4 到 IPv6 的映射不会用于该套接字 在许多操作系统上 默认情况下未设置 IPV6 V6ONLY 但在某些操作系统 例如 Windows 7
  • 报告 API 503 后端错误

    在过去的几周里 我用来从报告 API 检索用户使用情况报告的批处理任务一直失败 并出现以下响应 com google api client googleapis json GoogleJsonResponseException 503 OK
  • Docker:无法连接 Spring Boot 和 MYSQL

    我尝试使用 Dockerfile 或 Docker compose 将我的项目容器化 首先 我使用命令从 mysql 创建容器 docker run name ms p 3306 3306 e MYSQL ROOT PASSWORD pas