How to Keep Uploaded Files When Redeploying a Dockerized Spring Boot App?

0
3
Asked By TechyTurtle42 On

Hey everyone! I'm new to DevOps and Docker, and I'm having a bit of trouble. I'm working on containerizing an open-source Spring Boot project (Vireo) using Maven. The build runs smoothly, but there's a major catch: any files uploaded by users get stored in the JAR directory. This means that whenever I rebuild the image or create a new container, all the uploaded files disappear.

The relevant section in my `application.yml` looks like this:

```
app:
url: http://localhost:${server.port}

# comment says: 'override assets.uri with -Dassets.uri=file:/var/vireo/'
assets.uri: ${assets.uri}

public.folder: public
document.folder: private
```

My current run command is:

```
docker run -d --name vireo -p 9000:9000 your-image:latest
```

What I suspect is happening is that since `assets.uri` isn't explicitly set, Spring defaults to a relative path inside the fat JAR (`/app.jar!/WEB-INF/classes/private/...`). When the container stops or the image is rebuilt, that path is lost, which explains the missing uploads.

I've tried a couple of things:
1. Changing `document.folder` to an absolute path (`/vireo/uploads`), but uploads still go into the JAR unless I add `file:/`.
2. Adding `VOLUME /var/vireo` to my Dockerfile, which creates the folder but Spring still writes to the JAR.

Here are my questions:
1. Is using `assets.uri=file:/var/vireo/` the right approach, or should I set it at build-time with `-Dassets.uri`?
2. Are there any particular issues with missing trailing slashes or the `file:` scheme that I should be aware of?
3. For anyone who's deployed Vireo or similar Spring Boot apps, did you find it better to handle uploads with a named Docker volume instead of a bind mount? What are the pros and cons?

Thanks a bunch for your help! 🙏

3 Answers

Answered By VolumeGuru On

Note that if you declare a `VOLUME` in your Dockerfile without specifying it during container creation, Docker might create unnamed volumes. This can lead to multiple instances of volumes being generated, which won't allow you to see or use previous files effectively. To avoid this, always define your volumes with a clear path when you run the container.

Answered By SpringFan88 On

For your `assets.uri`, it’s best to set it explicitly as `-Dassets.uri=file:/var/vireo/` from your command line. That should direct uploads outside the JAR file and into your specified directory on the host. Just make sure that directory exists and is mounted properly.

Answered By DockerDude99 On

It sounds like your main issue is that you're not using a mounted volume. You really need to bind a volume from the host to the container using something like `-v $HOME/vireo:/var/vireo`. This way, any uploads will go directly to the host and persist through container restarts or rebuilds. Don't forget that containers are temporary, so data ends up being lost unless you set up proper volume persistence.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.