deploying fastapi with docker

Can't figure out how to make the app listen when deployed and assigned an url to. Works perfect when runned locally. dockerfile:
# Use Python image as base image
FROM python:3.11.6

# Set working directory in the container
WORKDIR /baseApp

# Copy the contents of the scraper directory to the container's /app directory
COPY . /baseApp

# Install project dependencies
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt

# Expose the port that FastAPI will run on
EXPOSE 8000

# Command to run the FastAPI application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
# Use Python image as base image
FROM python:3.11.6

# Set working directory in the container
WORKDIR /baseApp

# Copy the contents of the scraper directory to the container's /app directory
COPY . /baseApp

# Install project dependencies
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt

# Expose the port that FastAPI will run on
EXPOSE 8000

# Command to run the FastAPI application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Solution:
change your CMD to this
CMD uvicorn main:app --host 0.0.0.0 --port $PORT
CMD uvicorn main:app --host 0.0.0.0 --port $PORT
...
Jump to solution
5 Replies
Percy
Percy11mo ago
Project ID: N/A
Solution
Brody
Brody11mo ago
change your CMD to this
CMD uvicorn main:app --host 0.0.0.0 --port $PORT
CMD uvicorn main:app --host 0.0.0.0 --port $PORT
Brody
Brody11mo ago
more info about this sort of thing here https://docs.railway.app/troubleshoot/fixing-common-errors
Fantastisk
Fantastisk11mo ago
thank you so much!
Brody
Brody11mo ago
no problem!
Want results from more Discord servers?
Add your server