docker-compose 容器端口未显示在本地主机上
docker-compose container port not showing up on localhost
我正在尝试 运行 一个 docker-compose 具有两项服务的应用程序。一个用于构建 Web 服务器,另一个用于 运行 测试。
docker-compose.yml
version: "3.7"
services:
web:
build: .
ports:
- "127.0.0.1:5000:5000"
expose:
- 5000
test:
# expose:
# - 5000
depends_on:
- web
build: test_python/.
./Dockerfile
FROM python:buster
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
# Add .cargo/bin to PATH
ENV PATH="/root/.cargo/bin:${PATH}"
# Check cargo is visible
RUN cargo --help
WORKDIR /code
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 5000
COPY test_python .
CMD [ "python3", "base_routes.py" ]
test_python/Dockerfile
FROM python:buster
RUN pip3 install pytest requests
COPY . .
base_routes.py
from robyn import Robyn, static_file, jsonify
import asyncio
app = Robyn(__file__)
callCount = 0
@app.get("/")
async def h(request):
print(request)
global callCount
callCount += 1
message = "Called " + str(callCount) + " times"
return message
@app.get("/test")
async def test(request):
import os
path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_python/index.html"))
return static_file(path)
@app.get("/jsonify")
async def json_get(request):
return jsonify({"hello": "world"})
@app.post("/jsonify")
async def json(request):
print(request)
return jsonify({"hello": "world"})
@app.post("/post")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
@app.put("/put")
async def putreq(request):
return bytearray(request["body"]).decode("utf-8")
@app.delete("/delete")
async def deletereq(request):
return bytearray(request["body"]).decode("utf-8")
@app.patch("/patch")
async def patchreq(request):
return bytearray(request["body"]).decode("utf-8")
@app.get("/sleep")
async def sleeper():
await asyncio.sleep(5)
return "sleep function"
@app.get("/blocker")
def blocker():
import time
time.sleep(10)
return "blocker function"
if __name__ == "__main__":
app.add_header("server", "robyn")
app.add_directory(route="/test_dir",directory_path="./test_dir/build", index_file="index.html")
app.start(port=5000)
这些是我在项目中使用的文件。当我尝试从我的机器打开 127.0.0.1:5000
时,它什么也没显示。但是,当我登录 web
容器并执行 curl http://localhost:5000/
时,它给出了正确的响应。
我不知道如何在主机上访问它?
我必须让 python 服务器监听“0.0.0.0”。
我在代码库中添加了以下行
app.start(port=5000, url='0.0.0.0')
我正在尝试 运行 一个 docker-compose 具有两项服务的应用程序。一个用于构建 Web 服务器,另一个用于 运行 测试。
docker-compose.yml
version: "3.7"
services:
web:
build: .
ports:
- "127.0.0.1:5000:5000"
expose:
- 5000
test:
# expose:
# - 5000
depends_on:
- web
build: test_python/.
./Dockerfile
FROM python:buster
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
# Add .cargo/bin to PATH
ENV PATH="/root/.cargo/bin:${PATH}"
# Check cargo is visible
RUN cargo --help
WORKDIR /code
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 5000
COPY test_python .
CMD [ "python3", "base_routes.py" ]
test_python/Dockerfile
FROM python:buster
RUN pip3 install pytest requests
COPY . .
base_routes.py
from robyn import Robyn, static_file, jsonify
import asyncio
app = Robyn(__file__)
callCount = 0
@app.get("/")
async def h(request):
print(request)
global callCount
callCount += 1
message = "Called " + str(callCount) + " times"
return message
@app.get("/test")
async def test(request):
import os
path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_python/index.html"))
return static_file(path)
@app.get("/jsonify")
async def json_get(request):
return jsonify({"hello": "world"})
@app.post("/jsonify")
async def json(request):
print(request)
return jsonify({"hello": "world"})
@app.post("/post")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
@app.put("/put")
async def putreq(request):
return bytearray(request["body"]).decode("utf-8")
@app.delete("/delete")
async def deletereq(request):
return bytearray(request["body"]).decode("utf-8")
@app.patch("/patch")
async def patchreq(request):
return bytearray(request["body"]).decode("utf-8")
@app.get("/sleep")
async def sleeper():
await asyncio.sleep(5)
return "sleep function"
@app.get("/blocker")
def blocker():
import time
time.sleep(10)
return "blocker function"
if __name__ == "__main__":
app.add_header("server", "robyn")
app.add_directory(route="/test_dir",directory_path="./test_dir/build", index_file="index.html")
app.start(port=5000)
这些是我在项目中使用的文件。当我尝试从我的机器打开 127.0.0.1:5000
时,它什么也没显示。但是,当我登录 web
容器并执行 curl http://localhost:5000/
时,它给出了正确的响应。
我不知道如何在主机上访问它?
我必须让 python 服务器监听“0.0.0.0”。
我在代码库中添加了以下行
app.start(port=5000, url='0.0.0.0')