FastAPI deployment failing - Error: '$PORT' is not a valid integer despite multiple CMD formats
vishalj99
HOBBYOP

a year ago

I'm deploying a FastAPI backend and consistently getting this error:

Error: Invalid value for '--port': '$PORT' is not a valid integer.

What I've tried:

1. Dockerfile with exec form CMD:

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

2. Dockerfile with shell form CMD:

CMD uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}

3. Dockerfile with explicit sh -c:

CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"]

4. railway.toml configuration:

[build]

builder = "nixpacks"

[deploy]

startCommand = "uvicorn app.main:app --host 0.0.0.0 --port $PORT"

All attempts result in the same error. The build succeeds but the container fails to start.

Project structure:

backend/

├── Dockerfile

├── railway.toml

├── requirements.txt (fastapi, uvicorn)

└── app/

└── main.py

Is Railway using my Dockerfile or Nixpacks? How can I properly pass the PORT variable to uvicorn?

Deployment logs show:

- Build succeeds

- Container starts

- Immediately fails with PORT error

- Health check fails repeatedly

Any help appreciated!

Solved$10 Bounty

1 Replies

Railway
BOT

a year ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


Status changed to Solved vishalj99 about 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...