首页 > 解决方案 > 使用 Nginx Docker 容器部署 Django

问题描述

情况:我有一个要部署的 Django 应用程序,我使用的工具是 Nginx、Gunicorn,它们都位于使用 Docker Desktop 的 docker 容器中。

问题:我可以使用我的 docker 的 IP、我的机器的 IP 和 Loopback IP 在本地查看 django 应用程序。但是,当我尝试从笔记本电脑(连接到同一 wifi 的另一台机器)访问它时,我无法访问它。

我的机器: Windows 10,我已经在 windows 防火墙入站和出站中启用了端口 80 的公开。

采取的步骤:我试过在我的机器上做 python -m http.server 80 ,它工作得很好,所以我确信在我的 docker 桌面的 Hyper-V 或 nginx 配置上可能有一些事情要做

我的 docker-compose 文件

version: '3'

services:

  dashboard:
    build: .
    volumes:
      - .:/opt/services/dashboard/src
      - static_volume:/opt/services/dashboard/src/static
    networks:  # <-- here
      - nginx_network

  nginx:
    image: nginx:1.13
    ports:
      - 0.0.0.0:80:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static_volume:/opt/services/dashboard/src/static
    depends_on:
      - dashboard
    networks:  # <-- here
      - nginx_network

networks:  # <-- and here
  nginx_network:
    driver: bridge

volumes:
  static_volume:  # <-- declare the static volume

我的码头文件

# start from an official image
FROM python:3.6

# arbitrary location choice: you can change the directory
RUN mkdir -p /opt/services/dashboard/src
WORKDIR /opt/services/dashboard/src

# install our two dependencies
RUN pip install gunicorn django requests jira python-dateutil

# copy our project code
COPY . /opt/services/dashboard/src

# expose the port 80
EXPOSE 80

# define the default command to run when starting the container
CMD ["gunicorn", "--bind", ":80", "dashboard.wsgi:application"]

我的 nginx 配置文件

# first we declare our upstream server, which is our Gunicorn application
upstream dashboard_server {
    # docker will automatically resolve this to the correct address
    # because we use the same name as the service: "djangoapp"
    server dashboard:80;
}

# now we declare our main server
server {

    listen 80;
    server_name localhost;

    location / {
        # everything is passed to Gunicorn
        proxy_pass http://dashboard_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /opt/services/dashboard/src/static/;
    }
}

这是我的文件夹结构的图像。 文件夹结构图

问题:我如何至少让它在通过与台式机相同的 Wifi 连接的笔记本电脑上可见?我已经尝试使用我机器的 IP 来访问它。

标签: pythondjangodockernginxdevops

解决方案


重新启动路由器交换机,它工作得很好。


推荐阅读