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

Spring Boot部署Docker后静态资源访问返回404问题

Troubleshooting Static Assets Not Accessible in Spring Boot Docker Container

Hey there! I’ve run into this exact issue before, so let’s break down why your static resources work with mvn spring-boot:run but not in your Docker container, and fix it step by step.

Common Causes & Fixes

1. Static Resources Aren’t Included in the Built JAR

When you run mvn spring-boot:run, Spring Boot reads static assets directly from your src/main/resources directory. But if your Maven build isn’t configured properly, those assets might not get packaged into the final JAR file.

How to check:

  • Run mvn clean package to build your JAR, then use a tool like unzip to extract it. Look for a BOOT-INF/classes/static/ (or public/, resources/) directory—this is where Spring Boot expects static assets to live. If it’s missing, your build is skipping those files.

Fix:
Ensure your pom.xml includes proper resource configuration. Add or verify this section:

<build>
    <!-- Make sure all resources are included in the JAR -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <!-- Use the Spring Boot Maven plugin to package correctly -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Re-run mvn clean package to rebuild the JAR with all static assets included.

2. Dockerfile Path or Command Issues

Your current Dockerfile might have a subtle path problem that’s preventing the JAR from loading resources correctly.

Fix:
Update your Dockerfile to use absolute paths for the JAR, and ensure you’re copying the correct built file:

FROM openjdk:8-jdk-alpine
# Copy the built JAR to a specific path (avoids ambiguity)
COPY target/example-*.jar /app/example.jar
EXPOSE 8080
# Use absolute path to run the JAR
ENTRYPOINT ["java", "-jar", "/app/example.jar"]

Using COPY instead of ADD is more explicit here, and specifying an exact filename (example.jar) avoids wildcard matching issues in the entrypoint.

3. Incorrect Static Resource Path Configuration

If your static assets are stored in a non-default directory (not static/, public/, etc.), Spring Boot won’t automatically pick them up in the JAR environment.

Fix:
Add this line to your application.properties (or application.yml) to tell Spring Boot where to find your static resources:

# Replace with your actual static resource directory path
spring.web.resources.static-locations=classpath:/your-custom-static-folder/

Also double-check your frontend URLs: if your CSS is in static/css/style.css, the correct browser URL is http://localhost:8080/css/style.css (don’t include the static prefix in the URL).

Final Steps to Verify

  1. Rebuild your JAR with mvn clean package
  2. Rebuild your Docker image: docker build -t example-app .
  3. Run the container: docker run -p 8080:8080 example-app
  4. Check if static assets load now—if not, check the container logs with docker logs <container-id> for any hidden resource loading errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:16:44