【问题标题】:Can't see my running Docker Container on localhost [duplicate]在本地主机上看不到我正在运行的 Docker 容器 [重复]
【发布时间】:2020-03-31 14:03:34
【问题描述】:

我有一个简单的 python 应用程序,我想在 Docker 映像中运行。应用程序如下所示

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

还有一个看起来像这样的 dockerfile:

FROM python:3
RUN pip install --upgrade pip
RUN pip install flask
CMD ["python","app.py"]
COPY app.py /app.py

除了这两个文件之外,文件夹结构的其余部分如下所示:

├───.idea
│   └───inspectionProfiles
├───static
├───templates
├───venv
│   ├───Include
│   ├───Lib
│   │   ├───site-packages
│   │   │   └───pip-19.0.3-py3.7.egg
│   │   │       ├───EGG-INFO
│   │   │       └───pip
│   │   │           ├───_internal
│   │   │           │   ├───cli
│   │   │           │   ├───commands
│   │   │           │   ├───models
│   │   │           │   ├───operations
│   │   │           │   ├───req
│   │   │           │   ├───utils
│   │   │           │   └───vcs
│   │   │           └───_vendor
│   │   │               ├───cachecontrol
│   │   │               │   └───caches
│   │   │               ├───certifi
│   │   │               ├───chardet
│   │   │               │   └───cli
│   │   │               ├───colorama
│   │   │               ├───distlib
│   │   │               │   └───_backport
│   │   │               ├───html5lib
│   │   │               │   ├───filters
│   │   │               │   ├───treeadapters
│   │   │               │   ├───treebuilders
│   │   │               │   ├───treewalkers
│   │   │               │   └───_trie
│   │   │               ├───idna
│   │   │               ├───lockfile
│   │   │               ├───msgpack
│   │   │               ├───packaging
│   │   │               ├───pep517
│   │   │               ├───pkg_resources
│   │   │               ├───progress
│   │   │               ├───pytoml
│   │   │               ├───requests
│   │   │               ├───urllib3
│   │   │               │   ├───contrib
│   │   │               │   │   └───_securetransport
│   │   │               │   ├───packages
│   │   │               │   │   ├───backports
│   │   │               │   │   └───ssl_match_hostname
│   │   │               │   └───util
│   │   │               └───webencodings
│   │   └───tcl8.6
│   └───Scripts
└───__pycache__

然后我从 Powershell 通过编写以下命令构建 Docker 映像:

docker build . -t myusername/flaskapp
PS C:\Users\mypcuser\projects\flask_docker_test> docker build . -t myusername/flaskapp
Sending build context to Docker daemon  19.49MB
Step 1/5 : FROM python:3
 ---> f88b2f81f83a
Step 2/5 : RUN pip install --upgrade pip
 ---> Running in 56dc287d7501
Requirement already up-to-date: pip in /usr/local/lib/python3.8/site-packages (20.0.2)
Removing intermediate container 56dc287d7501
 ---> 2dff8ebf09c6
Step 3/5 : RUN pip install flask
 ---> Running in 5b59f8968a63
Collecting flask
  Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
Collecting Werkzeug>=0.15
  Downloading Werkzeug-1.0.0-py2.py3-none-any.whl (298 kB)
Collecting click>=5.1
  Downloading click-7.1.1-py2.py3-none-any.whl (82 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.1-py2.py3-none-any.whl (126 kB)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl (32 kB)
Installing collected packages: Werkzeug, click, MarkupSafe, Jinja2, itsdangerous, flask
Successfully installed Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 click-7.1.1 flask-1.1.1 itsdangerous-1.1.0
Removing intermediate container 5b59f8968a63
 ---> 7583bc2d8be6
Step 4/5 : CMD ["python","app.py"]
 ---> Running in 9394be530612
Removing intermediate container 9394be530612
 ---> 53e72fb77552
Step 5/5 : COPY app.py /app.py
 ---> 5925b08ae09e
Successfully built 5925b08ae09e
Successfully tagged myusername/flaskapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
PS C:\Users\mypcuser\projects\flask_docker_test>

然后我继续使用以下命令运行我的应用程序:

docker run -p 5001:5000 -t myusername/flaskapp

得到这个输出:

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

但是当我在 Firefox、Google Chrome 和 Postman 中访问这个 URL 时,我得到了这个:

【问题讨论】:

    标签: python docker flask


    【解决方案1】:

    127.0.0.1 指的是本地主机或容器的实际内部。 您需要通过 docker 网络适配器访问它。它通常类似于172.17.0.1,但您的可能会有所不同。

    您可以通过运行以下命令找到 docker 容器的 IP 地址

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <CONTAINER_NAME>

    【讨论】:

    • 好的。我通过写你的命令得到了这个 IP:172.17.0.7。将其输入浏览器时出现超时错误Could not get any response There was an error connecting to http://172.17.0.7. Why this might have happened: The server couldn't send a response: Ensure that the backend is working properly Self-signed SSL certificates are being blocked: Fix this by turning off 'SSL certificate verification' in Settings > General Proxy configured incorrectly Ensure that proxy is configured correctly in Settings > Proxy Request timeout: Change request timeout in Settings > General
    • @bjornsing 您还需要在 Dockerfile EXPOSE 5000 中公开端口 5000,然后当您导航到它时使用 http://172.17.0.7:5001 将容器端口 5000 转发到主机上的 5001 .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2022-01-07
    相关资源
    最近更新 更多