unable to run docker flask image -pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

Viewed 1774

I have built a docker image for a flask app I have with some html templates and after running my image I go to localhost:5000which takes me to the start page in my flask app . I press a register button to register a user using a flask endpoint but I get

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

Before going to localhost I run my mongodb image with sudo docker start mongodband the connection seems to hit this error whenever I have to search something in my monogdb database for the endpoint . Do I need a docker-compose.yml to connect and I cannot connect without one ?

This is how I connect to mongodb using pymongo

client = MongoClient('mongodb://localhost:27017/')
db = client['MovieFlixDB']

users = db['Users']
movies = db['Movies']

How I run my flask app :

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

I would appreciate your help . Thank you in advance

2 Answers

To connect containers to each other you should use networks.

  1. First you create a network
docker network create my-network
  1. Run mongodb specyfing the network.
docker container run -d --name mongodb -p 27017:27017 --network my-network mongodb:latest
  1. Modify your app to connect to mongodb as host instead of localhost. Containers that are connected to a common network can talk to each other by using their names (DNS names) that can be automatically resolved to container IPs.
client = MongoClient('mongodb://mongodb:27017/')

You could also think about providing such deatils (db host, user, password) through environment variables and read them in your app.

  1. Rebuild image with your app and run it
docker container run --name flask-app -d --network my-network my-flaskapp-image

You can read more about container networking in docker docs.

Do I need a docker-compose.yml to connect and I cannot connect without one ?

If you use docker-compose, it will be easier and don't have to use too many commands to deploy. Look at this example (there are too many however you can refer random service).

Steps -

  1. Build your docker-componse file [I have modified the one in the example of random service, removing rest] e.g.
version: '3.3'
  
services:
    web-random:
        build:
            context: .
            args:
                requirements: ./flask-mongodb-example/requirements.txt
        image: web-random-image
        ports:
            - "800:5000"
        entrypoint: python ./flask-mongodb-example/random_demo.py
        depends_on:
            - mongo

    mongo:
        image: mongo:4.2-bionic
        ports:
            - "27017:27017"
  1. Refer this example to update your mongo URL in your python code

  2. Now, use the following command to compose and bring up the containers

docker-compose build
docker-compose up
  1. Now, either browse your URL with browser or use the curl command
Related