gpt4 book ai didi

docker - 无法让流量进入 docker 容器

转载 作者:行者123 更新时间:2023-12-02 20:46:34 26 4
gpt4 key购买 nike

我正在尝试在 http://localhost:5000 的 docker 中运行网络服务器并且我读过的所有内容都说将“EXPOSE 5000”添加到我的 dockerfile 并将端口添加到我的 docker-compose 文件中。

我知道网络服务器正在运行,因为我可以在容器内使用 lynx 进行连接并转到 http://localhost:5000 .在容器内,一切正常。

当我尝试从主机系统上的容器外部访问它时,我运行了 tcpdump 并没有看到任何流量进入容器。

docker -compose.yml:

version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- ./code:/code

Dockerfile:
FROM scratch
ADD centos-7-docker.tar.xz /

LABEL org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20180804"

RUN yum clean all
RUN yum -y update

RUN yum install -y iputils gcc vim wget yum-utils groupinstall development lynx

#install Python 3.6
RUN yum install https://centos7.iuscommunity.org/ius-release.rpm -y
RUN yum install python36u -y
RUN yum install python36u-pip python36u-devel -y
RUN pip3.6 install --upgrade pip
#now you can run python as "python3.6 some_file.py" and pip as "pip3.6 <stuff>"


#install ms sql odbc driver for connecting to SQL Server
RUN curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
RUN ACCEPT_EULA=Y yum install msodbcsql17 -y
# optional: for bcp and sqlcmd in /opt/mssql-tools/bin
RUN ACCEPT_EULA=Y yum install mssql-tools -y
# optional: for unixODBC development headers
RUN yum install unixODBC-devel -y

#install python's odbc driver
RUN yum install gcc-c++ -y
RUN pip3.6 install pyodbc

#mount volumes
ADD . /code
WORKDIR /code
EXPOSE 5000

#install Flask and other dependencies (must come after "/code" dir created)
RUN pip3.6 install -r /code/requirements.txt

#execute file
CMD python3.6 /code/app.py

我正在尝试运行的 app.py:
import time
#import redis
import pyodbc
from flask import Flask

app = Flask(__name__)
#cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)

@app.route('/')
def hello():
#count = get_hit_count()
server = '123.123.123.123' #I changed these for posting to SO
username = 'usernameForMyApplication'
password = 'passwordForMyApplication'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';PORT=1443;UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
print ('Using the following SQL Server version:')
tsql = "SELECT @@version;"
with cursor.execute(tsql):
row = cursor.fetchone()
version = (str(row[0]))

return 'version {} \n'.format(version)

if __name__ == "__main__":
app.run(host="127.0.0.1", debug=True)

如何从主机容器外部访问网络服务器?

我应该补充一点,示例 ( https://docs.docker.com/compose/gettingstarted/#step-2-create-a-dockerfile) 在我的计算机上运行,​​所以我认为这不是我的 Windows 10 主机的配置问题。

最佳答案

您正在将 flask 服务器绑定(bind)到容器内的 localhost。将 127.0.0.1 更改为 0.0.0.0 应该可以解决问题。

关于docker - 无法让流量进入 docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com