Cant run Django in Uvicorn with Dockerfile
Django with uvi corn
Hi all i am trying to get Django working with uvcorn. Guni corn works perfect but now I would like to use uvicorn since i have some async processes.
My docker file is here.
# Pull the official base image test
FROM python:3.10-slim-buster
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory in the container
WORKDIR /code
# Install dependencies
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# Copy project
COPY . /code/
RUN python manage.py collectstatic --noinput
# Expose the port uWSGI will listen on
EXPOSE 8000
# Command to run the uWSGI server
CMD ["uvicorn", "myapp.asgi:application", "--host", "0.0.0.0", "--port", "8000", "--lifespan", "off"]
The build seems to finish and also the server is running. However I get Application failed to respond on railway. What am i doing wrong? Also note that I am using RabbitMQ (Externally hosted) and views that connect to RabbitMQ. Do i need to start a worker? Local host works perfect with just the python -m uvicorn myproject.asgi:application command.
Any help is really appricated!
project id: 04e1fc6d-4c23-41fc-b62d-9f4ae06203184 Replies
Project ID:
04e1fc6d-4c23-41fc-b62d-9f4ae0620318
Hi what would the dockerfile now be exactly? Cant seem to find it out already tried CMD ["uvicorn", "myapp.asgi:application", "--host", "0.0.0.0", "--port", "8000", "--lifespan", "off"]
managed to get it working! I had to add below
SOLUTION:
# Pull the official base image test
FROM python:3.10-slim-buster
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PORT=8000
# Expose the port
EXPOSE $PORT
# Set work directory in the container
WORKDIR /code
# Install dependencies
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# Copy project
COPY . /code/
RUN python manage.py collectstatic --noinput
# Command to run the uWSGI server
CMD uvicorn myapp.asgi:application --host 0.0.0.0 --port $PORT
I'm glad you got it working, for future reference you can enclose code with triple back-ticks to format your code like so..
it just makes it easier for us to read 🙂