问题:无法使用 docker-compose 连接到 MongoDB

我在使用 docker 时遇到问题。码头工人正在运行,主页正在运行。

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

但是当我尝试访问一些有 MongoDB 应用程序的页面时,它会显示以下内容:

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

MongoDB 正在我的机器上运行,我可以使用 POSTMAN 访问。

这是requirement.txtdocker-compose.ymlDockerfile配置:

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

我正在使用以下方式构建和运行:

$ docker-compose build
$ docker-compose up

这是我得到的:

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 -

解答

pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] 连接被拒绝

好像您正在尝试与web容器的localhost连接。

将应用程序中的连接字符串更新为

client = MongoClient('db', 27017)

此外,删除linking,docker-compose 创建默认网络,然后您可以引用任何具有该名称的容器。

当 docker-compose 执行 V2 文件时,它会自动在文件中定义的所有容器之间建立网络,每个容器都可以立即使用 docker-compose.yml 文件中定义的名称来引用其他容器.

你需要添加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

取决于

服务之间的表达依赖,服务依赖会导致以下行为:

docker-compose up 按依赖顺序启动服务。在以下示例中,db 和 redis 在 web 之前启动。

撰写文件依赖_on

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐