0

I have django App that also runs some Javascript templates with node. I'm struggling to run Gunicorn as the CMD of the Dockerfile.

Here is my dockerfile:

FROM node:22.12.0-alpine3.21

RUN mkdir /app
COPY . /app

WORKDIR /app/tools
RUN npm install --loglevel verbose
RUN npm run build

WORKDIR /app
RUN ls -la

# Set environment variable for Python version
ENV PYTHON_VERSION 3.10.10

# Install dependencies and Python 3.10
RUN apk update
RUN apk update && apk add --no-cache \
    build-base \
    bash \
    libffi-dev \
    zlib-dev \
    bzip2-dev \
    ncurses-dev \
    openssl-dev \
    sqlite-dev \
    readline-dev \
    tk-dev \
    gdbm-dev \
    xz-dev \
    libnsl-dev \
    curl \
    dcron
RUN curl -O https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz \
    && tar -xvf Python-${PYTHON_VERSION}.tgz \
    && cd Python-${PYTHON_VERSION} \
    && ./configure --enable-optimizations \
    && make -j$(nproc) \
    && make altinstall \
    && cd .. \
    && rm -rf Python-${PYTHON_VERSION} Python-${PYTHON_VERSION}.tgz \
    && python3.10 --version

RUN ln -sf /usr/local/bin/python3.10 /usr/bin/python3 \
    && ln -sf /usr/local/bin/pip3.10 /usr/bin/pip3

# Verify Python and pip installation
RUN python3 --version && pip3 --version
# Install pip and upgrade setuptools and wheel
RUN pip3 install --no-cache --upgrade pip setuptools wheel
RUN pip3 install -r requirements.txt

EXPOSE 8000

ENV PYTHONPATH=/app
WORKDIR /app
CMD ["python3", "-m", "gunicorn", "--bind", "0.0.0.0:8000", "wsgi.application"]

I have a wsgi.py at the same level of the manage.py.

The problem is:

  • In the Dockerfile, CMD ["python3","manage.py", "runserver", "0.0.0.0:8000"] WORKS ✅
  • docker run --rm -it -p 8000:8000 <image> python3 -m gunicorn --bind 0.0.0.0:8000 wsgi:application WORKS ✅
  • In the Dockerfile, CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi.application"] DON'T WORK ❌

The error I'm getting:

[2025-04-06 14:54:57 +0000] [1] [INFO] Starting gunicorn 23.0.0
[2025-04-06 14:54:57 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2025-04-06 14:54:57 +0000] [1] [INFO] Using worker: sync
[2025-04-06 14:54:57 +0000] [8] [INFO] Booting worker with pid: 8
[2025-04-06 14:54:57 +0000] [8] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/gunicorn/arbiter.py", line 608, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base.py", line 135, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base.py", line 147, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/base.py", line 66, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 57, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 47, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.10/site-packages/gunicorn/util.py", line 370, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1001, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'wsgi.application'; 'wsgi' is not a package

WHY? ANY HELP WELCOME!

5
  • I don't think wsgi.application is the correct argument to gunicorn. Why are you using that? Commented Apr 6 at 15:01
  • I think we might need to see your directory structure Commented Apr 6 at 15:27
  • Think the command should be CMD ["gunicorn", "--bind", "0.0.0.0:8000", "{CORE PROJECT DIRECTORY}.wsgi.application"]. Notice the folder before wsgi. Commented Apr 6 at 15:47
  • 1
    in working version you have : in wsgi:application but in not working version you have . in wsgi.application - maybe it makes difference Commented Apr 6 at 16:21
  • 1
    (Unless you need to run a node subprocess at runtime, you might find this setup to be significantly easier if you used a FROM node AS build stage to build the frontend application, and then a FROM python stage to run the actual server, COPY --from=build the built frontend application in.) Commented Apr 6 at 16:52

1 Answer 1

1

In the Django dcumentation "How to use Django with Gunicorn", you can find this syntax for the command.

gunicorn myproject.wsgi

... It requires that your project be on the Python path; the simplest way to ensure that is to run this command from the same directory as your manage.py file.

I checked the Docker Compose setup I use for a project — it works for my ASGI application.

gunicorn myproject.asgi:application --bind 0.0.0.0:8000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.