Docker -Состав Java Springboot и MySQL - PullRequest
2 голосов
/ 15 марта 2020

Я пытаюсь использовать docker -compose, чтобы сначала создать контейнер с запуском MySQL контейнера сервера, а затем запустить контейнер Springboot для соединения с контейнером MySQL.

Но соединение не установлено (см. Полную версию в журнале терминала ниже web_1 | com. mysql .cj.jdb c .exceptions.CommunicationsException: сбой линии связи

Как следует Я делаю изменения, чтобы сначала запустить мой контейнер MySQL, а затем привязать его к серверу Springboot с MySQL?

Мой журнал терминала

$ docker-compose up
Creating network "backend_default" 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 'mysql'
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] '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.
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] '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.
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 '/var/run/mysqld' 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: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  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: '/var/run/mysqld/mysqlx.sock'
db_1   | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' 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 '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' 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] '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.
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 '/var/run/mysqld' 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: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  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: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
web_1  | >> connected to MySQL database! <<
web_1  | 
web_1  |   .   ____          _            __ _ _
web_1  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
web_1  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
web_1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
web_1  |   '  |____| .__|_| |_|_| |_\__, | / / / /
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 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' 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


Мой Dockerfile. Загрузите необходимые изображения и запустите упаковщик. . sh wrapper. sh будет продолжать проверять, успешно ли работает контейнер mysql, если это так, чем запускать Springboot, в противном случае не будет

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"]

My wrapper. sh file keep check mysql сервер

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

Мой docker -компонентный файл

version: "2"
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...