使用Nginx Docker容器部署Django

时间:2019-07-09 23:18:27

标签: python django docker nginx devops

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

问题: :我可以使用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

我的dockerfile

# 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/;
    }
}

这是我的文件夹结构的图像。 Image of folder structure

问题:我如何使它在通过与台式机相同的Wifi连接的笔记本电脑上可见?我尝试使用计算机的IP对其进行访问。

1 个答案:

答案 0 :(得分:0)

重新启动路由器交换机,它工作正常。