1
votes

I am using Flask and MongoDB inside docker. My Log shows everything fine but I can not GET and POST data, Even localhost:5000 does not work.

Here is my Docker-compose file

version: '3.4'
services:
  flask_example:
    build: .
    ports:
      - '5000:5000'
    links:
      - mongodb
  mongodb:
    image: mongo:latest

Here in my Flask App

app.config['MONGO_DBNAME'] = 'restdb'
app.config['MONGO_URI'] = 'mongodb://mongodb:27017/restdb'
mongo = PyMongo(app)

I am getting the Log as follows :

mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/ data/db 64-bit host=64a180c6c04e mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] db version v3.6.5 mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] git version: a20ecd3e3a174162052ff99913bc2ca 9a839d618 mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016 mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] allocator: tcmalloc mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] modules: none mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] build environment: mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] distmod: debian81 mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] distarch: x86_64 mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] target_arch: x86_64 mongodb_1 | 2018-06-26T15:06:45.689+0000 I CONTROL [initandlisten] options: { net: { bindIpAll: true } } mongodb_1 | 2018-06-26T15:06:45.690+0000 I STORAGE [initandlisten] mongodb_1
| 2018-06-26T15:06:45.690+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is stro ngly recommended with the WiredTiger storage engine mongodb_1 | 2018-06-26T15:06:45.690+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/co re/prodnotes-filesystem mongodb_1 | 2018-06-26T15:06:45.690+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=47 8M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),cache_cursors=false,log =(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait= 0),verbose=(recovery_progress), mongodb_1 | 2018-06-26T15:06:46.604+0000 I STORAGE [initandlisten] WiredTiger message [1530025606:604056][1:0x7 f9e1d704a00], txn-recover: Set global recovery timestamp: 0 mongodb_1 | 2018-06-26T15:06:46.662+0000 I CONTROL [initandlisten] mongodb_1
| 2018-06-26T15:06:46.662+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled fo r the database. mongodb_1 | 2018-06-26T15:06:46.662+0000 I CONTROL [initandlisten] **
Read and write access to data an d configuration is unrestricted. mongodb_1 | 2018-06-26T15:06:46.662+0000 I CONTROL [initandlisten] mongodb_1 | 2018-06-26T15:06:46.663+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: cdebc7d3-958e-43db-b517-bcfdc59e92ce mongodb_1 | 2018-06-26T15:06:46.692+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.6 mongodb_1 | 2018-06-26T15:06:46.696+0000 I STORAGE [initandlisten] createCollection: local.startup_log with gen erated UUID: 5b6bb532-fde4-4156-bee9-e4bfa0ae55b7 mongodb_1 | 2018-06-26T15:06:46.731+0000 I FTDC [initandlisten] Initializing full-time diagnostic data captu re with directory '/data/db/diagnostic.data' mongodb_1 | 2018-06-26T15:06:46.731+0000 I NETWORK [initandlisten] waiting for connections on port 27017 mongodb_1 | 2018-06-26T15:06:47.028+0000 I NETWORK [listener] connection accepted from 172.28.0.3:46594 #1 (1 c onnection now open) flask_example_1 | * Serving Flask app "app" (lazy loading) flask_example_1 | * Environment: production flask_example_1 | WARNING: Do not use the development server in a production environment. mongodb_1 | 2018-06-26T15:06:47.030+0000 I NETWORK [conn1] received client metadata from 172.28.0.3:46594 conn1 : { driver: { name: "PyMongo", version: "3.6.1" }, os: { type: "Linux", name: "Linux", architecture: "x86_64", version: "4.9.87-linuxkit-aufs" }, platform: "CPython 3.6.5.final.0" } flask_example_1 | Use a production WSGI server instead. flask_example_1 | * Debug mode: on flask_example_1 | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) flask_example_1 | * Restarting with stat mongodb_1 | 2018-06-26T15:06:47.283+0000 I NETWORK [listener] connection accepted from 172.28.0.3:46596 #2 (2 c onnections now open) mongodb_1 | 2018-06-26T15:06:47.285+0000 I NETWORK [conn2] received client metadata from 172.28.0.3:46596 conn2 : { driver: { name: "PyMongo", version: "3.6.1" }, os: { type: "Linux", name: "Linux", architecture: "x86_64", version: "4.9.87-linuxkit-aufs" }, platform: "CPython 3.6.5.final.0" } flask_example_1 | * Debugger is active! flask_example_1 | * Debugger PIN: 174-151-610

2

2 Answers

1
votes

Solved it

In production / docker it has to be as follows

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)

therefore no localhost:5000, rather 0.0.0.0:5000

0
votes

What I see here is Flask and mongo served on separate containers. I think you should fix the links section with hostname mapping:

links:
  - mongodb:mongodb