6

I'm having problems with docker. Litteraly the docker is running, and the home page is working.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
c459b9b1b603        application_web     "python -u app.py"       41 seconds ago      Up 39 seconds       0.0.0.0:5000->5000/tcp   application_web_1
bc51d72486b0        mongo:3.0.2         "/entrypoint.sh mong…"   42 seconds ago      Up 40 seconds       27017/tcp                application_db_1

But when I try to access to some pages where there are MongoDB apps, it shows the following:

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

The MongoDB is running on my machine, I could access using POSTMAN.

Here's the requirement.txt , docker-compose.yml and Dockerfile configuration:

Dockerfile

FROM python:3.6.1-alpine
WORKDIR /project
ADD . /project
RUN pip install -r requirements.txt
CMD ["python","app.py"]

docker-compose.yml

web:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/project
  links:
    - db
db:
  image: mongo:3.0.2

requirements.txt

Flask==1.1.1
Bootstrap-Flask==1.1.0
pymongo==3.9.0

Im building and running using:

$ docker-compose build
$ docker-compose up

Here's what I got:

Status: Downloaded newer image for mongo:3.0.2
Creating application_db_1 ... done
Creating application_web_1 ... done
Attaching to application_db_1, application_web_1
db_1   | 2019-10-04T09:57:45.080+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
db_1   | 2019-10-04T09:57:45.081+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
db_1   | 2019-10-04T09:57:45.213+0000 I JOURNAL  [durability] Durability thread started
db_1   | 2019-10-04T09:57:45.213+0000 I JOURNAL  [journal writer] Journal writer thread started
db_1   | 2019-10-04T09:57:45.215+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=bc51d72486b0
db_1   | 2019-10-04T09:57:45.216+0000 I CONTROL  [initandlisten] db version v3.0.2
db_1   | 2019-10-04T09:57:45.216+0000 I CONTROL  [initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f
db_1   | 2019-10-04T09:57:45.216+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
db_1   | 2019-10-04T09:57:45.216+0000 I CONTROL  [initandlisten] build info: Linux ip-10-171-120-232 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
db_1   | 2019-10-04T09:57:45.216+0000 I CONTROL  [initandlisten] allocator: tcmalloc
db_1   | 2019-10-04T09:57:45.216+0000 I CONTROL  [initandlisten] options: {}
db_1   | 2019-10-04T09:57:45.216+0000 I INDEX    [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
db_1   | 2019-10-04T09:57:45.277+0000 I STORAGE  [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
db_1   | 2019-10-04T09:57:45.277+0000 I STORAGE  [FileAllocator] creating directory /data/db/_tmp
db_1   | 2019-10-04T09:57:45.280+0000 I STORAGE  [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0.001 secs
db_1   | 2019-10-04T09:57:45.285+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
web_1  |  * Serving Flask app "app" (lazy loading)
web_1  |  * Environment: production
web_1  |    WARNING: This is a development server. Do not use it in a production deployment.
web_1  |    Use a production WSGI server instead.
web_1  |  * Debug mode: on
web_1  |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1  |  * Restarting with stat
web_1  |  * Debugger is active!
web_1  |  * Debugger PIN: 328-327-883
web_1  | 172.17.0.1 - - [04/Oct/2019 09:57:59] "GET / HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:00] "GET /favicon.ico HTTP/1.1" 404 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:31] "GET /objects HTTP/1.1" 500 -
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
web_1  |     return self.wsgi_app(environ, start_response)
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
web_1  |     response = self.handle_exception(e)
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
web_1  |     reraise(exc_type, exc_value, tb)
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
web_1  |     raise value
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
web_1  |     response = self.full_dispatch_request()
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
web_1  |     rv = self.handle_user_exception(e)
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
web_1  |     reraise(exc_type, exc_value, tb)
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
web_1  |     raise value
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
web_1  |     rv = self.dispatch_request()
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
web_1  |     return self.view_functions[rule.endpoint](**req.view_args)
web_1  |   File "/project/app.py", line 24, in list_objects
web_1  |     return render_template('objects.html', rows=object_list)
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/templating.py", line 140, in render_template
web_1  |     ctx.app,
web_1  |   File "/usr/local/lib/python3.6/site-packages/flask/templating.py", line 120, in _render
web_1  |     rv = template.render(context)
web_1  |   File "/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
web_1  |     return original_render(self, *args, **kwargs)
web_1  |   File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
web_1  |     return self.environment.handle_exception(exc_info, True)
web_1  |   File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
web_1  |     reraise(exc_type, exc_value, tb)
web_1  |   File "/usr/local/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
web_1  |     raise value.with_traceback(tb)
web_1  |   File "/project/templates/objects.html", line 8, in top-level template code
web_1  |     {% extends "template.html" %}
web_1  |   File "/project/templates/template.html", line 46, in top-level template code
web_1  |     {% block content %}
web_1  |   File "/project/templates/objects.html", line 28, in block "content"
web_1  |     {% for row in rows %}
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/cursor.py", line 1156, in next
web_1  |     if len(self.__data) or self._refresh():
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/cursor.py", line 1050, in _refresh
web_1  |     self.__session = self.__collection.database.client._ensure_session()
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1807, in _ensure_session
web_1  |     return self.__start_session(True, causal_consistency=False)
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1760, in __start_session
web_1  |     server_session = self._get_server_session()
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1793, in _get_server_session
web_1  |     return self._topology.get_server_session()
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/topology.py", line 477, in get_server_session
web_1  |     None)
web_1  |   File "/usr/local/lib/python3.6/site-packages/pymongo/topology.py", line 205, in _select_servers_loop
web_1  |     self._error_message(selector))
web_1  | pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:31] "GET /objects?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:31] "GET /objects?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:31] "GET /objects?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:32] "GET /objects?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:32] "GET /objects?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 09:58:32] "GET /objects?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
web_1  | 172.17.0.1 - - [04/Oct/2019 10:00:39] "GET / HTTP/1.0" 200 -
2
  • Configure your database host to the container name. Commented Oct 4, 2019 at 10:07
  • @KlausD. Sorry, I couldn't get it correctly? Commented Oct 4, 2019 at 10:09

1 Answer 1

7

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

seems like you are trying to connect with localhost of the web container.

Update you connection string in your application to

client = MongoClient('db', 27017)

Also, remove the linking, docker-compose create default network and you can then refer to any container with the name.

When docker-compose executes V2 files, it will automatically build a network between all of the containers defined in the file, and every container will be immediately able to refer to the others just using the names defined in the docker-compose.yml file.

You need to add depends_on:

web:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  depends_on:
    - db
  volumes:
    - .:/project
  links:
    - db
db:
  image: mongo:3.0.2

depends_on

Express dependency between services, Service dependencies cause the following behaviors:

docker-compose up starts services in dependency order. In the following example, db and redis are started before web.

compose-file depend_on

Sign up to request clarification or add additional context in comments.

2 Comments

Oh yeah! I totally forgot linking :D Thanks a lot :)
I am getting the exact same error despite providing the correct container name as specified in the docker-compose file. Upon inspection, it turned out that somehow my folder name is prepended to all the containers spawned by docker-compose. Because of that my app never connects to the database. Without hardcoding, what is the best way to make sure that it works with this scenario?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.