R
Railway•2y ago
p1gp3n

Docker + Streamlit not working

I am trying to deploy a Streamlit app with Docker. The image runs fine locally. I let Railway detect the build and run command. Railway indicates the image is deployed successfully, get I cannot access via provided URL.
Project ID: ac828fce-6f54-43e9-825c-ac4871d226ba
43 Replies
Percy
Percy•2y ago
Project ID: ac828fce-6f54-43e9-825c-ac4871d226ba
p1gp3n
p1gp3nOP•2y ago
Dockerfile is here:
Brody
Brody•2y ago
503 status code?
p1gp3n
p1gp3nOP•2y ago
hi @Brody thanks for the response. I was actually able to resolve. the key was specifying the port in the dockerfile: CMD streamlit run --server.port $PORT frontend.py i saw this in the heroku knowledge base for streamlit, figured the same applied for Railway
Brody
Brody•2y ago
awsome, glad you got it solved
p1gp3n
p1gp3nOP•2y ago
hmm. still getting issues with this actually. Can I use the start command to specify the docker run command? I'm not sure how you are e.g. mapping ports or passing in env variables. But certain things aren't working as expected
Brody
Brody•2y ago
you can't specific a docker run command. and you can't only forward one internal port to the external 443 port over https so it's totally possible that you won't be able to run streamlit, I have no experience with it myself so I don't actually know if it would be possible
p1gp3n
p1gp3nOP•2y ago
I actually have the streamlit service running on Railway now. Service ID: 0eaf8754-ca2d-45cc-a225-737e2d7b3a49 the issue is, i need to define env variables so the staging/prod builds point to the correct staging/prod endpoints. but I'm not entirely sure how I do this with Railway.
i've read the docs (it's only one page on Docker). i've tried setting env variables in the Railway console, etc. is there maybe a sample dockerfile i can reference?
p1gp3n
p1gp3nOP•2y ago
ARG ARG_ENVIRONMENT="staging" ARG ARG_PASSWORD="fake" FROM python:3.9-slim WORKDIR /app COPY . /app RUN pip3 install -r requirements.txt EXPOSE 8501 ENV APP_ENVIRONMENT=$ARG_ENVIRONMENT ENV APP_PASSWORD=$ARG_PASSWORD ENV PORT=8501 CMD streamlit run frontend.py --server.port $PORT i see in teh doc it says to pass in the args before from, but that's it. and that's what i try to do here
Brody
Brody•2y ago
railway provides a RAILWAY_ENVIRONMENT variable https://docs.railway.app/develop/variables#railway-provided-variables
p1gp3n
p1gp3nOP•2y ago
ah. that could work. do i define that as an ARG before the FROM in the file? or do I put this in the Variables section of the service in the dashboard?
Brody
Brody•2y ago
its injected by railway, no need to define it yourself in the service, you would still need to reference it with ARG tho
p1gp3n
p1gp3nOP•2y ago
in the dockerfile?
Brody
Brody•2y ago
yes, where else would you use ARG lol
p1gp3n
p1gp3nOP•2y ago
because the app itself needs it. its a value i have in my .env file, and it uses this to pull the correct value for my backend api. ha. yeah i figured u meant the dockerfile. but im new to railway so i dont know if there's somewhere else its used
Brody
Brody•2y ago
in python you would just use os.environ or whatever method you want
p1gp3n
p1gp3nOP•2y ago
yeah it works in python no problem. ARG RAILWAY_ENVIRONMENT= ARG APP_PASSWORD= "fake" FROM python:3.9-slim WORKDIR /app COPY . /app RUN pip3 install -r requirements.txt EXPOSE 8501 ENV APP_ENVIRONMENT= $RAILWAY_ENVIRONMENT ENV APP_PASSWORD= $ARG_PASSWORD CMD streamlit run frontend.py --server.port $PORT so would it be somethign like this? where i keep my python .env name as APP_ENVIRONMENT. Railway injects the RAILWAY_ENVIORNMENT at build time, passes that to my Env that my app can use?
Brody
Brody•2y ago
please enclose your code (or dockerfiles) with triple back-ticks
p1gp3n
p1gp3nOP•2y ago
ARG RAILWAY_ENVIRONMENT=
ARG APP_PASSWORD= "fake"

FROM python:3.9-slim

WORKDIR /app

COPY . /app

RUN pip3 install -r requirements.txt

EXPOSE 8501

ENV APP_ENVIRONMENT= $RAILWAY_ENVIRONMENT
ENV APP_PASSWORD= $ARG_PASSWORD

CMD streamlit run frontend.py --server.port $PORT
ARG RAILWAY_ENVIRONMENT=
ARG APP_PASSWORD= "fake"

FROM python:3.9-slim

WORKDIR /app

COPY . /app

RUN pip3 install -r requirements.txt

EXPOSE 8501

ENV APP_ENVIRONMENT= $RAILWAY_ENVIRONMENT
ENV APP_PASSWORD= $ARG_PASSWORD

CMD streamlit run frontend.py --server.port $PORT
Brody
Brody•2y ago
ARG RAILWAY_ENVIRONMENT
ARG APP_PASSWORD="fake"

FROM python:3.9-slim

WORKDIR /app

COPY . /app

RUN pip3 install -r requirements.txt

ENV APP_ENVIRONMENT=$RAILWAY_ENVIRONMENT
ENV APP_PASSWORD=$ARG_PASSWORD

CMD streamlit run frontend.py --server.port $PORT
ARG RAILWAY_ENVIRONMENT
ARG APP_PASSWORD="fake"

FROM python:3.9-slim

WORKDIR /app

COPY . /app

RUN pip3 install -r requirements.txt

ENV APP_ENVIRONMENT=$RAILWAY_ENVIRONMENT
ENV APP_PASSWORD=$ARG_PASSWORD

CMD streamlit run frontend.py --server.port $PORT
p1gp3n
p1gp3nOP•2y ago
ah ok perfect i'll try it! thakns. really hope this works.
Brody
Brody•2y ago
keep in mind i still have zero clue what streamlit is
p1gp3n
p1gp3nOP•2y ago
its just a python frontend app i assume since its dockerized the actual implementation shouldnt matter a ton ive been a longtime heroku user. hate it. i just found Railway this week and love it so far. but gotta get my apps ported else im stuck with heroku
Brody
Brody•2y ago
i see, well ill do my best to help with what i can
p1gp3n
p1gp3nOP•2y ago
ugh. still not working. the deployment picked up my local configs:
page_doc_summary:<module>:12 - Request: http://localhost:8000/chat
page_doc_summary:<module>:12 - Request: http://localhost:8000/chat
vs. using the RAILWAY_ENVIRONMENT i passed in
Brody
Brody•2y ago
are you trying to access APP_ENVIRONMENT after the build stage?
p1gp3n
p1gp3nOP•2y ago
yes
Brody
Brody•2y ago
okay okay I see what's gone wrong
p1gp3n
p1gp3nOP•2y ago
i thought i passed RAILWAY_ENV.. ok
Brody
Brody•2y ago
you only need to do ARG <variable> in your dockerfile if you want to use that service variable during build, but you aren't using any variables during build once your app starts, all variables are available automatically, service variables, railway variables, everything
p1gp3n
p1gp3nOP•2y ago
correct. i just need them during runtime. but i cant call 'docker run' so im not sure how to do it
Brody
Brody•2y ago
^ no need to docker run, after start, all variables are available, there's nothing you need to do but grab them in code like you normally would
p1gp3n
p1gp3nOP•2y ago
its not working though. i have an APP_ENVIRONMENT that i use in a config.py file to determine the env value and then give me the right e.g. URLs to use in the app based on environment. when i deploy to railway, the app isn't finding this env so it defaults to localhost. so im trying to set this through the dockerfile. also the railway dashboard. can't do docker-run. so i cant figure out how to get this value to my app. i'll try to add some additonal logging to verify what the value is when i get it. but i'm pretty sure its nulls so defauilts to my localhost
Brody
Brody•2y ago
no need for setting any variables in the dockerfile, if you aren't using them during build (you are not) for example, if you set a service variable TEST = 123 you could access that variable with os.getenv("TEST") like I said, there is no need for doing anything with docker run (you can't anyway) railway will automatically inject all your service variables, and all the service variables (along with railways variables) will be available at run time
p1gp3n
p1gp3nOP•2y ago
so i removed my dockerfile ARGS. and set the env variables as Service Variables in Railway
p1gp3n
p1gp3nOP•2y ago
and then my code looks like this
p1gp3n
p1gp3nOP•2y ago
so the Railway service variables should be available to me at runtime, so that APP_ENVIRONMENT should be 'staging' so i should get my staging urls. but what has been happening is it defaults to local (localhost/8000)
Brody
Brody•2y ago
print the entire os.eviron dictionary check if your variables are in the printed text
p1gp3n
p1gp3nOP•2y ago
ah i think i see the issue...
Brody
Brody•2y ago
do tell
p1gp3n
p1gp3nOP•2y ago
looks like its working 🙂 thanks for the help! so see the file above. i actually dont call load_dotenv initially, so i dont ever set the env. it just goes to the case statement. i needed to call load_dotenv initially to get the local/railway env value, then use this to load the proper .env.[environment] file ugh i had just ported to railway, and dont use a ton of docker, so i was convinced this was the issue. when ultimately everything on railway was configured correctly 🙂 thanks for the patience. got this owrking now! on to the next issue 🙂
Brody
Brody•2y ago
awsome glad you got it solved, and glad i was of some assistance
Want results from more Discord servers?
Add your server