Docker-Compose Java Springboot and MySQL

huangapple 未分类评论52阅读模式
英文:

Docker-Compose Java Springboot and MySQL

问题

我尝试使用docker-compose来将MySQL服务器容器化,并首先启动MySQL容器,然后启动一个Spring Boot容器与MySQL容器连接。

但是连接失败(在终端日志中的完整版本请参见下面)
web_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: 通信链接失败

我应该如何更改以先启动MySQL容器,然后将Spring Boot服务器与MySQL连接起来?

我的终端日志



$ docker-compose up
正在创建网络“backend_default” ,并使用默认驱动程序
已创建 backend_db_1 ... 完成
已创建 backend_web_1 ... 完成
附加到 backend_db_1、backend_web_1
db_1   | 2020-03-15 05:59:09+00:00 [Note] [Entrypoint]: MySQL Server 8.0.19-1debian10 的入口脚本已启动。
db_1   | 2020-03-15 05:59:09+00:00 [Note] [Entrypoint]: 正在切换到专用用户 'mysql'
...
...(日志中的其余部分)
...
web_1  | >> 已连接到 MySQL 数据库! <<
...
...(日志中的其余部分)
...

我的Dockerfile
下载所需的镜像并运行wrapper.sh
wrapper.sh将不断检查MySQL容器是否成功运行
如果是,则启动Spring Boot;否则不启动

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ARG SH_FILE=src/main/resources/*.sh
COPY ${SH_FILE} wrapper.sh
RUN /bin/sh -c "apk add --no-cache bash"
RUN bash -c 'touch /app.jar'
RUN bash -c 'chmod +x /wrapper.sh'

ENTRYPOINT ["/bin/bash", "/wrapper.sh"]

我的wrapper.sh文件
不断检查MySQL服务器

#!/usr/bin/env bash
while ! exec 6<&amp;/dev/tcp/${DATABASE_HOST}/${DATABASE_PORT}; do
    echo &quot;Trying to connect to MySQL at ${DATABASE_HOST}:${DATABASE_PORT}...&quot;
    sleep 10
done
echo &quot;&gt;&gt; connected to MySQL database! &lt;&lt;&quot;
java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=containers -jar /app.jar

我的docker-compose文件

version: &quot;2&quot;
services:
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
      - MYSQL_DATABASE=example
      - MYSQL_USER=shihao
      - MYSQL_PASSWORD=shihao
    ports:
      - 3306:3306
  web:
    image: xli68/dog_of_the_dow:latest
    depends_on:
      - db
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=db
      - DATABASE_USER=shihao
      - DATABASE_PASSWORD=shihao
      - DATABASE_NAME=dog_of_the_dow_app
      - DATABASE_PORT=3306
英文:

I try to use docker-compose to containerize with starting MySQL server container first, and then start a Springboot Container to connect with the MySQL container.

But the Connection is fail(See full version in the terminal log below
web_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

How should I make changes to start my MySQL container first, then link it the Springboot Server to MySQL?

My terminal log

$ docker-compose up
Creating network &quot;backend_default&quot; with the default driver
Creating backend_db_1 ... done
Creating backend_web_1 ... done
Attaching to backend_db_1, backend_web_1
db_1   | 2020-03-15 05:59:09+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
db_1   | 2020-03-15 05:59:09+00:00 [Note] [Entrypoint]: Switching to dedicated user &#39;mysql&#39;
db_1   | 2020-03-15 05:59:09+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
web_1  | Trying to connect to MySQL at db:3306...
db_1   | 2020-03-15 05:59:09+00:00 [Note] [Entrypoint]: Initializing database files
web_1  | /wrapper.sh: connect: Connection refused
web_1  | /wrapper.sh: line 2: /dev/tcp/db/3306: Connection refused
db_1   | 2020-03-15T05:59:09.686844Z 0 [Warning] [MY-011070] [Server] &#39;Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it&#39; is deprecated and will be removed in a future release.
db_1   | 2020-03-15T05:59:09.686961Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 43
db_1   | 2020-03-15T05:59:12.938749Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db_1   | 2020-03-15 05:59:16+00:00 [Note] [Entrypoint]: Database files initialized
db_1   | 2020-03-15 05:59:16+00:00 [Note] [Entrypoint]: Starting temporary server
db_1   | 2020-03-15T05:59:17.100779Z 0 [Warning] [MY-011070] [Server] &#39;Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it&#39; is deprecated and will be removed in a future release.
db_1   | 2020-03-15T05:59:17.100921Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 93
db_1   | 2020-03-15T05:59:17.688885Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1   | 2020-03-15T05:59:17.692200Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location &#39;/var/run/mysqld&#39; in the path is accessible to all OS users. Consider choosing a different directory.
db_1   | 2020-03-15T05:59:17.730154Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: &#39;8.0.19&#39;  socket: &#39;/var/run/mysqld/mysqld.sock&#39;  port: 0  MySQL Community Server - GPL.
db_1   | 2020-03-15 05:59:17+00:00 [Note] [Entrypoint]: Temporary server started.
db_1   | 2020-03-15T05:59:17.802956Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: &#39;/var/run/mysqld/mysqlx.sock&#39;
db_1   | Warning: Unable to load &#39;/usr/share/zoneinfo/iso3166.tab&#39; as time zone. Skipping it.
db_1   | Warning: Unable to load &#39;/usr/share/zoneinfo/leap-seconds.list&#39; as time zone. Skipping it.
web_1  | /wrapper.sh: connect: Connection refused
web_1  | /wrapper.sh: line 2: /dev/tcp/db/3306: Connection refused
web_1  | Trying to connect to MySQL at db:3306...
db_1   | Warning: Unable to load &#39;/usr/share/zoneinfo/zone.tab&#39; as time zone. Skipping it.
db_1   | Warning: Unable to load &#39;/usr/share/zoneinfo/zone1970.tab&#39; as time zone. Skipping it.
db_1   | 2020-03-15 05:59:24+00:00 [Note] [Entrypoint]: Creating database dog_of_the_dow_app
db_1   | 2020-03-15 05:59:24+00:00 [Note] [Entrypoint]: Creating user shihao
db_1   | 2020-03-15 05:59:24+00:00 [Note] [Entrypoint]: Giving user shihao access to schema dog_of_the_dow_app
db_1   | 
db_1   | 2020-03-15 05:59:24+00:00 [Note] [Entrypoint]: Stopping temporary server
db_1   | 2020-03-15T05:59:24.460353Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.19).
db_1   | 2020-03-15T05:59:25.812000Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19)  MySQL Community Server - GPL.
db_1   | 2020-03-15 05:59:26+00:00 [Note] [Entrypoint]: Temporary server stopped
db_1   | 
db_1   | 2020-03-15 05:59:26+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
db_1   | 
db_1   | 2020-03-15T05:59:26.755391Z 0 [Warning] [MY-011070] [Server] &#39;Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it&#39; is deprecated and will be removed in a future release.
db_1   | 2020-03-15T05:59:26.755538Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1
db_1   | 2020-03-15T05:59:27.241304Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1   | 2020-03-15T05:59:27.245423Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location &#39;/var/run/mysqld&#39; in the path is accessible to all OS users. Consider choosing a different directory.
db_1   | 2020-03-15T05:59:27.270535Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: &#39;8.0.19&#39;  socket: &#39;/var/run/mysqld/mysqld.sock&#39;  port: 3306  MySQL Community Server - GPL.
db_1   | 2020-03-15T05:59:27.417150Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: &#39;/var/run/mysqld/mysqlx.sock&#39; bind-address: &#39;::&#39; port: 33060
web_1  | &gt;&gt; connected to MySQL database! &lt;&lt;
web_1  | 
web_1  |   .   ____          _            __ _ _
web_1  |  /\\ / ___&#39;_ __ _ _(_)_ __  __ _ \ \ \ \
web_1  | ( ( )\___ | &#39;_ | &#39;_| | &#39;_ \/ _` | \ \ \ \
web_1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
web_1  |   &#39;  |____| .__|_| |_|_| |_\__, | / / / /
web_1  |  =========|_|==============|___/=/_/_/_/
web_1  |  :: Spring Boot ::        (v2.2.4.RELEASE)
web_1  | 
web_1  | 2020-03-15 05:59:32.794  INFO 8 --- [           main] c.s.a.w.d.DogOfTheDowAppApplication      : Starting DogOfTheDowAppApplication v0.0.1-SNAPSHOT on 754a319ebef1 with PID 8 (/app.jar started by root in /)
web_1  | 2020-03-15 05:59:32.811  INFO 8 --- [           main] c.s.a.w.d.DogOfTheDowAppApplication      : The following profiles are active: containers
web_1  | 2020-03-15 05:59:34.732  INFO 8 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
web_1  | 2020-03-15 05:59:34.886  INFO 8 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 131ms. Found 2 JPA repository interfaces.
web_1  | 2020-03-15 05:59:35.721  INFO 8 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean &#39;org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration&#39; of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
web_1  | 2020-03-15 05:59:36.338  INFO 8 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
web_1  | 2020-03-15 05:59:36.375  INFO 8 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
web_1  | 2020-03-15 05:59:36.376  INFO 8 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
web_1  | 2020-03-15 05:59:36.528  INFO 8 --- [           main] o.a.c.c.C.[.[.[/mobile-app-ws]           : Initializing Spring embedded WebApplicationContext
web_1  | 2020-03-15 05:59:36.529  INFO 8 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3531 ms
web_1  | 2020-03-15 05:59:37.363  INFO 8 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
web_1  | 2020-03-15 05:59:37.588  INFO 8 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.10.Final}
web_1  | 2020-03-15 05:59:38.015  INFO 8 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
web_1  | 2020-03-15 05:59:38.325  INFO 8 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
web_1  | 2020-03-15 05:59:39.613 ERROR 8 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
web_1  | 
web_1  | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
web_1  | 
web_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1  | 	at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
web_1  | 	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
...
....
web_1  | 
backend_web_1 exited with code 1


My Dockerfile
Download required images
and run the wrapper.sh
wrapper.sh will keep checking if the mysql container is successfully running
if so, than start the springboot, otherwise not

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ARG SH_FILE=src/main/resources/*.sh
COPY ${SH_FILE} wrapper.sh
RUN /bin/sh -c &quot;apk add --no-cache bash&quot;
RUN bash -c &#39;touch /app.jar&#39;
RUN bash -c &#39;chmod +x /wrapper.sh&#39;

ENTRYPOINT [&quot;/bin/bash&quot;, &quot;/wrapper.sh&quot;]

My wrapper.sh file
keep checking mysql server

#!/usr/bin/env bash
while ! exec 6&lt;&gt;/dev/tcp/${DATABASE_HOST}/${DATABASE_PORT}; do
    echo &quot;Trying to connect to MySQL at ${DATABASE_HOST}:${DATABASE_PORT}...&quot;
    sleep 10
done
echo &quot;&gt;&gt; connected to MySQL database! &lt;&lt;&quot;
java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=containers -jar /app.jar

My docker-compose file

version: &quot;2&quot;
services:
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
      - MYSQL_DATABASE=example
      - MYSQL_USER=shihao
      - MYSQL_PASSWORD=shihao
    ports:
      - 3306:3306
  web:
    image: xli68/dog_of_the_dow:latest
    depends_on:
      - db
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=db
      - DATABASE_USER=shihao
      - DATABASE_PASSWORD=shihao
      - DATABASE_NAME=dog_of_the_dow_app
      - DATABASE_PORT=3306

huangapple
  • 本文由 发表于 2020年3月15日 14:16:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/60690267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定