以下是我的Dockerfile:
构建标签并通过
FROM alpine:3.5
RUN apk add --no-cache python3 && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
rm -r /root/.cache
# Copy files
COPY ./requirements.txt /app/requirements.txt
COPY ./main.py /app/main.py
# Install deps
RUN pip install --upgrade pip
RUN pip install --requirement /app/requirements.txt
# Set the default directory where CMD will execute
WORKDIR /app
EXPOSE 5000
CMD [ "python3", "-u", "./main.py" ]
and my main.py
file:
from flask im
port Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
构建标签并通过
docker run <tag>
运行后,获取日志记录,表示 Flask 服务器已成功启动:$ docker run test:latest
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
但是我无法在浏览器中连接服务器。
我认为问题可能出在端口映射上,所以我按照以下方式运行了容器:
$ docker run -p 5000:5000 test:latest
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
还是不行。在容器外,我可以通过运行python3 main.py
来运行服务器。我做错了什么?
127.0.0.1
是环回IP...这个不应该可以访问吗?0.0.0.0
不是实际上相同的IP地址吗? - dopatraman