看不到我在本地主机上运行的Docker容器

时间:2020-03-31 14:03:34

标签: python docker flask

我有一个想要在Docker Image中运行的简单python应用程序。该应用程序看起来像这样

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时,会得到以下信息:

enter image description here

1 个答案:

答案 0 :(得分:2)

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

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

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

相关问题