【问题标题】:Can't get traffic into docker container无法将流量引入 docker 容器
【发布时间】:2018-09-15 01:53:48
【问题描述】:

我正在尝试在 http://localhost:5000 的 docker 中运行一个网络服务器,我读过的所有内容都说要在我的 dockerfile 和我的 docker-compose 文件中添加“EXPOSE 5000”。

我知道网络服务器正在运行,因为我可以使用容器内的 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 主机的配置问题。

【问题讨论】:

    标签: docker docker-compose dockerfile centos7


    【解决方案1】:

    您正在将烧瓶服务器绑定到容器内的本地主机。将 127.0.0.1 更改为 0.0.0.0 应该可以解决问题。

    【讨论】:

      【解决方案2】:

      尝试更改您的 IP 地址

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

      【讨论】:

        猜你喜欢
        • 2021-09-01
        • 1970-01-01
        • 2021-05-10
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-23
        相关资源
        最近更新 更多