简体   繁体   中英

Deploying Django using Nginx Docker Container

Situation: I have a Django Application that I want to deploy, the tools I use for this one are Nginx, Gunicorn, and all of them are inside a docker container using Docker Desktop.

Problem: I'm able to view the django app locally using the IP of my docker, IP of my machine, and Loopback IP. However when I try to access it from my laptop(another machine connected on same wifi), I can't access it.

My Machine: Windows 10, I have already enable the expose of port 80 in the windows firewall inbound and outbound.

Steps Taken: I've tried doing python -m http.server 80 on my machine, and it's working perfectly fine so I'm sure there is something to do maybe on my Hyper-V of docker desktop or maybe nginx configuration

My docker-compose file

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

My 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"]

My nginx config file

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

Here is an image of my folder structure. Image of folder structure

QUESTION: How do I atleast make it viewable on my laptop which is connected through the same Wifi as my desktop machine? I've tried accesing it using the IP of my machine.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM