每次我在 Docker 上使用 Django 应用程序中的请求模块时出现 uWSGI 错误

uWSGI error every time I use the requests module in Django app on Docker

我正在 运行使用 Docker 中的 uWSGI 和 docker-compose 构建一个 Django 应用程序。我每次都遇到同样的错误:

  1. 使用 AJAX
  2. 发送 POST 请求
  3. 在我看来,在处理上述请求时,我使用了 python 的请求模块,即 r = requests.get(some_url)

uWSGI 表示如下:

!!! uWSGI process 13 got Segmentation Fault !!!
DAMN ! worker 1 (pid: 13) died :( trying respawn ...
Respawned uWSGI worker 1 (new pid: 24)
spawned 4 offload threads for uWSGI worker 1

浏览器中的控制台显示 net::ERR_EMPTY_RESPONSE

我试过在不同的地方使用请求模块,无论我把它放在哪里,我都会遇到同样的分段错误。我还能够 运行 docker 以外的一切都没有错误,所以我将其缩小到:docker + requests module = errror.

是否有什么东西可以阻止从 docker 容器中通过请求模块发送的请求?预先感谢您的帮助。

这是我的 uwsgi.ini 文件:

[uwsgi]
chdir           = %d
module          = my_project.wsgi:application
master          = true
processes       = 2
http            = 0.0.0.0:8000
vacuum          = true
pidfile         = /tmp/my_project.pid
daemonize       = %d/my_project.log
check-static    = %d
static-expires  = /* 7776000
offload-threads = %k
uid             = 1000
gid             = 1000
# there is no /etc/mime.types on the docker Arch Linux image
mime-file       = %d/mime.types

Docker文件:

FROM alpine:3.8
ENV PYTHONUNBUFFERED 1

RUN mkdir /my_project
WORKDIR /my_project

RUN apk add build-base python3-dev py3-pip python3

# deps for python cryptography
RUN apk add libffi-dev musl-dev openssl-dev

# dep for uwsgi
RUN apk add linux-headers

ADD requirements.txt /my_project/
RUN pip3 install -r requirements.txt

ADD . /my_project/

ENTRYPOINT ./start.sh

docker-compose.yml:

version: '3'

services:
  web:
    build: .
    entrypoint: ./start.sh
    volumes:
      - .:/my_project
    ports:
      - "8000:8000"
    environment:
      - DEBUG_LEVEL=INFO
    network_mode: "host"

start.sh:

#!/bin/sh
echo '' > logfile.log
uwsgi --ini uwsgi.ini
tail -f logfile.log

解决方案:将基本映像更改为 Ubuntu 16.04,现在一切正常。