【问题标题】:Unable to access web application that's running in container built on docker无法访问在 docker 上构建的容器中运行的 Web 应用程序
【发布时间】:2022-02-19 14:42:09
【问题描述】:

我有一个简单的 python 程序,它执行以下操作

identitydock.py

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello Docker!\n'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

我的Dockerfile如下

Dockerfile

FROM python:3.4

RUN groupadd -r uwsgi && useradd -r -g uwsgi uwsgi
RUN pip install Flask==0.10.1 uWSGI==2.0.8
WORKDIR /app
COPY app /app
COPY cmd.sh /

EXPOSE 9090 9191
USER uwsgi

CMD ["/cmd.sh"]

我的cmd.sh如下:

#!/bin/bash
set -e

if [ "$ENV" = 'DEV' ]; then
  echo "Running Development Server"
  exec python "identidock.py"
else
  echo "Running Production Server"
  exec uwsgi --http 0.0.0.0:9090 --wsgi-file /app/identidock.py \
             --callable app --stats 0.0.0.0:9191
fi

当我使用 docker 运行时,由于某种原因应该返回“hello docker”的简单网页不起作用。

我给出的运行应用程序的命令:

docker build -t identidock 。

docker run -d -p 5000:5000 identidock

当我执行 curl localhost:5000 时,我收到以下消息

curl: (7) 无法连接到 localhost 端口 5000:连接被拒绝

我可以知道我的 docker 配置是否有问题吗?

我在 MacOSX 上使用以下 docker 版本

Docker 版本 17.03.1-ce,构建 c6d412e

参考来源 https://github.com/using-docker/using_docker_in_dev

编辑 -1

在 docker ps -a 上,容器状态显示为 'UP'

在 docker logs CONTAINER_ID 上,我得到以下日志

Running Production Server
*** Starting uWSGI 2.0.8 (64bit) on [Thu Jul  6 02:04:04 2017] ***
compiled with version: 4.9.2 on 06 July 2017 02:03:16
os: Linux-4.4.74-boot2docker #1 SMP Mon Jun 26 18:01:14 UTC 2017
nodename: 323e6ff35d0d
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /app
detected binary path: /usr/local/bin/uwsgi
your processes number limit is 1048576
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 http bound on 0.0.0.0:9090 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:45710 (port auto-assigned) fd 3
Python version: 3.4.6 (default, Jun 21 2017, 18:32:49)  [GCC 4.9.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1bd9640
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1bd9640 pid: 1 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1)
spawned uWSGI worker 1 (pid: 6, cores: 1)
*** Stats server enabled on 0.0.0.0:9191 fd: 13 ***
spawned uWSGI http 1 (pid: 7)

【问题讨论】:

  • 为什么需要cmd.sh?这是在做什么?
  • @cricket_007,添加了 cmd.sh
  • 当您尝试访问容器时容器是否处于活动状态?在docker ps -a 中查看其状态。另请参阅其输出:docker logs <container-id>
  • @Robert,在此处添加日志
  • 尝试进入容器内部,查看flask 服务器是否正在运行。 docker exec -it <container_name> /bin/bash

标签: python docker uwsgi


【解决方案1】:

您将主机上的端口 5000 连接到容器上的端口 5000。但是你没有在端口 5000 上运行任何东西,你在 9090 和 9091 上运行了一些东西。所以试试docker run -d -p 5000:9090 identidockdocker run -d -p 5000:9091 identidock

【讨论】:

  • 不,做 curl localhost:5000 时仍然出现同样的错误
  • ``` $ docker run -d -p 5000:9090 identidock ... $ docker logs 99 运行生产服务器 ... 无法打开 python 文件 /app/identidock.py 无法加载应用程序0 (mountpoint='') (callable not found or import error) *** 没有加载应用程序。进入完全动态模式 *** ...-- 未找到 Python 应用程序,检查启动日志是否有错误 --- ...--- 未找到 Python 应用程序,检查启动日志是否有错误 --- ... ```
【解决方案2】:

我猜你正在看书Developing and Deploying Software with Containers。我遇到了类似的问题,结果发现我没有完全按照步骤。

根据您提供的代码和命令,作者正在尝试将原始 Flask WebServer(将容器中的端口 5000 与主机中的端口 5000 绑定)替换为 uWSGI(将容器中的端口 9090 与主机中的 9090 端口绑定)。 但是docker run -d -p 5000:5000 identidock表示你坚持要将容器的5000端口和宿主机的5000端口绑定,所以你无法访问容器中运行的uWSGI。

dockerfile中使用的命令EXPOSE 9090 9191只为容器分配端口,所以需要为宿主机分配端口。

顺便提一下,作者推荐使用-P让宿主机随机分配可用端口,这样就不需要像-p 9090:9090 -p 9191:9191这样手动分配端口了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多