Docker: can't connect Spring Boot & MYSQL

房东的猫 提交于 2021-02-20 02:15:45

问题


I try containerize my project with Dockerfile or Docker-compose. Firstly, I create container from mysql with command: docker run --name ms -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql then I create database and table in container and put data in table, then I build image of my project: docker build -f Dockerfile -t week . and i have logs:

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.

next I create container with my image: docker run -p 8080:8080 --name weeek --link ms:mysql -d week My weeek logs:

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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 logs 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

docker ps


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

And if i send a request through postman I get:

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

or if i put to the browser: localhost:8080/products/allProducts i get: Page not found

All my project here: Project


回答1:


I have noticed a couple of issues on your code:

  1. Docker-compose file

    • using volume ./mysql-data:/var/lib/mysql you are more likely to encounter this error

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

      change it to any directory e.g /data/mysql

    • the probability that Java service container will start first before database container is very high. Hence there is a need to add restart-on-failure option or wait-for-it.sh or sleep option.These options will enable service container to wait for database container to start.

          java:
             restart: on-failure
      
  2. Properties file.

    Java Service property file is pointing to none existence container ms

    change it to

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

    instead of

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

after the above changes run

docker-compose up --build

Github Pull Request



来源:https://stackoverflow.com/questions/61167859/docker-cant-connect-spring-boot-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!