Issue Deploying FastAPI Backend on Railway with Docker
abidjohar
PROOP

3 months ago

I am trying to deploy my FastAPI backend on Railway using a Dockerfile, but I’m running into an issue where the container fails to start. Sometimes the error shows poetry: not found, and other times it shows uvicorn: not found.

Here is a snippet of the error during deployment:

[Region: us-east4]

Using Detected Dockerfile

Container failed to start

The executable /root/.local/bin/uvicorn could not be found.

I have installed dependencies globally using Poetry in the Dockerfile, so I expected uvicorn to be available, but it still cannot be found when the container starts.

I suspect this may be related to Poetry’s default installation path not being included in the container’s PATH, or the commands being executed outside the virtual environment.

Could you please guide me on the correct approach to ensure that both Poetry and Uvicorn are properly available in the Docker container at runtime?

Thank you for your help.

Best regards,
Abid Hussain

$10 Bounty

1 Replies

3 months ago

Hey!
My recommendation if you're using a Dockerfile is to always make sure they work on your machine before you ship to railway.
If your container builds and runs on your machine it will work totally fine once deployed hereslightly_smiling_face emoji

That said, here's an example Dockerfile that should work just fine Poetry:

FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1
ENV POETRY_VIRTUALENVS_CREATE=false
ENV POETRY_NO_INTERACTION=1

WORKDIR /app

# Install system deps (optional, depends on your libs)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN pip install --no-cache-dir poetry

# Copy only dependency files first (cache-friendly)
COPY pyproject.toml poetry.lock ./

# Install dependencies (no venv, no dev deps)
RUN poetry install --only main --no-root

# Now copy the app
COPY . .

# Install the local package (fast, no re-solving)
RUN poetry install --only main

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

You can add in your own deps in the apt-get step if needed / alter as needed.
Does your Dockerfile look similar to this one?

Railway has a guide on deploying with FastAPI but they suggest using a requirements.txt to keep thing simple. You can check that out here if you're interested in that as an example.
You can also generate a requirements.txt from your Poetry lock file and use the example Dockerfile in that link if you'd like:

just do poetry export -f requirements.txt --output requirements.txt and commit the requirements.txt alongside your Dockerfile. Given you're already using Poetry I would just stick with that for simplicity though.


Loading...