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
Project ID:
ed447a02-0626-4d37-a35a-c8aa82678ceb
ed447a02-0626-4d37-a35a-c8aa82678ceb
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
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.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 projectI see. So it will automatically ignore the included volume and use the mounted volume?
Yes
Perfect, thanks. Trying that now 👍
You can see that template in action here
https://django-volumes-production.up.railway.app/
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.That shouldn’t be the issue. Are you able to share your repo?
Unfortunately not because its a private repo. I can send you any code via chat
Did you set the
urls.py
file with the media link?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?
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 offMy volume name is /webapp/media
I guess this is the issue
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 goodSame 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'
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 folderI 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.
The difference between your development settings and using volumes is that your development settings do not persist data between deploys
Yes, exactly 😄
Using volumes allows your data to persist
Mh
I don't know what the issue could be
Changed my settings to this again:
MEDIA_URL = "media/"
MEDIA_ROOT = BASE_DIR / "media"
I’m away from my computer so I can’t do any tests to see what’s going on
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
No worries. I’ll report any findings I have back here
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? 🙂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
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.
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
Your app is not inside the volume. The way I have the template setup attaches the volume inside the Django app
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
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 thatThe 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! 😅😅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'
If that's the case urgh 😄
Maybe Ray can help here?
hi
This is exactly how I have the template setup… I’ll definitely be reevaluating it, that’s for sure
Soo, who can help me here? 😄
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 itOk I'll try
But it's RAILWAY_VOLUME_MOUNT_PATH isn't it?
Yeah, it is
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
🤔
I am using it like this:
MEDIA_URL = "media/"
MEDIA_ROOT = RAILWAY_VOLUME_MOUNT_PATH
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 settingTried it, error again:
[Errno 2] No such file or directory: '/app/media/covers/7829_cover bild.jpg'
🤔 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
Ok, thanks
No problem
I don't get why it works in your template. Maybe you are saving to the ephemeral storage?
In testing, the volume usage increases upon uploading. But I’ll give it some more in depth testing tonight
Oh my God, solved it..!
Let me check some other things but I think it should work now
Oh nice! If you did, share the result so I can update the template if necessary
Yeah sure, will do 👍
I just sent a pull request to update it. This way it should work. Works for me for sure!
Nice
Thanks again for all your help!
No problem
@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 🙂
Yeah, been busy with some personal stuff. I’ll get to them today sometime