skip to Main Content

I have a folder which already contains images. The user can add/update/delete the image with the object.
Problem is when I dockerize my application, the user who get my docker image it gets the folder with images (I have a folder where are stored image.png) too? If not how can I make sure it gets the folder too inside docker.

[This other problem it was solved] My problem is that inside Docker I don’t know how to show these images to my angular application.

Here is my dockerfile:

FROM openjdk:17
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
ADD target/Nation-0.0.1-SNAPSHOT.jar target/springboot-docker-county.jar
EXPOSE 8090
CMD ["java", "-jar", "target/springboot-docker-county.jar"]

Here my docker compose:

services:
  db:
    image: mongo:latest
    ports:
      - 27017:27017
    environment:
      SPRING_DATA_MONGODB_URI: mongodb://host.docker.internal:27017
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: pass
    volumes:
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:r
  web:
    build: ./WorldBE/target/classes/county-fe
    ports:
      - 80:80
  api:
    build: ./WorldBE
    ports:
      - 8090:8090
    volumes:
      - ./images/:/usr/app/application/images

volumes:
  county:

Here my application.yml to get the folder:

spring:
  web:
    resources:
      static-locations: /usr/app/application/images

Here how can I perform the add method (it works):

 public void saveFile(String region, Integer id, Binary image) throws Exception {
        Path url = Paths.get( pathToSave);
        if (image != null) {
            File fileToSave = new File(url +"/"+ this.regional(region, id) + ".png");
            if (fileToSave.exists()) {
                log.error("Image for {} and regional: {} already exist! file: {}", id, region, fileToSave);
                throw new Exception(String.format("Image for : %s and region: %s already exist!", id, region));
            }

            if (!fileToSave.exists()) {
                log.info("file doesn't exist: {}", fileToSave);
            }
            // Try-with-resource
            try (OutputStream out = new FileOutputStream(fileToSave)) {
                out.write(image.getData());
                out.flush();
                log.info("file saved: {}", fileToSave);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            log.error("No image set");
        }

The user get the mount (folder with image.png) already populate when it pulls the image or should I do something else to store this folder in docker?

2

Answers


  1. In spring-boot, you can expose the endpoint as a static resource with the following properties:

    spring.web.resources.static-locations=file:/usr/app/application/images
    spring.mvc.static-path-pattern=/images/**
    

    Please note the file prefix when defining static-locations

    Login or Signup to reply.
  2. Move the folder with the images in your resource/static folder, than in your docker file (after MKDIR) add:

    ADD src/main/resources/static/images application/images
    

    and delete from your docker-compose:

      volumes:
          - ./images/:/usr/app/application/images
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search