You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Eureka Server Docker构建报错:找不到target目录

Docker构建Eureka Server时找不到/target目录的问题分析与修复

问题原因

你的问题出在Docker构建时的项目结构复制逻辑和Maven构建上下文不匹配:

  • 本地在eureka-server目录执行mvn package时,使用的是子模块自身的pom.xml,能正确生成target目录和JAR包。
  • 但Docker构建时,你复制了根目录的pom.xml到/app,然后只复制了eureka-server/src到/app/src,却没有复制eureka-server的pom.xml和完整目录结构。Maven基于根pom执行构建时,找不到子模块的配置,无法完成构建,自然不会生成/app/target目录。

解决方案

方案一:直接基于子模块pom构建(适合单个子模块场景)

修改Dockerfile,让Maven使用eureka-server自身的pom来构建:

# Use a base image with Java and Maven installed
FROM maven:3.8.4-openjdk-11-slim AS build

# Set the working directory
WORKDIR /app

# 复制eureka-server的pom.xml(而非根pom)
COPY eureka-server/pom.xml .

# Download the project dependencies
RUN mvn dependency:go-offline 

# 复制eureka-server的源码到当前目录的src
COPY eureka-server/src ./src

# Build the project
RUN mvn package 

# Create a new image with a smaller base image
FROM openjdk:11-jre-slim

# Set the working directory
WORKDIR /app

# Copy the built JAR file from the previous stage
COPY --from=build /app/target/*.jar ./eureka-server.jar

# Expose the port that the application will run on
EXPOSE 8761

# Command to run the application
CMD ["java", "-jar", "eureka-server.jar"]

方案二:基于根pom构建(适合多子模块场景)

如果根pom是父项目,需要管理多个子模块,调整Dockerfile复制完整项目结构,并指定构建eureka-server子模块:

# Use a base image with Java and Maven installed
FROM maven:3.8.4-openjdk-11-slim AS build

# Set the working directory
WORKDIR /app

# 复制根pom.xml
COPY pom.xml .

# 复制eureka-server的pom.xml到对应子目录
COPY eureka-server/pom.xml ./eureka-server/

# 下载eureka-server及其依赖的离线包
RUN mvn dependency:go-offline -pl eureka-server -am

# 复制eureka-server的源码
COPY eureka-server/src ./eureka-server/src

# 构建eureka-server子模块
RUN mvn package -pl eureka-server -am

# Create a new image with a smaller base image
FROM openjdk:11-jre-slim

# Set the working directory
WORKDIR /app

# 注意这里的路径是子模块下的target目录
COPY --from=build /app/eureka-server/target/*.jar ./eureka-server.jar

# Expose the port that the application will run on
EXPOSE 8761

# Command to run the application
CMD ["java", "-jar", "eureka-server.jar"]

验证

修改后重新执行docker-compose build --no-cache,即可正常构建并找到JAR包。

内容的提问来源于stack exchange,提问作者Leon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:35:05