Deploying a mono repo, python backend, nextjs frontend, SQL database
I'm currently trying to deploy my repo with the following setup.
Python flask API - /backend directory
NextJS Frontend -/frontend directory
I have a docker-compose file that creates these two and a SQL database. Whats the best way to deploy this on railway?
Solution:Jump to solution
can you send the compose file so I can get a better idea of what's going on?
but at a high level, your railway project will require 3 services, a service for the frontend, a service for the backend, and a service for the database...
45 Replies
Project ID:
b9906d9a-fef1-41f1-ab48-5a9a7c735512
b9906d9a-fef1-41f1-ab48-5a9a7c735512
The docker-compose file is in the root, the /backend directory has the script to initial the database.
My gitrepo structure
Solution
can you send the compose file so I can get a better idea of what's going on?
but at a high level, your railway project will require 3 services, a service for the frontend, a service for the backend, and a service for the database
Thanks for the quick response @Brody I have 3 services running, but my frontend is having problems talking to the backend
b9906d9a-fef1-41f1-ab48-5a9a7c735512
locally my frontend would reference the python api in code 127.0.0.1:5000/endpoitn
I have a environment variable setup for the internal networking now backend.railway.internal
do I need to setup the port as well for the flask api?
yes this is a private network, it works the same as when you are developing locally, so you do indeed need to specify a port in the request url
one thing to note, the private network is ipv6 only, so you may need to bind to
[::]
with gunicorn
I think by default gunicorn only binds to 0.0.0.0
but that's an ipv4 addressso my app run needs to look something like
if name == 'main':
app.run(host='::' debug=True)
Sorry, I'm reading up on this topic now
im using flask
you want to use gunicorn, otherwise you will be starting a development server, and it even warns to never use that in a production environment
I will set this up, thank you.
here's an example repo
https://github.com/alphasecio/flask/tree/main
note the Procfile, and you'd also need to add
Gunicorn=<latest version number>
to your requirements.txt
and as for the start command with the --bind flag, you'd want to use this format instead
web: gunicorn main:app -b [::]:$PORT
and make sure you have set a PORT service variable to 5000 in the flask's railway serviceThanks, I believe im using a dockerfile for the backend so I will make changes accordingly there.
I can add the procfile to the container?
or does it need to be outside of the container
if you are using a Dockerfile then a Procfile is irrelevant, just change your CMD instruction in your Dockerfile accordingly
I believe this is correct
I'd honestly recommend letting Railway build your app with nixpacks instead of using a dockerfile
Okay, can I have it ignore the dockerfile?
you'd have to rename or remove the Dockerfile
I typically use them for local dev. I will rename
maybe to Dockerfile.dev or something
So without the Dockerfile you'd go back to needing the Procfile
Im seeing errors related to the python dependencies now.
quick question, your nextjs is going to call the flask service over the private network right?
Yes, that is what I would like
I believe I need a higher python version
and next is calling the flask service only from the backend of the next app, right?
the nextjs app is frontend only. Its calling my api in the components. useEffect, fetch calls.
then you can't call the private networking domain, you need to call the public domain for the flask app
Noted rebuilding with 3.11
the frontend is public and accessed by the public, so it in turn also needs to be able to call the public domain
Redeploying services now.
what method did you use to specify 3.11
NIXPACKS_PYTHON_VERSION variable
that works
no port
right because you can only expose your app publicly on port 443, but you don't need to specify that because it's implied with https
Mmmm I have the variable set in my frontend for the backend API but im not seeing anything in the api logs.
I try to curl and nothing shows up
show me the variable?
HEALIX_BACKEND_API=https://healixai-backend-production.up.railway.app
My frontend uses process.env.HEALIX_BACKEND_API
backend logs
can you please replace the variable value with a reference value?
https://docs.railway.app/develop/variables#reference-variables
looks good to me, does something not work?
I think its a frontend issue, let me run some other curl commands.
please implement this
Doing this now
show me the value you come up with please
API_URL=https://${{ HealixAI-Backend.RAILWAY_PUBLIC_DOMAIN }}
you didn't need to use the same variable name, you could stick with the name you already had, but the value side of things looks good
I think the issue was my api reference needed to be appended with NEXTPUBLIC redeploying now
MMmm now getting a 405 method not allowed on the post routes on the frontend. I can curl everything though.
I think the scope of this is resolved.
make sure you are using https in your requests
Everything is working as expected now. Thanks for all your help! Excited to be building on railway
awsome, welcome to railway!