我是docker的新手,我有一个简单的Flask程序,该程序将MongoDB的数据打印到HTML页面。当我使用Python执行应用程序时,它就像一个魅力。但是,使用Docker我得到消息:
pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:27017: [Errno 111] Connection refused
我尝试更改:
db = MongoClient('mongodb:27017').mydatabase
其中之一
db = MongoClient('mongodb://mongodb:27017').mydatabase
db = MongoClient('localhost:27017').mydatabase
db = MongoClient('0.0.0.0:27017').mydatabase
没有任何作用。
我的app.py文件:
from flask import Flask, render_template
import database.config as db_conf
from base64 import b64encode
import datetime
import pandas as pd
from altair import Chart
from pymongo import MongoClient
import os
app = Flask(__name__)
db = MongoClient('mongodb:27017/').mydatabase
@app.route('/', methods=['GET'])
def get_all_images():
pictures = db.pictures
output = []
html_body = "<ul>"
collection = pictures.find()
if collection:
for q in collection:
html_body += " <li><a href="+q['md5']+">"+q['md5']+"</a></li>"
output.append({'md5': q['md5'], 'md5': q['md5'], 'height': q['height'], 'width': q['width'],
'timestamp': q['timestamp']})
html_body += "</ul>"
else:
html_body = "<p>No images on database</p>"
return html_body.format()
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
我的docker-compose.yml文件:
version: '2'
services:
app:
build:
context: ./
dockerfile: ./DockerFile
ports:
- "5000:5000"
links:
- mongo
depends_on:
- mongo
volumes:
- .:/code
mongo:
image: "mongo:latest"
ports:
- "27017:27017"
command: mongod --port 27017 --bind_ip 0.0.0.0
我的Docker文件:
FROM python:3.6
WORKDIR /app
COPY . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
CMD ["python", "app.py"]
我使用
在WINDOWS 10上执行了脚本docker-compose build
docker-compose up
答案 0 :(得分:0)
根据Compose文件,您的Mongo主机称为mongo
,而不是mongodb
。试试
db = MongoClient('mongo:27017/').mydatabase
也许吗?