简体   繁体   中英

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. Everything is working perfectly, except I have an image, a css file and a js file that does not load. Upon inspecting the page, these files show 404 not found error. I have used winscp to upload my files to my aws instance. In myApp folder is where my docker file is and where I build my container.

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.

Spring Boot automatically looks for static files in /src/main/resources/static. I'm thinking my docker container does not have this file structure.

Here is my 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. I am at a complete loss on this one. I have tried adding the resources individually from the dockerfile and still no luck.

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 ). You have probably forgot . (dot) in front of that path - ./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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM