Docker 容器中仅显示 nginx 默认页面

Only nginx default page showing in Docker container

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

现在,我正尝试将此 Flask 应用程序 运行 放入 docker 容器中。虽然可以成功构建容器(使用 docker build . -t minex_image)和 运行(使用 docker run --name minex_container -p 80:80 minex_image),但应用程序的主页不会显示。相反,我只在打开 localhost:80.

时获得 nginx 默认页面

我已经尝试过 ,但无济于事。任何帮助将不胜感激。

这是来自 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

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

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

我找到了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"]