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:applicationWORKS ✅- 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!
wsgi.applicationis the correct argument togunicorn. Why are you using that?:inwsgi:applicationbut in not working version you have.inwsgi.application- maybe it makes differencenodesubprocess at runtime, you might find this setup to be significantly easier if you used aFROM node AS buildstage to build the frontend application, and then aFROM pythonstage to run the actual server,COPY --from=buildthe built frontend application in.)