在应用程序构建期间Dockerization失败

时间:2020-09-22 11:06:56

标签: java mysql docker dockerfile

我是堆栈溢出的新手。构建Spring Boot应用程序PetClinic时遇到几个问题。我已经创建了一个具有多阶段构建的Dockerfile,但是我也想连接到MySQL数据库。我已经阅读了很多有关如何对PetClinic App进行Docker化的文档,但对我来说情况似乎有所不同,并且相关的解决方案也不相关。我正在将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了解到的这一行 需要设置为env变量才能与MySQL数据库通信,但是,如果设置了此变量,则maven无法构建Docker映像,并抱怨:

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

以下是“图像构建”参数(我先构建图像,然后运行撰写)

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

0 个答案:

没有答案