Docker Compose 中的 Azure Web 应用程序仅显示 Nginx 主页的问题

问题描述 投票:0回答:1

我在 Azure Web 应用程序上部署项目时遇到问题已经好几天了,但一直无法解决。我觉得它已经很接近工作了,但是 nginx 为我的 Flask 应用程序提供服务的方式似乎存在问题。我的 azure 网站只显示 nginx 登陆页面:

我正在使用 Azure Web 应用程序“Docker Compose(预览版)”来部署此应用程序。注册表源来自我的 Docker Hub 存储库。我的 docker-compose.yml 文件如下所示:

version: "3"

services:
  app:
    build:
      context: ./
    image: tracrux/irelandwindfarms
    command: gunicorn wsgi:app --bind 0.0.0.0:5000
    volumes:
      - ${WEBAPP_STORAGE_HOME}:/flaskapp/
      - ${WEBAPP_STORAGE_HOME}static:/flaskapp/static
    ports:
      - "5000"


  nginx:
    image: nginx:latest
    volumes:
      - ${WEBAPP_STORAGE_HOME}nginx.conf:/etc/nginx/nginx.conf:ro
      - ${WEBAPP_STORAGE_HOME}static:/flaskapp/static
    ports:
      - "80:80"
    depends_on:
      - app

volumes:
  static:

我不确定这是否是一个坏主意,让 Docker-compose 文件从上下文构建我的应用程序,然后也从 dockerhub 中提取图像。我猜我的上下文部分在这里是多余的,因为它只会被图像覆盖。另外,对于“${WEBAPP_STORAGE_HOME}”(天蓝色的网络应用程序日志对我尖叫,我需要在我的卷路径中),我已在 .env 文件中将其设置为 WEBAPP_STORAGE_HOME=./ “./”是它工作时在本地设置的路径,所以我只是想复制这个。我的 Dockerfile 看起来像这样:

FROM python:3.12 # set the working directory 
WORKDIR /flaskapp # install dependencies 
COPY ./requirements.txt /flaskapp 
RUN pip install --no-cache-dir --upgrade -r requirements.txt # Copy your Flask application code 
COPY . . 
EXPOSE 5000

我的 nginx.conf 文件如下所示:

events {
    worker_connections 1000;
}

upstream my_site_proxy {
  server app:5000;
}

http {
    server {
    listen 80;
    listen [::]:80;
    server_name  https://irelandwindfarms.azurewebsites.net/;
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://my_site_proxy/;
            proxy_redirect off;
        }

        location /static/ {
            alias /flaskapp/static/;
        }
    }
}

我已经尝试了网上能找到的所有方法来解决这个问题。我已经重新配置了我的 docker-compose.yml、dockerfile 和 nginx.conf 很多次了,无论我做什么,我都看不到让它工作。这是我的部署中心日志,表明一切正常:

2024-03-15T18:00:11.445Z INFO - Starting multi-container app..
2024-03-15T18:00:12.038Z INFO - Pulling image: tracrux/irelandwindfarms
2024-03-15T18:00:13.417Z INFO - latest Pulling from tracrux/irelandwindfarms
2024-03-15T18:00:13.427Z INFO - Digest: sha256:dbb401c5936d150717e74541d1fc53419dcdd330929599eccbb4c8e00dd321a4
2024-03-15T18:00:13.429Z INFO - Status: Image is up to date for tracrux/irelandwindfarms:latest
2024-03-15T18:00:13.439Z INFO - Pull Image successful, Time taken: 1 Seconds
2024-03-15T18:00:13.488Z INFO - Starting container for site
2024-03-15T18:00:13.490Z INFO - docker run -d --name irelandwindfarms_app_0_cad2ad5c -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITE_SITE_NAME=irelandwindfarms -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=irelandwindfarms.azurewebsites.net -e WEBSITE_INSTANCE_ID=11af5b4880bcafd0e33c899dd84747a89c767943adf0db58295f85ef997b95d4 tracrux/irelandwindfarms gunicorn wsgi:app --bind 0.0.0.0:5000
2024-03-15T18:00:13.504Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2024-03-15T18:00:13.928Z INFO - Pulling image: nginx:latest
2024-03-15T18:00:15.054Z INFO - latest Pulling from library/nginx
2024-03-15T18:00:15.101Z INFO - Digest: sha256:6db391d1c0cfb30588ba0bf72ea999404f2764febf0f1f196acd5867ac7efa7e
2024-03-15T18:00:15.105Z INFO - Status: Image is up to date for nginx:latest
2024-03-15T18:00:15.120Z INFO - Pull Image successful, Time taken: 1 Seconds
2024-03-15T18:00:15.154Z INFO - Starting container for site
2024-03-15T18:00:15.155Z INFO - docker run -d -p 8362:80 --name irelandwindfarms_nginx_0_cad2ad5c -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITE_SITE_NAME=irelandwindfarms -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=irelandwindfarms.azurewebsites.net -e WEBSITE_INSTANCE_ID=11af5b4880bcafd0e33c899dd84747a89c767943adf0db58295f85ef997b95d4 nginx:latest
2024-03-15T18:00:15.157Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2024-03-15T18:00:22.966Z INFO - Started multi-container app
2024-03-15T18:00:22.972Z INFO - Initiating warmup request to container irelandwindfarms_nginx_0_cad2ad5c for site irelandwindfarms
2024-03-15T18:00:22.974Z INFO - Container irelandwindfarms_nginx_0_cad2ad5c for site irelandwindfarms initialized successfully and is ready to serve requests.

任何人对问题可能是什么有任何想法,我将不胜感激。

azure flask nginx docker-compose azure-web-app-service
1个回答
0
投票

我尝试了你的代码,做了一些更改,并且能够通过 dockerhub 将其部署到 Azure,没有任何问题。

我的docker-compose.yml:

version: "3"
services:
app:
build:
context: ./
image: tracrux/irelandwindfarms
command: gunicorn wsgi:app --bind 0.0.0.0:5000
volumes:
- .:/flaskapp
- ./static:/flaskapp/static
ports:
- "5000:5000"
nginx:
image: nginx:latest
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./static:/flaskapp/static
ports:
- "80:80"
depends_on:
- app
volumes:
static:

nginx.conf:

events {
worker_connections 1000;
}
http {
server {
listen 80;
listen [::]:80;
server_name irelandwindfarms.azurewebsites.net;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app:5000;
proxy_redirect off;
}
location /static/ {
alias /flaskapp/static/;
}
}
}

Dockerfile:

FROM  python:3.12
WORKDIR  /app
COPY  requirements.txt  requirements.txt
RUN  pip  install  -r  requirements.txt
COPY  .  .
EXPOSE  5000
CMD  ["python",  "wsgi.py"]

这是本地输出: enter image description here

enter image description here

运行以下命令将 docker 镜像推送到 docker hub。

docker login

使用您的 Docker Hub 用户名和存储库名称标记您的 Docker 映像:

docker tag your_image_name:latest your_dockerhub_username/your_repository_name:latest

将标记的镜像推送到 Docker Hub:

docker push your_dockerhub_username/your_repository_name

创建 Web 应用程序时,选择 docker 容器作为发布选项并提供所需的详细信息

enter image description here

这是 Azure 应用服务输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.