【问题标题】:Only nginx default page showing in Docker containerDocker 容器中仅显示 nginx 默认页面
【发布时间】:2021-12-30 09:43:32
【问题描述】:

我创建了一个在 Flask 开发服务器上运行良好的 Flask 应用程序。

现在,我正在尝试在 docker 容器中运行这个 Flask 应用程序。虽然可以成功构建容器(使用docker build . -t minex_image)并运行(使用 docker run --name minex_container -p 80:80 minex_image),但应用程序的主页不显示。相反,我打开localhost:80时只得到nginx默认页面。

我已经尝试过setting the socket permissions to 666,但无济于事。任何帮助将不胜感激。

这是来自 nginx 和 uWSGI 的日志:

Starting nginx: nginx.

[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Fri Nov 19 15:28:04 2021] ***
compiled with version: 8.3.0 on 19 November 2021 15:26:21
os: Linux-5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020
nodename: a551630c2d9e
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /srv/minexample_app
detected binary path: /usr/local/bin/uwsgi
setgid() to 33
setuid() to 33
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.socket fd 3
Python version: 3.7.4 (default, Oct 17 2019, 05:59:21) [GCC 8.3.0]

*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x55ce21285920
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 437520 bytes (427 KB) for 5 cores

*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55ce21285920 pid: 25 (default app)

*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 25)
spawned uWSGI worker 1 (pid: 28, cores: 1)
spawned uWSGI worker 2 (pid: 29, cores: 1)
spawned uWSGI worker 3 (pid: 30, cores: 1)
spawned uWSGI worker 4 (pid: 31, cores: 1)
spawned uWSGI worker 5 (pid: 32, cores: 1)

附录 - 应用程序文件

应用程序文件夹包含以下文件:

app
|__src
|  |__app.py
|  |__templates
|     |__home.html
|__Dockerfile
|__nginx.conf
|__requirements.txt
|__start.sh
|__uwsgi.ini

重要的配置文件可以在下面找到。我使用了来自herethere 的示例来生成配置文件。

Dockerfile

#Download Python from DockerHub and use it
FROM python:3.7.4
#Set the working directory in the Docker container
WORKDIR /srv/minexample_app
#Copy the configuration files to the working directory
COPY requirements.txt .
COPY start.sh .
COPY uwsgi.ini .
#Copy the Flask app code to the working directory
COPY src/ .
#Copy nginx configuration file to the nginx folder
COPY nginx.conf /etc/nginx
#Install nginx web server
RUN apt-get clean \
    && apt-get -y update
RUN apt-get -y install nginx \
    && apt-get -y install python3-dev \
    && apt-get -y install build-essential
#Install the dependencies
RUN pip install -r requirements.txt --src /usr/local/src
#Run the container
RUN chmod +x ./start.sh
CMD ["./start.sh"]

start.sh

#!/usr/bin/env bash
service nginx start
uwsgi --ini uwsgi.ini

【问题讨论】:

    标签: python docker nginx flask uwsgi


    【解决方案1】:

    我找到了 nginx 配置不正确的原因。在 Dockerfile 中,我将 nginx 配置文件复制到文件夹/etc/nginx。之后我通过apt-get安装了nginx,导致我的配置被默认配置文件覆盖了。

    因此,需要通过将COPY nginx.conf /etc/nginx 移到apt-get 后面来更正Dockerfile。

    已更正Dockerfile

    #Download Python from DockerHub and use it
    FROM python:3.7.4
    #Set the working directory in the Docker container
    WORKDIR /srv/minexample_app
    #Copy the configuration files to the working directory
    COPY requirements.txt .
    COPY start.sh .
    COPY uwsgi.ini .
    #Copy the Flask app code to the working directory
    COPY src/ .
    
    #Install nginx web server
    RUN apt-get clean \
        && apt-get -y update
    RUN apt-get -y install nginx \
        && apt-get -y install python3-dev \
        && apt-get -y install build-essential
    #Install the dependencies
    RUN pip install -r requirements.txt --src /usr/local/src
    
    #Copy nginx configuration file to the nginx folder
    COPY nginx.conf /etc/nginx
    #Run the container
    RUN chmod +x ./start.sh
    CMD ["./start.sh"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      相关资源
      最近更新 更多