[英]Spring Boot static resources are in my built jar but not in jar deployed to Docker
I wonder if the title of this post shouldn't be "I'm ignorant."不知这篇文章的标题不应该是“我无知”。 This is obviously a common problem and I've read multiple threads on it but somewhere I'm missing knowledge or... something.
这显然是一个常见问题,我已经阅读了多个主题,但在某处我缺少知识或......某些东西。
I've confirmed that my problem is that the static resources aren't making it to Docker. Using CLI, I can unzip the jar there and see that no html, js, or css files are there.我已经确认我的问题是 static 资源没有到达 Docker。使用 CLI,我可以在那里解压缩 jar 并看到那里没有 html、js 或 css 文件。
However, I've confirmed that the jar I'm using (I think I'm using it, anyway. I don't know how it couldn't be.) does have them in it.但是,我已经确认我正在使用的 jar(我想我正在使用它,无论如何。我不知道它怎么可能。)确实有它们。 It's as if they're getting deliberately extracted and excluded somehow.
就好像他们被故意提取并以某种方式排除在外。
Here's my application.properties:这是我的 application.properties:
#FOR LOCAL
#spring.datasource.url=jdbc:postgresql://localhost:5432/library
#FOR DOCKER
spring.datasource.url=jdbc:postgresql://postgresqldb:5432/library
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
#The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
#Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
#This enables app to serve the static content. Not sure why I need it, but it makes it work locally.
server.contextPath=/
Here's my Dockerfile这是我的 Dockerfile
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/microservice1-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} bookws.jar
ENTRYPOINT ["java","-jar","/bookws.jar"]
And here's my docker-compose.yaml这是我的 docker-compose.yaml
version: '3.1'
services:
app:
container_name: bookws
image: bookws
build: ./
ports:
- "8080:8080"
depends_on:
- postgresqldb
postgresqldb:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=library
Here's my main java file:这是我的主要 java 文件:
//package and imports excluded for brevity
@SpringBootApplication
public class Microservice1Application {
public static void main(String[] args) {
SpringApplication.run(Microservice1Application.class, args);
}
}
My static resources are stored in src/main/resources/static我的 static 资源存放在 src/main/resources/static
static/index.html
static/js/book-crud.js
static/js/jquery-3.5.0.min.js
static/js/jquery-ui-1.12.1.min.js
static/css/main.css
static/img/ (some .pngs and some .jpgs)
EDIT: Adding my build.gradle file:编辑:添加我的 build.gradle 文件:
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.familam.learning'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
// annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Thanks in advance for any insights,.在此先感谢您的任何见解。 If I need to add any further info?
如果我需要添加任何进一步的信息? I'm happy to.
我很乐意。 What is the probably simple thing I'm missing?
我缺少的可能很简单的东西是什么?
My problem was not with my configuration, but with my ignorance of how docker-compose works.我的问题不在于我的配置,而在于我对 docker-compose 的工作原理一无所知。 The docker-compose command 'up' does not re-build the docker image.
docker-compose 命令“up”不会重新构建 docker 映像。 You need to run the 'docker-compose build' command first.
您需要先运行“docker-compose build”命令。 That was all I was lacking.
这就是我所缺乏的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.