简体   繁体   English

如何在 AWS 上托管的 docker 容器内使用 spring 引导加载 static 文件?

[英]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.

相关问题 如何使用Tomcat托管的Spring Boot应用程序从Docker Container通过JDBC访问Oracle AWS RDS - How to access Oracle AWS RDS through JDBC from Docker Container with Spring boot application hosted on Tomcat 从 docker 容器(Spring Boot 应用程序)托管的 Amazon AWS ECS 集群以编程方式发送电子邮件的最佳方式? - Best way to send e-mail programmatically from docker container (Spring Boot app) hosted Amazon AWS ECS Cluster? 如何在 AWS EC2 托管的 spring 启动应用程序和 AWS 密钥空间之间设置数据库连接? - How to setup db connection between AWS EC2 hosted spring boot app and AWS key space? 如何将AWS Docker映像用于Spring Boot项目 - How to use aws docker image into spring boot project 连接到 AWS ECS 上不同 docker 容器中的 spring-boot 应用程序的错误请求 - Bad Request Connecting to a spring-boot app in a different docker container on AWS ECS 如何在 docker 容器内运行 AWS CW 代理? - How to run AWS CW Agent inside docker container? 如何在AWS的Docker容器中查找正在运行的进程的状态? - How to find the status of a running process inside docker container in AWS? 如何从 docker 容器内的主机工作站获取 aws 凭据 - how to get aws credentials from host workstation, inside docker container Spring 在 Docker 容器中启动失败 - Spring Boot fails to start up in Docker container 在 Spring Boot AWS 容器中重写 url - Rewrite url in Spring Boot AWS container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM