提问者:小点点

只有 nginx 默认页面显示在 Docker 容器中


我创建了一个Flask应用程序,可以很好地运行Flask开发服务器。

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

我已经尝试将套接字权限设置为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

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

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

共1个答案

匿名用户

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

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

已更正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"]