1

I'm unable to get the Daphne server running. It always fails with the following error:

 Traceback (most recent call last):
   File "/usr/local/bin/daphne", line 8, in <module>
     sys.exit(CommandLineInterface.entrypoint())
   File "/usr/local/lib/python3.8/site-packages/daphne/cli.py", line 171, in entrypoint
     cls().run(sys.argv[1:])
   File "/usr/local/lib/python3.8/site-packages/daphne/cli.py", line 233, in run
     application = import_by_path(args.application)
   File "/usr/local/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
     target = importlib.import_module(module_path)
   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
 ModuleNotFoundError: No module named 'apps'

/home/prassel/apps/asgi.py

django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter(
    {
        "http": django_asgi_app,
        "websocket": URLRouter([path('ws/notifications/', NotificationConsumer.as_asgi())]),
    }
)

I'm starting Daphne as follows from /home/prassel/docker-compose.yml

  daphne:
    platform: linux/amd64
    build:
      context: .
      dockerfile: Dockerfile.daphne
    command: 'sh -c "daphne -b 0.0.0.0 -p 8000  apps.asgi:application"'
    restart: always
    working_dir: /home/prassel

Dockerfile.daphne

FROM python:3.8-buster

ENV PYTHONUNBUFFERED 1

ARG REQUIREMENTS_FILE

WORKDIR /home/prassel/

RUN pip install daphne
Neo
  • 49
  • 12

1 Answers1

1

UPDATE: You're not adding your project files to your Docker container during the build, so your container doesn't have anything to run. You need to add them using a COPY or an ADD command.

It's one of the basics of Docker. You should do some reading on what are Docker containers and how they work, or you can use an official example or follow one of a countless Django + Docker tutorials available on the web.


I believe the issue lies within your Docker setup. Make sure you set WORKDIR in your Dockerfile.daphne and working_dir: in your docker-compose.yml to the same value, and you should be good.

Dockerfile:

FROM ...

WORKDIR /yourproject

COPY ...

RUN ...

...

...

docker-compose.yml:

  daphne:
    platform: linux/amd64
    build:
      context: .
      dockerfile: Dockerfile.daphne
    command: 'sh -c "daphne -b 0.0.0.0 -p 8000  apps.asgi:application"'
    restart: always
    working_dir: /yourproject
Igonato
  • 10,175
  • 3
  • 35
  • 64
  • @SelvaPrasad can you add your Dockerfile to the question. Or upload a minimal reproduction repo to GitHub? Everything should really work at this point. Also, make sure that you're running `docker compose build` after making changes to Dockerfiles – Igonato Apr 05 '23 at 10:44
  • I have added the Dockerfile contents. I will run docker compose build – Neo Apr 05 '23 at 10:47
  • Can you please tell me if I'm using the module name correctly? What is the asgi name that I need to pass on to Daphne? Is the name apps.asgi:application correct? I'm suspecting that I'm mixing it up there. – Neo Apr 05 '23 at 10:56
  • 1
    @SelvaPrasad updated my answer. Your module name is correct. Your Dockerfile is really off. You need to read up on Docker or try working without it and add it later when you need it – Igonato Apr 05 '23 at 10:57