not able to access PORT environment variable in dockerfile

Dockerfile code is written below and railway deploy logs display an error saying $PORT is not a valid integer thereby meaning that port variable is not read. FROM python:3.10 WORKDIR /code COPY ./requirements.txt /code/requirements.txt RUN pip install --no-cache-dir --upgrade -r ./requirements.txt COPY ./app /code/app ARG PORT ENV PORT=$PORT CMD [ "uvicorn", "app.main:app", "--host", "0.0.0.0" , "--port" , "$PORT" ]
12 Replies
Percy
Percy17mo ago
Project ID: N/A
Brody
Brody17mo ago
may I ask why you are using a dockerfile? your dockerfile is not doing anything nixpacks can't do
.aizen_sama
.aizen_sama17mo ago
I am trying to learn dockerfile as I am using it in another project involving react frontend . Yes, nixpacks is not giving this error, but no clue why dockerfile gives the error.
Brody
Brody17mo ago
I know why, but help me understand your need for the dockerfile first please? why does a react frontend need a dockerfile?
.aizen_sama
.aizen_sama17mo ago
So, I am trying to create docker containers for my fronend and backend, that's why learning to use dockerfile, could you please tell me where I am going wrong ? thanks.
Brody
Brody17mo ago
two possible reasons, ARG PORT is creating a blank PORT variable or $PORT is not being escaped, fix both these potential issues by writing your CMD command out like this CMD uvicorn app.main:app --host 0.0.0.0 --port $PORT and remove the ARG and ENV directives from your dockerfile
.aizen_sama
.aizen_sama17mo ago
spent 3 hrs trying different methods to sort the issue, ended up giving PORT a fixed number to make it work, thanks for the help
Brody
Brody17mo ago
that's unnecessary and not the recommended way to go about it
.aizen_sama
.aizen_sama17mo ago
I used ARG PORT cause it was mentioned in this doc https://docs.railway.app/deploy/dockerfiles and I also read online that "$PORT" does make it escape, so used it in my dockerfile.
Railway Docs
Dockerfiles | Railway Docs
Documentation for Railway
Brody
Brody17mo ago
the PORT variable is not available during build, there's no use in doing ARG PORT it will just create an empty variable. have you tried the changes I suggested?
.aizen_sama
.aizen_sama17mo ago
Yes I tried, and it deployed successfully, thanks a lot 🙏
Brody
Brody17mo ago
awsome!
Want results from more Discord servers?
Add your server