使用 Docker Compose 运行时的端口发布 [英] Port Publishing When Running with Docker Compose
问题描述
我似乎无法找到一种方法让端口发布与 docker-compose run
一起使用,就像我使用 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屋!