我似乎无法找到一种方法来docker-compose run像我一样使用port发布docker run。
docker-compose run
docker run
使用Docker Compose(以及中的端口映射docker-compose.yml)会产生来自的“无法连接”错误curl:
docker-compose.yml
curl
$ docker-compose run flask * Running on http://0.0.0.0:2048/ (Press CTRL+C to quit) $ curl http://localhost:2048/ curl: (7) Failed connect to localhost:2048; Connection refused
但是,手动将端口传递给时,情况就很好了docker run:
$ docker run -p 2048:2048 --name flask -t flask_image * Running on http://0.0.0.0:2048/ (Press CTRL+C to quit) $ curl http://localhost:2048 Hello World!
我想念什么?
Docker文件
FROM centos:7 # Install EPEL repo. RUN rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm # Install Python and Pip. RUN yum -y update && yum -y install \ python \ python-pip # Flask is necessary to run the app. RUN pip install flask EXPOSE 2048 ADD hello_world_flask_app.py /src/hello_world_flask_app.py CMD ["python", "/src/hello_world_flask_app.py"]
hello_world_flask_app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run(host='0.0.0.0', port=2048)
version: '2' services: flask: build: . ports: - "2048:2048"
默认情况下,docker-compose run不发布服务的端口。您可以传递--service-ports选项以按docker- compose.yml中的定义发布端口,也可以使用该-p选项发布所有端口。
--service-ports
-p
请参阅docker-compose run的文档