R
Railway15mo ago
alcamech

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:
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...
Jump to solution
45 Replies
Percy
Percy15mo ago
Project ID: b9906d9a-fef1-41f1-ab48-5a9a7c735512
alcamech
alcamechOP15mo ago
b9906d9a-fef1-41f1-ab48-5a9a7c735512 The docker-compose file is in the root, the /backend directory has the script to initial the database.
.
├── docker-compose.yml
├── backend/
├── frontend/
├── README.md
├── requirements.txt
└── runtime.txt
.
├── docker-compose.yml
├── backend/
├── frontend/
├── README.md
├── requirements.txt
└── runtime.txt
My gitrepo structure
Solution
Brody
Brody15mo ago
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
alcamech
alcamechOP15mo ago
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?
Brody
Brody15mo ago
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 address
alcamech
alcamechOP15mo ago
so 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
Brody
Brody15mo ago
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
alcamech
alcamechOP15mo ago
I will set this up, thank you.
Brody
Brody15mo ago
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 service
alcamech
alcamechOP15mo ago
Thanks, 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
Brody
Brody15mo ago
if you are using a Dockerfile then a Procfile is irrelevant, just change your CMD instruction in your Dockerfile accordingly
alcamech
alcamechOP15mo ago
FROM ubuntu:latest

WORKDIR /app

COPY . /app

RUN apt-get update && \
apt-get install -y python3-pip && \
pip3 install --upgrade pip && \
pip3 install --no-cache-dir -r requirements.txt && \
apt-get clean

ENV PORT 5000
EXPOSE $PORT

CMD gunicorn main:app -b [::]:$PORT
FROM ubuntu:latest

WORKDIR /app

COPY . /app

RUN apt-get update && \
apt-get install -y python3-pip && \
pip3 install --upgrade pip && \
pip3 install --no-cache-dir -r requirements.txt && \
apt-get clean

ENV PORT 5000
EXPOSE $PORT

CMD gunicorn main:app -b [::]:$PORT
I believe this is correct
Brody
Brody15mo ago
I'd honestly recommend letting Railway build your app with nixpacks instead of using a dockerfile
alcamech
alcamechOP15mo ago
Okay, can I have it ignore the dockerfile?
Brody
Brody15mo ago
you'd have to rename or remove the Dockerfile
alcamech
alcamechOP15mo ago
I typically use them for local dev. I will rename
Brody
Brody15mo ago
maybe to Dockerfile.dev or something So without the Dockerfile you'd go back to needing the Procfile
alcamech
alcamechOP15mo ago
Im seeing errors related to the python dependencies now.
Brody
Brody15mo ago
quick question, your nextjs is going to call the flask service over the private network right?
alcamech
alcamechOP15mo ago
Yes, that is what I would like I believe I need a higher python version
Brody
Brody15mo ago
and next is calling the flask service only from the backend of the next app, right?
alcamech
alcamechOP15mo ago
the nextjs app is frontend only. Its calling my api in the components. useEffect, fetch calls.
Brody
Brody15mo ago
then you can't call the private networking domain, you need to call the public domain for the flask app
alcamech
alcamechOP15mo ago
Noted rebuilding with 3.11
Brody
Brody15mo ago
the frontend is public and accessed by the public, so it in turn also needs to be able to call the public domain
alcamech
alcamechOP15mo ago
Redeploying services now.
Brody
Brody15mo ago
what method did you use to specify 3.11
alcamech
alcamechOP15mo ago
NIXPACKS_PYTHON_VERSION variable
Brody
Brody15mo ago
that works
alcamech
alcamechOP15mo ago
no port
Brody
Brody15mo ago
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
alcamech
alcamechOP15mo ago
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
Brody
Brody15mo ago
show me the variable?
alcamech
alcamechOP15mo ago
HEALIX_BACKEND_API=https://healixai-backend-production.up.railway.app My frontend uses process.env.HEALIX_BACKEND_API backend logs
2023-09-24 15:51:45 +0000] [1] [INFO] Starting gunicorn 21.2.0
[2023-09-24 15:51:45 +0000] [1] [INFO] Listening at: http://[::]:5000 (1)
[2023-09-24 15:51:45 +0000] [1] [INFO] Using worker: sync
[2023-09-24 15:51:45 +0000] [9] [INFO] Booting worker with pid: 9
2023-09-24 15:51:45 +0000] [1] [INFO] Starting gunicorn 21.2.0
[2023-09-24 15:51:45 +0000] [1] [INFO] Listening at: http://[::]:5000 (1)
[2023-09-24 15:51:45 +0000] [1] [INFO] Using worker: sync
[2023-09-24 15:51:45 +0000] [9] [INFO] Booting worker with pid: 9
Brody
Brody15mo ago
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?
alcamech
alcamechOP15mo ago
I think its a frontend issue, let me run some other curl commands.
Brody
Brody15mo ago
please implement this
alcamech
alcamechOP15mo ago
Doing this now
Brody
Brody15mo ago
show me the value you come up with please
alcamech
alcamechOP15mo ago
API_URL=https://${{ HealixAI-Backend.RAILWAY_PUBLIC_DOMAIN }}
Brody
Brody15mo ago
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
alcamech
alcamechOP15mo ago
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.
Brody
Brody15mo ago
make sure you are using https in your requests
alcamech
alcamechOP15mo ago
Everything is working as expected now. Thanks for all your help! Excited to be building on railway
Brody
Brody15mo ago
awsome, welcome to railway!
Want results from more Discord servers?
Add your server