Python Server Crashing in Railway
mihirpenugonda
PROOP

2 years ago

When we run the server locally it runs with no issues but it keeps crashing in railway

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

16 Replies

unicodeveloper
PRO

2 years ago

Can I see what your Dockerfile looks like?


Status changed to Awaiting User Response Railway over 1 year ago


unicodeveloper

Can I see what your Dockerfile looks like?

mihirpenugonda
PROOP

2 years ago

```

FROM python:3.11-slim

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    git \
    && rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Copy requirements first to leverage Docker cache
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu && \
    pip install --no-cache-dir -r requirements.txt

# Copy the entire application with the correct structure
COPY . .

# Set Python path - this is important because you have nested modules
ENV PYTHONPATH="${PYTHONPATH}:/app"

# Create a non-root user and set proper permissions
RUN useradd -m appuser && \
    chown -R appuser:appuser /app && \
    chmod -R 755 /app

USER appuser

# Expose the port the app runs on
EXPOSE 8000

# Command to run the application
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]```

Status changed to Awaiting Railway Response Railway over 1 year ago


mihirpenugonda

\`\`\` ```dockerfile FROM python:3.11-slim # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ git \ && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /app # Copy requirements first to leverage Docker cache COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu && \ pip install --no-cache-dir -r requirements.txt # Copy the entire application with the correct structure COPY . . # Set Python path - this is important because you have nested modules ENV PYTHONPATH="${PYTHONPATH}:/app" # Create a non-root user and set proper permissions RUN useradd -m appuser && \ chown -R appuser:appuser /app && \ chmod -R 755 /app USER appuser # Expose the port the app runs on EXPOSE 8000 # Command to run the application CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]``` ```

mihirpenugonda
PROOP

2 years ago

I can see that its a lot of import errors in the build but I am not able to understand why my local build works but this does not.


unicodeveloper
PRO

2 years ago

Issue might be from the PYTHONPATH. Is setting it that way correct?


Status changed to Awaiting User Response Railway over 1 year ago


unicodeveloper
PRO

2 years ago

Can you try it without setting the PATH?


mihirpenugonda
PROOP

2 years ago

gotcha lemme get back to you on this once


Status changed to Awaiting Railway Response Railway over 1 year ago


unicodeveloper

Can you try it without setting the PATH?

mihirpenugonda
PROOP

2 years ago

just tried doing this, still getting the same error, if you see the logs its a path issue which we are not able to recreate locally


unicodeveloper
PRO

2 years ago

Have you been able to build with that Dockerfile locally and then run the image?

I need to know what errors you are getting run it with Dockerfile locally


Status changed to Awaiting User Response Railway over 1 year ago


unicodeveloper

Have you been able to build with that Dockerfile locally and then run the image? I need to know what errors you are getting run it with Dockerfile locally

mihirpenugonda
PROOP

2 years ago

yes we have been able to build it locally and it works, but not on railway


Status changed to Awaiting Railway Response Railway over 1 year ago


unicodeveloper
PRO

2 years ago

Can i see a screenshot of it working on Docker locally?


Status changed to Awaiting User Response Railway over 1 year ago


mihirpenugonda
PROOP

2 years ago

this is the screenshot


Status changed to Awaiting Railway Response Railway over 1 year ago


mihirpenugonda
PROOP

2 years ago

any chance we can setup a short call to discuss this once?


unicodeveloper
PRO

2 years ago

Can you try this?

```

FROM python:3.11-slim

RUN apt-get update && apt-get install -y \

build-essential \

git \

&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir --upgrade pip && \

pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu && \

pip install --no-cache-dir -r requirements.txt

COPY . .

ENV PYTHONPATH=/app

EXPOSE 8000

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

```


Status changed to Awaiting User Response Railway over 1 year ago


unicodeveloper

Can you try this? \`\`\` FROM python:3.11-slim RUN apt-get update && apt-get install -y \\ build-essential \\ git \\ && rm -rf /var/lib/apt/lists/\* WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip && \\ pip install --no-cache-dir torch torchvision --index-url <https://download.pytorch.org/whl/cpu> && \\ pip install --no-cache-dir -r requirements.txt COPY . . ENV PYTHONPATH=/app EXPOSE 8000 CMD \["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"\] \`\`\`

mihirpenugonda
PROOP

2 years ago

still facing the same issue. the docker container is working locally as well


Status changed to Awaiting Railway Response Railway over 1 year ago


unicodeveloper
PRO

2 years ago

I really don't know what could be wrong here. Is your server.py file in the root of your directory?


Status changed to Awaiting User Response Railway over 1 year ago


mihirpenugonda

\`\`\` ```dockerfile FROM python:3.11-slim # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ git \ && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /app # Copy requirements first to leverage Docker cache COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu && \ pip install --no-cache-dir -r requirements.txt # Copy the entire application with the correct structure COPY . . # Set Python path - this is important because you have nested modules ENV PYTHONPATH="${PYTHONPATH}:/app" # Create a non-root user and set proper permissions RUN useradd -m appuser && \ chown -R appuser:appuser /app && \ chmod -R 755 /app USER appuser # Expose the port the app runs on EXPOSE 8000 # Command to run the application CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]``` ```

2 years ago

I think you should consider using a virtual environment within your Docker container. Here's a blog post I wrote on deploying Django but can be used for yours with a few tweaks. You might consider using gunicorn with unicorn workers. Original post is here but the Dockerfile is:

# Set the python version as a build-time argument

# with Python 3.12 as the default

ARG PYTHON_VERSION=3.12-slim-bullseye

FROM python:${PYTHON_VERSION}

# Create a virtual environment

RUN python -m venv /opt/venv

# Set the virtual environment as the current location

ENV PATH=/opt/venv/bin:$PATH

# Upgrade pip

RUN pip install --upgrade pip

# Set Python-related environment variables

ENV PYTHONDONTWRITEBYTECODE 1

ENV PYTHONUNBUFFERED 1

# Install os dependencies for our mini vm

RUN apt-get update && apt-get install -y \

# for postgres

libpq-dev \

# for Pillow

libjpeg-dev \

# for CairoSVG

libcairo2 \

# other

gcc \

&& rm -rf /var/lib/apt/lists/*

# Create the mini vm's code directory

RUN mkdir -p /code

# Set the working directory to that same code directory

WORKDIR /code

# Copy the requirements file into the container

COPY requirements.txt /tmp/requirements.txt

# copy the project code into the container's working directory

COPY ./src /code

# Install the Python project requirements

RUN pip install -r /tmp/requirements.txt

# database isn't available during build

# run any other commands that do not need the database

# such as:

# RUN python manage.py collectstatic --noinput

# set the Django default project name

ARG PROJ_NAME="cfehome"

# create a bash script to run the Django project

# this script will execute at runtime when

# the container starts and the database is available

RUN printf "#!/bin/bash\n" > ./paracord_runner.sh && \

printf "RUN_PORT=\"\${PORT:-8000}\"\n\n" >> ./paracord_runner.sh && \

printf "python manage.py migrate --no-input\n" >> ./paracord_runner.sh && \

printf "gunicorn ${PROJ_NAME}.wsgi:application --bind \"0.0.0.0:\$RUN_PORT\"\n" >> ./paracord_runner.sh

# make the bash script executable

RUN chmod +x paracord_runner.sh

# Clean up apt cache to reduce image size

RUN apt-get remove --purge -y \

&& apt-get autoremove -y \

&& apt-get clean \

&& rm -rf /var/lib/apt/lists/*

# Run the Django project via the runtime script

# when the container starts

CMD ./paracord_runner.sh

I have found that using a script as your run command yields more consistent results. To make this work, you'd change:

RUN printf "#!/bin/bash\n" > ./paracord_runner.sh && \

printf "RUN_PORT=\"\${PORT:-8000}\"\n\n" >> ./paracord_runner.sh && \

printf "python manage.py migrate --no-input\n" >> ./paracord_runner.sh && \

printf "gunicorn ${PROJ_NAME}.wsgi:application --bind \"0.0.0.0:\$RUN_PORT\"\n" >> ./paracord_runner.sh

To

RUN printf "#!/bin/bash\n" > ./paracord_runner.sh && \

printf "RUN_PORT=\"\${PORT:-8000}\"\n\n" >> ./paracord_runner.sh && \

printf "gunicorn server:app -k uvicorn.workers.UvicornWorker--bind \"0.0.0.0:\$RUN_PORT\"\n" >> ./paracord_runner.sh

Be sure to add guincorn to requirements.txt and/or as another step within the dockerfile. Try this with and without pytorch.

Edit: Sorry for the formatting, multi-line code pasting isn't working for some reason.


Status changed to Awaiting Railway Response Railway over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...