I'm looking for a way to store data in a specific folder in the docker container

cross-posted from: lemm.ee/post/12034711

Hello Lemmy,

I have to grade student work. Students who have made their first springboot applications. I’m wary for idiots who don’t understand anything about java, yet somehow manage to include code that wipes my entire User folder, so I run the projects in docker these days.

One criterium is that they include a upload/download functionality. Some students decide to do that by storing the files in the DB, other store it in an “uploads” folder.

The first one works fine in docker. The second one not so much. The java code refers to a path “sourceroot/uploads”, but that doesn’t exist in the docker container. How can I make it exist in the docker container?

This is the DOCKERFILE I use to docker build -t image .


<span style="color:#323232;">FROM eclipse-temurin:17-jdk-alpine
</span><span style="color:#323232;">VOLUME /tmp
</span><span style="color:#323232;">EXPOSE 8080
</span><span style="color:#323232;">EXPOSE 5432
</span><span style="color:#323232;">COPY target/*.jar application.jar
</span><span style="color:#323232;">ENTRYPOINT ["java", "-jar", "application.jar"]
</span>

This is the yaml I use to run docker compose up -d


<span style="color:#323232;">version: '2'
</span><span style="color:#323232;">
</span><span style="color:#323232;">services:
</span><span style="color:#323232;">  app:
</span><span style="color:#323232;">    image: 'image'
</span><span style="color:#323232;">    ports: 
</span><span style="color:#323232;">        - "8080:8080"
</span><span style="color:#323232;">    container_name: app
</span><span style="color:#323232;">    depends_on:
</span><span style="color:#323232;">      db:
</span><span style="color:#323232;">        condition: service_healthy
</span><span style="color:#323232;">    environment:
</span><span style="color:#323232;">      - SERVER.PORT=8080
</span><span style="color:#323232;">      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/testing
</span><span style="color:#323232;">      - SPRING_DATASOURCE_USERNAME=postgres
</span><span style="color:#323232;">      - SPRING_DATASOURCE_PASSWORD=postgres
</span><span style="color:#323232;">      - SPRING_JPA_HIBERNATE_DDL_AUTO=create-drop
</span><span style="color:#323232;">          
</span><span style="color:#323232;">  db:
</span><span style="color:#323232;">    image: 'postgres:13.1-alpine'
</span><span style="color:#323232;">    ports:
</span><span style="color:#323232;">      - "5432:5432"
</span><span style="color:#323232;">    expose: 
</span><span style="color:#323232;">      - "5432"
</span><span style="color:#323232;">    container_name: db
</span><span style="color:#323232;">    environment:
</span><span style="color:#323232;">      - POSTGRES_USER=postgres
</span><span style="color:#323232;">      - POSTGRES_HOST_AUTH_METHOD=trust
</span><span style="color:#323232;">      - POSTGRES_DB=testing
</span><span style="color:#323232;">    healthcheck:
</span><span style="color:#323232;">      test: ["CMD-SHELL", "pg_isready -U postgres"]
</span><span style="color:#323232;">      interval: 10s
</span><span style="color:#323232;">      timeout: 5s
</span><span style="color:#323232;">      retries: 5
</span><span style="color:#323232;">  
</span><span style="color:#323232;">
</span><span style="color:#323232;">networks:
</span><span style="color:#323232;">  default:
</span><span style="color:#323232;">    name: docker
</span><span style="color:#323232;">
</span>

I’m sorry if this code offends anybody. I’ve made this Frankenstein’s monster myself from bits and pieces. However, I can’t seem to figure out how to add a working “uploads folder” to it that could be used by the springboot application inside the container. Any help would be appreciated.

Bldck,

Try adding app.volumes to the yaml


<span style="color:#323232;">
</span><span style="color:#323232;">volumes:
</span><span style="color:#323232;">     - /tmp/uploads:/uploads
</span><span style="color:#323232;">
</span>
abbadon420,

Thank you so much, this works perfectly! I’d been trying things with volumes, but never thought to add it to the “app” service XD

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • test
  • CafeMeta
  • [email protected]
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • TheResearchGuardian
  • Ask_kbincafe
  • KbinCafe
  • feritale
  • Socialism
  • oklahoma
  • SuperSentai
  • KamenRider
  • All magazines