[英]How to load static files with spring boot inside a docker container hosted on AWS?
I got my SpringBoot app running in a docker container built from a Dockerfile and hosted on AWS ubuntu instance.我的 SpringBoot 应用程序在 docker 容器中运行,该容器由 Dockerfile 构建并托管在 AWS ubuntu 实例上。 Everything is working perfectly, except I have an image, a css file and a js file that does not load.
一切正常,除了我有一个图像、一个 css 文件和一个无法加载的 js 文件。 Upon inspecting the page, these files show 404 not found error.
检查页面后,这些文件显示 404 not found 错误。 I have used winscp to upload my files to my aws instance.
我已经使用 winscp 将我的文件上传到我的 aws 实例。 In myApp folder is where my docker file is and where I build my container.
在 myApp 文件夹中是我的 docker 文件所在的位置以及我构建容器的位置。
Directory Structure is:目录结构为:
myApp
-Dockerfile
-target
-myApp.jar
-src
-main
-java
-[all my code in respective subdirectories]
-resources
-static
-my.js
-my.css
-my.jpg
-templates
-folder1
-html
-html2
-folder2
-html
-html2
I am almost certain my problem lies in the docker container and my dockerfile.我几乎可以肯定我的问题在于 docker 容器和我的 dockerfile。
Spring Boot automatically looks for static files in /src/main/resources/static. Spring 引导自动在 /src/main/resources/static 中查找 static 文件。 I'm thinking my docker container does not have this file structure.
我在想我的 docker 容器没有这个文件结构。
Here is my Dockerfile这是我的 Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y openjdk-14-jdk
WORKDIR /usr/local/bin/myApp
ADD . /src/main/resources/static
ADD target/myapp.jar .
ENTRYPOINT ["java", "-jar", "myApp.jar"]
When i build the container it shows everything copied and built successfully, but the files are not being reached.当我构建容器时,它显示成功复制和构建的所有内容,但未访问文件。 And what is weird to me is spring boot is serving the correct templates from the static folder.
对我来说奇怪的是 spring 引导正在提供 static 文件夹中的正确模板。 I am at a complete loss on this one.
我对此完全不知所措。 I have tried adding the resources individually from the dockerfile and still no luck.
我尝试从 dockerfile 单独添加资源,但仍然没有运气。
You are setting WORKDIR
and then copying .jar
into that location using relative path but when you are copying the other stuff ( /src/main/resources/static
), you are using absolute path which is completely destroying your folder structure (since those files are not copied into folder referenced by your WORKDIR
).您正在设置
WORKDIR
,然后使用相对路径将.jar
复制到该位置,但是当您复制其他内容( /src/main/resources/static
)时,您使用的是完全破坏文件夹结构的绝对路径(因为这些文件不会复制到您的WORKDIR
引用的文件夹中)。 You have probably forgot .
你可能已经忘记了
.
(dot) in front of that path - ./src/main/resources/static
. (点)在该路径前面 -
./src/main/resources/static
。
Run docker exec -it <image-id> bash
to get access into your running container and see what was copied where if you are not sure, fixing the stuff in your Dockerfile should be easy from then on.运行
docker exec -it <image-id> bash
以访问您正在运行的容器并查看复制的内容,如果您不确定,修复 Z3254677A7917C6C01F55212F86C7 中的内容应该很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.