Spring Boot部署Docker后静态资源访问返回404问题
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 packageto build your JAR, then use a tool likeunzipto extract it. Look for aBOOT-INF/classes/static/(orpublic/,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
- Rebuild your JAR with
mvn clean package - Rebuild your Docker image:
docker build -t example-app . - Run the container:
docker run -p 8080:8080 example-app - 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

