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
Percy9mo ago
Project ID: b9906d9a-fef1-41f1-ab48-5a9a7c735512
alcamech
alcamech9mo 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
Brody9mo 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
alcamech9mo 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
Brody9mo 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
alcamech9mo 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
Brody9mo 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
alcamech9mo ago
I will set this up, thank you.
Brody
Brody9mo 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
alcamech9mo 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
Brody9mo ago
if you are using a Dockerfile then a Procfile is irrelevant, just change your CMD instruction in your Dockerfile accordingly
alcamech
alcamech9mo 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
Brody9mo ago
I'd honestly recommend letting Railway build your app with nixpacks instead of using a dockerfile
alcamech
alcamech9mo ago
Okay, can I have it ignore the dockerfile?
Brody
Brody9mo ago
you'd have to rename or remove the Dockerfile
alcamech
alcamech9mo ago
I typically use them for local dev. I will rename
Brody
Brody9mo ago
maybe to Dockerfile.dev or something So without the Dockerfile you'd go back to needing the Procfile
alcamech
alcamech9mo ago
Im seeing errors related to the python dependencies now.
Brody
Brody9mo ago
quick question, your nextjs is going to call the flask service over the private network right?
alcamech
alcamech9mo ago
Yes, that is what I would like I believe I need a higher python version
Brody
Brody9mo ago
and next is calling the flask service only from the backend of the next app, right?
alcamech
alcamech9mo ago
the nextjs app is frontend only. Its calling my api in the components. useEffect, fetch calls.
Brody
Brody9mo ago
then you can't call the private networking domain, you need to call the public domain for the flask app
alcamech
alcamech9mo ago
Noted rebuilding with 3.11
Brody
Brody9mo 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
alcamech9mo ago
Redeploying services now.
Brody
Brody9mo ago
what method did you use to specify 3.11
alcamech
alcamech9mo ago
NIXPACKS_PYTHON_VERSION variable
Brody
Brody9mo ago
that works
alcamech
alcamech9mo ago
no port
Brody
Brody9mo 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
alcamech9mo 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
Brody9mo ago
show me the variable?
alcamech
alcamech9mo 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
Brody9mo 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
alcamech9mo ago
I think its a frontend issue, let me run some other curl commands.
Brody
Brody9mo ago
please implement this
alcamech
alcamech9mo ago
Doing this now
Brody
Brody9mo ago
show me the value you come up with please
alcamech
alcamech9mo ago
API_URL=https://${{ HealixAI-Backend.RAILWAY_PUBLIC_DOMAIN }}
Brody
Brody9mo 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
alcamech9mo 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
Brody9mo ago
make sure you are using https in your requests
alcamech
alcamech9mo ago
Everything is working as expected now. Thanks for all your help! Excited to be building on railway
Brody
Brody9mo ago
awsome, welcome to railway!
Want results from more Discord servers?
Add your server
More Posts