R
Railway16mo ago
ZEYD

How to import RAILWAY_VOLUME_MOUNT_PATH in ENV

Hi, I already read the documentation but whenever I try using the RAILWAY_VOLUME_MOUNT_PATH in my ENV like this it will not work: USER_DATA=RAILWAY_VOLUME_MOUNT_PATH USER_DATA will only contain the string. It's a Django project I am working on. How can I solve it? My settings file looks like this: MEDIA_URL = "media/" MEDIA_ROOT = os.environ.get("USER_DATA")
61 Replies
Percy
Percy16mo ago
Project ID: ed447a02-0626-4d37-a35a-c8aa82678ceb
ZEYD
ZEYD16mo ago
ed447a02-0626-4d37-a35a-c8aa82678ceb
MantisInABox
MantisInABox16mo ago
The volume path is injected into your deployment by default. But, if you are using Django and want to use volumes with it, you can look at the repo attached to this template https://railway.app/template/AWUIv6
ZEYD
ZEYD16mo ago
Thanks for the response. In the repo it looks like this: MEDIA_URL = "media/" MEDIA_ROOT = BASE_DIR / "media" How is the volume path injected here? Is BASE_DIR automatically set to the volume? It looks like it works in the repo but I'd like to copy the working code over to my project instead of using the template. Nevermind, I just realized that the mount paths name itself is like Django needs it. Will try that out now.
MantisInABox
MantisInABox16mo ago
You don’t need to use the injected variable. Your app lives inside /app in the container. If you mount your volume inside of /app you can use the BASE_DIR variable set in your settings.py file because it’s using the path to your project
ZEYD
ZEYD16mo ago
I see. So it will automatically ignore the included volume and use the mounted volume?
MantisInABox
MantisInABox16mo ago
Yes
ZEYD
ZEYD16mo ago
Perfect, thanks. Trying that now 👍
MantisInABox
MantisInABox16mo ago
You can see that template in action here https://django-volumes-production.up.railway.app/
ZEYD
ZEYD16mo ago
I get this error: [Errno 2] No such file or directory: '/app/media/covers/6935_cover bild 2.jpg' Could it be because of my projects name, which is "webapp"? I changed the volume name to /webapp/media already while adding.
MantisInABox
MantisInABox16mo ago
That shouldn’t be the issue. Are you able to share your repo?
ZEYD
ZEYD16mo ago
Unfortunately not because its a private repo. I can send you any code via chat
MantisInABox
MantisInABox16mo ago
Did you set the urls.py file with the media link?
ZEYD
ZEYD16mo ago
Yes from django.contrib import admin from django.urls import include, path, re_path from django.conf import settings from django.views.static import serve from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path("", include("webapp.urls")), path("", include("django.contrib.auth.urls")), ] # + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += [ re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})] Ah, I didnt change Debug to false could this be the issue?
MantisInABox
MantisInABox16mo ago
Okay, cool. You shouldn’t need to rename your volume path as anything but /app/media No, it should work with both debug on and off
ZEYD
ZEYD16mo ago
My volume name is /webapp/media I guess this is the issue
MantisInABox
MantisInABox16mo ago
That’s gonna be the problem. When the builder grabs your code it puts it in the /app folder in the container. As long as you mount to a non-existing folder in that directory, you should be good
ZEYD
ZEYD16mo ago
Same error. What do you mean with "as long as you mount to a non-existing folder"? Is media a pre-existing folder? In my repo I have: -projectName --webapp ---media ----folder1 ----folder2 So, my media folder is pre-existing in a sense. Could that be an issue? [Errno 2] No such file or directory: '/app/media/covers/206_cover bild.jpg'
MantisInABox
MantisInABox16mo ago
It shouldn’t be an issue due to the layout of the project. When deployed, your code would live at /app/projectName/webapp as long as projectName is a folder
ZEYD
ZEYD16mo ago
I see. Funny thing is with my development settings it works on Railway: MEDIA_URL = "media/" MEDIA_ROOT = os.path.join(BASE_DIR, 'webapp/media') Wait, I mean it worked, before mounting the volume. Gonna try with volume on now. Okay I see now. It works BUT if I redeply my data is gone.
MantisInABox
MantisInABox16mo ago
The difference between your development settings and using volumes is that your development settings do not persist data between deploys
ZEYD
ZEYD16mo ago
Yes, exactly 😄
MantisInABox
MantisInABox16mo ago
Using volumes allows your data to persist
ZEYD
ZEYD16mo ago
Mh I don't know what the issue could be Changed my settings to this again: MEDIA_URL = "media/" MEDIA_ROOT = BASE_DIR / "media"
MantisInABox
MantisInABox16mo ago
I’m away from my computer so I can’t do any tests to see what’s going on
ZEYD
ZEYD16mo ago
No worries. I'll just try out a few things and would love if you could back to me if you can test some things. I will not be in office till tomorrow but I'll get back to you asap then
MantisInABox
MantisInABox16mo ago
No worries. I’ll report any findings I have back here
ZEYD
ZEYD16mo ago
Thanks! FYI i changed this MEDIA_ROOT = BASE_DIR / "media" to this again for other testing purposes in my code (unrelated to media): MEDIA_ROOT = os.path.join(BASE_DIR, 'webapp/media') Hi @Vin, any findings? 🙂
MantisInABox
MantisInABox16mo ago
All the testing I did, seems to work. And it’s all based off the template that I had sent you. Without seeing your code, it’s kinda hard to tell what’s going on, unfortunately
Ray
Ray16mo ago
Dumb question but are you saving the media files to the vol? Quick way to test would be to upload -> retrieve. For MEDIA_ROOT, you should set it to the env var provided (VOLUME_MOUNT_PATH), so your original approach is correct.
ZEYD
ZEYD16mo ago
What do you mean? I thought Django was deployed directly inside my volume? At least I understood it that way from what Vin had written
MantisInABox
MantisInABox16mo ago
Your app is not inside the volume. The way I have the template setup attaches the volume inside the Django app
Ray
Ray16mo ago
Ya so there are two different storages. Ephemeral (where your app/service lives) and persistent (that you attach to your app/service). They are different There’s no overlap between the two, so you gotta make sure you’re writing to the correct volume path
ZEYD
ZEYD16mo ago
How do I do that? I thought something like this above would do it: MEDIA_URL = "media/" MEDIA_ROOT = BASE_DIR / "media" But if you say that's completely sepearted from my Django project I guess I have to link to the volume somehow? But the template that Vin sent me doenst do any linking at all as well. Yeah I know that but I thought when linking them together in my Railway Dashboard that would run together e.g. my Django project inside the persistent volume. But thats not the case I guess I see. Where do you see that inside the template? Maybe I can copy that
MantisInABox
MantisInABox16mo ago
The way I have the template setup is the volume mount point is set to /app/media the Django code uses the media folder set to that path. And when testing with uploading images, the storage used on the volume increases Maybe I’m doing something wrong then! I’m gonna reevaluate the template after work! 😅😅
ZEYD
ZEYD16mo ago
I have this path already set up for my volume. My settings look like this: MEDIA_URL = "media/" MEDIA_ROOT = BASE_DIR / "media" When I run my server and try uploading anything I get the following error: [Errno 2] No such file or directory: '/app/media/covers/1222_cover bild.jpg'
ZEYD
ZEYD16mo ago
If that's the case urgh 😄 Maybe Ray can help here?
alex
alex16mo ago
hi humanRaylaShake
MantisInABox
MantisInABox16mo ago
This is exactly how I have the template setup… I’ll definitely be reevaluating it, that’s for sure
ZEYD
ZEYD16mo ago
Soo, who can help me here? 😄
MantisInABox
MantisInABox16mo ago
I would try rays method of using the built-in VOLUME_MOUNT_PATH environment variable for the location of your media folder, and see if that fixes it
ZEYD
ZEYD16mo ago
Ok I'll try But it's RAILWAY_VOLUME_MOUNT_PATH isn't it?
MantisInABox
MantisInABox16mo ago
Yeah, it is
ZEYD
ZEYD16mo ago
How can I access it? When I try using the variable inside my settings file like here (https://docs.railway.app/reference/volumes) it gets me an error: NameError: name 'RAILWAY_VOLUME_MOUNT_PATH' is not defined
MantisInABox
MantisInABox16mo ago
🤔
ZEYD
ZEYD16mo ago
I am using it like this: MEDIA_URL = "media/" MEDIA_ROOT = RAILWAY_VOLUME_MOUNT_PATH
MantisInABox
MantisInABox16mo ago
Try changing it to an environment variable declaration os.environ[“RAILWAY_VOLUME_MOUNT_PATH”] because the way you are doing it is trying to read a variable you would be setting
ZEYD
ZEYD16mo ago
Tried it, error again: [Errno 2] No such file or directory: '/app/media/covers/7829_cover bild.jpg'
MantisInABox
MantisInABox16mo ago
🤔 okay. I’ll play around a bit more tonight using the template and some fresh code, and see if I can figure out a solution for you I’m at work for another 11 hours
ZEYD
ZEYD16mo ago
Ok, thanks
MantisInABox
MantisInABox16mo ago
No problem
ZEYD
ZEYD16mo ago
I don't get why it works in your template. Maybe you are saving to the ephemeral storage?
MantisInABox
MantisInABox16mo ago
In testing, the volume usage increases upon uploading. But I’ll give it some more in depth testing tonight
ZEYD
ZEYD16mo ago
Oh my God, solved it..! Let me check some other things but I think it should work now
MantisInABox
MantisInABox16mo ago
Oh nice! If you did, share the result so I can update the template if necessary
ZEYD
ZEYD16mo ago
Yeah sure, will do 👍 I just sent a pull request to update it. This way it should work. Works for me for sure!
MantisInABox
MantisInABox16mo ago
Nice
ZEYD
ZEYD16mo ago
Thanks again for all your help!
MantisInABox
MantisInABox16mo ago
No problem
ZEYD
ZEYD15mo ago
@Vin Did you already had chance to check those? @Vin 2x pull requests are pending. Would love if you could check because I think this will help other people. One other person already commented under one of the pull requests that it worked for him as well 🙂
MantisInABox
MantisInABox15mo ago
Yeah, been busy with some personal stuff. I’ll get to them today sometime
Want results from more Discord servers?
Add your server