使用Docker Compose运行时的端口发布 [英] Port Publishing When Running with Docker Compose
问题描述
我似乎无法找到一种方法来使端口发布能够与docker run
一样使用.
I can't seem to work out a way to get port publishing to work with docker-compose run
in the same way as I can with docker run
.
使用Docker Compose(以及docker-compose.yml
中的端口映射)从curl
发出连接失败"错误:
Using Docker Compose (and therefore the port mapping in docker-compose.yml
) gives a "Failed to connect" error from 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
时一切正常:
However, things are fine when manually passing the ports to 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!
我想念什么?
Dockerfile
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)
docker-compose.yml
version: '2'
services:
flask:
build: .
ports:
- "2048:2048"
推荐答案
默认情况下,docker-compose run
不发布服务的端口.您可以通过--service-ports
选项来发布docker-compose.yml中定义的端口,也可以使用-p
选项来发布所有端口.
By default, docker-compose run
does not publish the service's ports. You can either pass the --service-ports
option to publish the ports as they are defined in the docker-compose.yml, or use the -p
option to publish all ports.
这篇关于使用Docker Compose运行时的端口发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!