Dockers 在 Linux box 上工作,但不在 Windows box 上工作

Dockers work on Linux box but not on Windows box

我的 docker 在 Linux 盒子上效果很好。在我的 windows 笔记本电脑上使用类似的 docker、docker-compose 和配置文件,但它们没有。

当我浏览到 127.0.0.1:1337 时收到 502 Bad Gateway 错误。从 docker 日志 pbginx 我得到:

172.20.0.1 - - [08/Oct/2020:20:32:04 +0000] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "-"
172.20.0.1 - - [08/Oct/2020:20:33:12 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "-"
2020/10/08 20:33:12 [error] 30#30: *2 upstream prematurely closed connection while reading response header from upstream, client: 172.20.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://172.20.0.2:6060/", host: "localhost:1337"

我的 Dockerfile:

FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN chmod -R 755 /code
RUN useradd -m user
RUN chmod 777 /home/user
USER user
ENV PORT 7000
EXPOSE 7000
CMD ["gunicorn", "PB_Django.wsgi:application", "--config", "docker/pb_django/gunicorn.conf", "--keep-alive", "600"]

我的docker-compose.yml

version: "3.8"

services:
  web:
      build:
          context: ../..
          dockerfile: docker/pb_django/Dockerfile
      image: pbdjango  
      container_name: pbdjango
      expose:
        - "7000"
      networks:
        - cieenetwork
      external_links:
        - stanfordcorenlp
  nginx:
      build: ./nginx
      image: pbnginx  
      container_name: pbnginx
      ports:
        - "1337:80"
      depends_on:
        - web
      networks:
        - cieenetwork
networks:
  cieenetwork:
    external: true

我的 nginx Dockerfile:

FROM nginx:alpine

RUN rm -f /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY static_cdn /static

我的 nginx.conf 文件:

upstream pb_django {
    server web:7000 fail_timeout=0;
}

server {
    root /;

    listen 80;

    location / {
        proxy_pass http://pb_django/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_read_timeout 10m;
        proxy_connect_timeout 10m;
        client_max_body_size 700m;
    }

    location /static/ {
    }
}

我的gunicorn.conf是:

bind = "0.0.0.0:7000"
timeout = 10000

docker ps -a 显示:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES 
1b4e4d92b259        pbnginx             "/docker-entrypoint.…"   28 minutes ago      Up 28 minutes       0.0.0.0:1337->80/tcp     pbnginx 
c332c8e378e1        pbdjango            "gunicorn PB_Django.…"   28 minutes ago      Up 28 minutes       7000/tcp                 pbdjango 
1989d9a75c0d        stanfordcorenlp     "/bin/sh -c 'java -j…"   17 hours ago        Up 17 hours         0.0.0.0:9000->9000/tcp   stanfordcorenlp

docker 网络检查 cieenetwork 显示:

[
    {
        "Name": "cieenetwork",
        "Id": "a58547f1d257eae4880f5411482bb33be4534a4299e55b0e40780e3f6679f521",
        "Created": "2020-09-30T17:49:38.9145072Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1989d9a75c0d8722a89a879d57d5c2d454507ff43f3b4b79742469556d48b387": {
                "Name": "stanfordcorenlp",
                "EndpointID": "c8aff59b96aa2816daee7cf4e82b9d894a56d8870be935ac28c1b1754eb62aed",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            },
            "1b4e4d92b2597077027bbce9f8aa22a137de581592c58d7dc0204f452ce7e2ca": {
                "Name": "pbnginx",
                "EndpointID": "1f3c78aa73463610d166d3ba10792e095bee76ace3b29c1e81da567456dacdd1",
                "MacAddress": "02:42:ac:14:00:04",
                "IPv4Address": "172.20.0.4/16",
                "IPv6Address": ""
            },
            "c332c8e378e192f661ebb17f6b8ee9a88c47c76355e9a2acc4ecc34a9687a1de": {
                "Name": "pbdjango",
                "EndpointID": "c78c8d3e92472191f24f2c6bcc7108038a754bb5f933a49f59bd4e6243d1295e",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

查看“Quickstart: Compose and Django”,尝试进行测试,注释:

On certain platforms (Windows 10), you might need to edit ALLOWED_HOSTS inside settings.py and add your Docker host name or IP address to the list.
For demo purposes, you can set the value to:

ALLOWED_HOSTS = ['*']

This value is not safe for production usage
Refer to the Django documentation for more information.

如果您发现与“Dockerizing Django with Postgres, Gunicorn, and Nginx”有任何不同,请同时检查。

尽管 docker inspect pbdjango 报告 OOMKIlled 是错误的(由于内存不足而被杀死),我在 docker 桌面上将内存从 2 GB 增加到 8 GB,并且我的应用程序与以上配置。感谢所有花时间看这篇文章的人 post。