How to use docker-compose to setup a mongodb with pymonogo app?

I am trying to spin up a python application with mongodb server using docker-composer, but I get the following error:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

I really don't understand what am I missing in my setup:

version: '3.6'
services:
  web:
    build: .
    ports:
     - "5000:5000"
     - "9000:9000"
    links:
      - db
  db:
    image: mongo:latest
    container_name: "mongodb"
    ports:
        - 27017:27017
    command: mongod --smallfiles # --quiet    

  redis:
    image: "redis:alpine"

I am not sure why I can't connect to the server which seems to be up (docker ps):

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
fb5a00bb8867        composedir_web      "python app.py"          20 seconds ago      Up 19 seconds       0.0.0.0:5000->5000/tcp, 0.0.0.0:9000->9000/tcp   composedir_web_1
56aeae245ad5        mongo:latest        "docker-entrypoint.s…"   14 minutes ago      Up 20 seconds       0.0.0.0:27017->27017/tcp                         mongodb
51c64650bab8        redis:alpine        "docker-entrypoint.s…"   14 minutes ago      Up 21 seconds       6379/tcp                                         composedir_red

1 answer

  • answered 2018-05-16 06:21 abinet

    You need to configure the python application to connect mongo on db:27017 instead of localhost:27017