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
Project ID:
N/A
may I ask why you are using a dockerfile? your dockerfile is not doing anything nixpacks can't do
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.
I know why, but help me understand your need for the dockerfile first please? why does a react frontend need a dockerfile?
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.
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 dockerfilespent 3 hrs trying different methods to sort the issue, ended up giving PORT a fixed number to make it work, thanks for the help
that's unnecessary and not the recommended way to go about it
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.
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?
Yes I tried, and it deployed successfully, thanks a lot 🙏
awsome!