首页 > 解决方案 > Dockerization 失败,在应用程序构建期间

问题描述

我是堆栈溢出的新手。我在构建 Spring Boot 应用程序 PetClinic 时遇到了几个问题。我创建了一个多阶段构建的 Dockerfile,但我还想连接到 MySQL 数据库。我已经阅读了很多关于如何 Dockerize PetClinic App 的文档,但我的情况对我来说似乎是不同的,并且相关的解决方案并不相关。我正在对接https://github.com/spring-projects/spring-petclinic

那么你能否建议我,如何:

  1. 使用 PetClinic 应用程序创建 Docker 映像

  2. 将创建的 PetClinic 应用程序的 docker 映像连接到 MySQL 数据库

  3. 如何在 MySQL 和 PetClinic 应用程序之间设置卷

这是我的 Dockerfile

# Multi Stage Build > Step 1 Clone > Base Image Alpine Git
FROM alpine/git as get
ARG  url
WORKDIR  /app
RUN  git clone ${url}

# Multi Stage Build > Step 2 Compile > Base Image Alpine Maven
FROM maven:3.5-jdk-8-alpine as compile
ARG  project
# ENV  SPRING_PROFILES_ACTIVE docker, mysql
WORKDIR  /app
COPY --from=get /app/${project} /app
RUN  mvn package

# Multi Stage Build > Step 3 Build > Base Image Alpine Openjdk
FROM openjdk:8-jre-alpine
ARG  artifactid
ARG  version
ENV  artifact ${artifactid}-${version}.jar
WORKDIR  /app
COPY --from=compile /app/target/${artifact} /app
EXPOSE   8080
ENTRYPOINT [ "sh", "-c" ]
CMD  [ "java -jar ${artifact}" ]

正如你所注意到的,我已经评论了# ENV SPRING_PROFILES_ACTIVE docker, mysql

所以我从https://gitlab.comquent.de/petclinic/spring-petclinic-microservices/tree/master了解到的这一行 需要设置为环境变量才能与 MySQL 数据库通信,但是如果设置了这个变量, maven 无法构建 Docker Image,并抱怨:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException:
Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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.
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException:
Failed to obtain JDBC Connection; nested exception is 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.
Caused by: 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.
Caused by: com.mysql.cj.exceptions.CJCommunicationsException:
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.
Caused by: java.net.ConnectException: Connection refused (Connection refused)

我不擅长Java,但问题是自己回答的:

如果设置ENV SPRING_PROFILES_ACTIVE docker, mysql它不是构建图像

如果未设置ENV SPRING_PROFILES_ACTIVE docker, mysql,则正在构建图像

这是 Docker-Compose 文件,用于构建 MySQL 数据库

---
 version: '3'
 services:
   mysql:
     image: mysql:5.7
     ports:
       - "3306:3306"
     environment:
       - MYSQL_ROOT_PASSWORD=petclinic
       - MYSQL_ALLOW_EMPTY_PASSWORD=true
       - MYSQL_USER=petclinic
       - MYSQL_PASSWORD=petclinic
       - MYSQL_DATABASE=petclinic
     volumes:
       - "./conf.d:/etc/mysql/conf.d:rw"
       - "./conf.d/data:/var/lib/mysql"
   spring:
     image: local-petclinic:latest
     ports:
       - "8080:8080"
     environment:
       - SPRING_PROFILES_ACTIVE=docker,mysql
     depends_on:
       - mysql

这是图像构建参数(我先构建图像,然后运行 ​​compose)

docker build \
   --build-arg url=https://github.com/spring-projects/spring-petclinic.git \
   --build-arg project=spring-petclinic     \
   --build-arg artifactid=spring-petclinic  \
   --build-arg version=2.3.0.BUILD-SNAPSHOT \
   -t local-petclinic:latest - < Dockerfile

标签: javamysqldockerdockerfile

解决方案


推荐阅读