我正试图在http://localhost:5000的docker中运行一个webserver,我所读到的每一篇文章都说要在我的dockerfile中添加“暴露5000”,并将端口添加到我的docker-组合文件中。
我知道and服务器正在运行,因为我可以在容器内使用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)
如何从主机容器外部到达How服务器?
我应该补充一下,示例( https://docs.docker.com/compose/gettingstarted/#step-2-create-a-dockerfile )可以在我的计算机上工作,所以我不认为这是我的Windows 10主机的配置问题。
发布于 2018-09-14 09:51:47
您正在将您的烧瓶服务器绑定到容器内的本地主机。将127.0.0.1改为0.0.0.0,这将修复一些问题。
发布于 2018-09-14 09:53:48
试着改变你的IP地址
if __name__ == "__main__":
app.run(host="0.0.0.0", port="5000", debug=True)
https://stackoverflow.com/questions/52336837
复制