Flask with MongoDB Docker 图片无法连接
Flask with MongoDB Docker Image unable to connect
我收到服务器超时错误,所以我假设我的 MongoDB 和 flask 应用程序存在连接错误。
在docker-compose.yml
mongodb:
image: mongo:latest
hostname: test_mongodb
environment:
- MONGO_INITDB_DATABASE=staff_db
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
ports:
- 27017:27017
app.py
def get_db():
client = MongoClient(host='test_mongodb',
port=27017,
username='admin',
password='password',
authSource='admin')
db = client["staff_db"]
print("connection good")
return db
@app.route('/location')
def get_staff_location():
db = ""
staffs = []
try:
db = get_db()
_staffs = db.beaconLocation_tb.find()
for staff in _staffs:
temp_dict = {
"id": staffLocation["id"],
"staffid": staffLocation["staffid"],
"name": staffLocation["name"]
}
staffs.append(temp_dict)
return jsonify({'staff': staffs})
except:
pass
finally:
if type(db) == MongoClient:
db.close()
init-db.js
db = db.getSiblingDB("staff_db");
db.staff_tb.drop();
db.staff_tb.insertMany([
{
"id":1,
"staffid": 1,
"name": "Darren"
},
{
"id": 2,
"staffid": 2,
"name": "Tony"
},
]);
错误信息
pymongo.errors.ServerSelectionTimeoutError: test_mongodb:27017: [Errno
-3] Temporary failure in name resolution, Timeout: 30s, Topology Description: <TopologyDescription id: 6194ffbbbce36c5d06b983f6,
topology_type: Single, servers: [<ServerDescription ('test_mongodb',
27017) server_type: Unknown, rtt: None,
error=AutoReconnect('ict3102_mongodb:27017: [Errno -3] Temporary
failure in name resolution')>]>
有人能看到我犯的错误吗?
hostname:
在 Compose 设置中几乎什么都不做。如果您 docker exec
在容器中获取 shell 并且 shell 提示显示本地主机名,该设置将更改显示在那里的主机名,但这几乎是唯一的它将产生的影响。您通常不需要设置此选项。
反之,Compose服务名可以作为容器间通信的主机名。此设置在 Docker 文档的 Networking in Compose 中有进一步描述。
mongodb: # <-- this is a valid host name
image: mongo:latest
# hostname: test_mongodb # <-- does nothing, remove it
ports: # <-- optional and ignored for connections
- 27017:27017 # between containers; always use 27017
我建议让您的数据库连接信息可通过环境变量进行配置。请注意,如果您从容器或 Docker.
之外的开发环境连接,则需要连接到此 MongoDB 实例的主机名会有所不同
# If the environment variable is unset (you're running it from a
# non-Docker virtual environment) use a reasonable default for a
# local developer
client = MongoClient(os.environ.get('MONGODB_HOST', 'localhost'), ...)
version: '3.8'
services:
application:
build: .
environment:
MONGODB_HOST: mongodb # its Compose service name, not its hostname:
我收到服务器超时错误,所以我假设我的 MongoDB 和 flask 应用程序存在连接错误。
在docker-compose.yml
mongodb:
image: mongo:latest
hostname: test_mongodb
environment:
- MONGO_INITDB_DATABASE=staff_db
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
ports:
- 27017:27017
app.py
def get_db():
client = MongoClient(host='test_mongodb',
port=27017,
username='admin',
password='password',
authSource='admin')
db = client["staff_db"]
print("connection good")
return db
@app.route('/location')
def get_staff_location():
db = ""
staffs = []
try:
db = get_db()
_staffs = db.beaconLocation_tb.find()
for staff in _staffs:
temp_dict = {
"id": staffLocation["id"],
"staffid": staffLocation["staffid"],
"name": staffLocation["name"]
}
staffs.append(temp_dict)
return jsonify({'staff': staffs})
except:
pass
finally:
if type(db) == MongoClient:
db.close()
init-db.js
db = db.getSiblingDB("staff_db");
db.staff_tb.drop();
db.staff_tb.insertMany([
{
"id":1,
"staffid": 1,
"name": "Darren"
},
{
"id": 2,
"staffid": 2,
"name": "Tony"
},
]);
错误信息
pymongo.errors.ServerSelectionTimeoutError: test_mongodb:27017: [Errno -3] Temporary failure in name resolution, Timeout: 30s, Topology Description: <TopologyDescription id: 6194ffbbbce36c5d06b983f6, topology_type: Single, servers: [<ServerDescription ('test_mongodb', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ict3102_mongodb:27017: [Errno -3] Temporary failure in name resolution')>]>
有人能看到我犯的错误吗?
hostname:
在 Compose 设置中几乎什么都不做。如果您 docker exec
在容器中获取 shell 并且 shell 提示显示本地主机名,该设置将更改显示在那里的主机名,但这几乎是唯一的它将产生的影响。您通常不需要设置此选项。
反之,Compose服务名可以作为容器间通信的主机名。此设置在 Docker 文档的 Networking in Compose 中有进一步描述。
mongodb: # <-- this is a valid host name
image: mongo:latest
# hostname: test_mongodb # <-- does nothing, remove it
ports: # <-- optional and ignored for connections
- 27017:27017 # between containers; always use 27017
我建议让您的数据库连接信息可通过环境变量进行配置。请注意,如果您从容器或 Docker.
之外的开发环境连接,则需要连接到此 MongoDB 实例的主机名会有所不同# If the environment variable is unset (you're running it from a
# non-Docker virtual environment) use a reasonable default for a
# local developer
client = MongoClient(os.environ.get('MONGODB_HOST', 'localhost'), ...)
version: '3.8'
services:
application:
build: .
environment:
MONGODB_HOST: mongodb # its Compose service name, not its hostname: