【发布时间】:2019-11-19 13:00:00
【问题描述】:
情况:我有一个想要部署的 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
我的码头文件
# 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 访问它。
【问题讨论】:
-
您说您从连接到同一网络的笔记本电脑“无法访问它”。你试过什么网址?您应该转到运行 Web 应用程序的机器的本地 IP,例如 192.168.0.X,其中 X 介于 2 和 253(254?)之间。
-
我已经编辑了问题部分,是的,我尝试使用 IP 访问它。我正在使用 docker desktop btw,我在想也许我需要设置它的网络?
-
从本地机器访问 URL 是否有效?
-
是的。我在想这可能是 Hyper-V 的 NAT 的问题?这是一种可能。但我还不确定。
-
为什么要在 Dockerfile 中评论 EXPOSE 80?仪表板服务需要暴露 80 端口吗?
标签: python django docker nginx devops