无法将流量引入 docker 容器

Can't get traffic into docker container

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

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

当我尝试从主机系统上的容器外部访问它时,我 运行 tcpdump 发现没有流量进入容器。

docker-compose.yml:

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

Docker 文件:

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 服务器绑定到容器内的本地主机。将 127.0.0.1 更改为 0.0.0.0,这应该可以解决问题。

尝试更改您的 IP 地址

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="5000", debug=True)