简体   繁体   中英

Upload File by API when using Docker

I am writing a spring boot application that has a PostgreSQL database. I have an API in my controller that gets a file as

@RequestParam("file") MultipartFile file

and saves the file in a folder in the project's root folder. but when I'm using docker after calling the API no file is being saved in any folder or maybe I don't know where it is saved in the container. I am using docker-compose for running the project and here are my application.properties and docker-compose.yml files:

application.properties:

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.platform=postgres

spring.datasource.url= jdbc:postgresql://localhost:5432/myDB?useSSL=true
spring.datasource.username= myusername
spring.datasource.password= mypassword
    
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto= update


## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# All files uploaded through the REST API will be stored in this directory
file.upload-dir=E:uploadedFilesOfMyProject

docker-compose:

version: '3.1'
services:
  API:
    image: 'myJar.jar'
    ports:
      - "8080:8080"
    depends_on:
      PostgreSQL:
        condition: service_healthy
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://PostgreSQL:5432/myDB
      - SPRING_DATASOURCE_USERNAME=myusername
      - SPRING_DATASOURCE_PASSWORD=mypassword
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update

  PostgreSQL:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=mypassword
      - POSTGRES_USER=myusername
      - POSTGRES_DB=myDB
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U myusername"]
      interval: 10s
      timeout: 5s
      retries: 5

I have set the directory as follows in the application.properties and when I use postman to upload files, nothing is saved in this folder when the project is running by docker-compose up

file.upload-dir=E:uploadedFilesOfMyProject

where am I doing wrong? what did I misunderstand?

If anyone could help me I would be very appreciative.

this is the final docker-compose.yml file I used: I hope it will help others too:

version: "3.8"
services:
  pg_db:
    image: postgres
    restart: always
    volumes:
      - "./postgres/data:/var/lib/postgresql/data"
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: 
      POSTGRES_PASSWORD: 
      POSTGRES_DB: 
    networks:
      - product-net

  api_service:
    build: .
    restart: always
    ports:
      - 8080:8080
    depends_on:
      - pg_db
    networks:
      - product-net

networks:
  product-net:
    driver: bridge

volumes:
  pg_db:
    driver: local

I think your problem is that you have to define the environment "uploadedFilesOfMyProject" in your composer file for your application.

Regards, Josef

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