docker-compose与来自sql转储的多个postgres数据库

时间:2018-04-29 07:59:51

标签: python postgresql docker docker-compose dockerfile

我知道这可能看起来像是docker-compose with multiple databases的副本,但在完成答案后我仍然无法让它工作。

这是我的docker-compose.yml

version: '3'
services:
  backend:
    image: backend:1.0
    build: ./backend
    ports:
      - "9090:9090"
    depends_on:
      - db
      - ppt
    environment:
      - DATABASE_HOST=db
  db:
    image: main_db:26.03.18
    restart: always
    build: ./db
    ports:
      - "5432:5432"
  ppt:
    image: ppt_generator:1.0
    build: ./ppt
    ports:
      - "6060:6060"
  login:
    image: login:1.0
    build: ./login
    ports:
      - "7070:7070"
    depends_on:
      - login_db
  login_db:
    image: login_db:27.04.2018
    restart: always
    build: ./login_db
    ports:
      - "5433:5433"

请注意,我在端口5433上有一个数据库,在5432上有另一个数据库。但是,当我在启动容器后运行docker ps时,我得到以下内容。我不完全了解端口发生了什么。

CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS                              NAMES
997f816ddff3        backend:1.0           "/bin/sh -c 'pytho..."   About a minute ago   Up About a minute   0.0.0.0:9090->9090/tcp             backendservices_backend_1
759546109a66        login:1.0             "/bin/sh -c 'pytho..."   About a minute ago   Up About a minute   0.0.0.0:7070->7070/tcp, 9090/tcp   backendservices_login_1
a2a26b72dd0c        login_db:27.04.2018   "docker-entrypoint..."   About a minute ago   Up About a minute   5432/tcp, 0.0.0.0:5433->5433/tcp   backendservices_login_db_1
3f97de7fc41e        main_db:26.03.18   "docker-entrypoint..."   About a minute ago   Up About a minute   0.0.0.0:5432->5432/tcp             backendservices_db_1
1a61e741ccba        ppt_generator:1.0     "/bin/sh -c 'pytho..."   About a minute ago   Up About a minute   0.0.0.0:6060->6060/tcp             backendservices_ppt_1

除了我公开的端口号外,我的db dockerfiles基本相同:

FROM postgres:9.6.3
ENV POSTGRES_USER ludo
ENV POSTGRES_PASSWORD password
ENV POSTGRES_DB login
EXPOSE 5433

ADD db_dump.sql /docker-entrypoint-initdb.d

这是我得到的错误:

backend_1   | Traceback (most recent call last):
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
backend_1   |     return fn()
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 387, in connect
backend_1   |     return _ConnectionFairy._checkout(self)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
backend_1   |     fairy = _ConnectionRecord.checkout(pool)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
backend_1   |     rec = pool._do_get()
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
backend_1   |     self._dec_overflow()
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
backend_1   |     compat.reraise(exc_type, exc_value, exc_tb)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
backend_1   |     raise value
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
backend_1   |     return self._create_connection()
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
backend_1   |     return _ConnectionRecord(self)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
backend_1   |     self.__connect(first_connect_check=True)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
backend_1   |     connection = pool._invoke_creator(self)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
backend_1   |     return dialect.connect(*cargs, **cparams)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
backend_1   |     return self.dbapi.connect(*cargs, **cparams)
backend_1   |   File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
backend_1   |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
backend_1   | psycopg2.OperationalError: could not connect to server: Connection refused
backend_1   |   Is the server running on host "localhost" (127.0.0.1) and accepting
backend_1   |   TCP/IP connections on port 5432?
backend_1   | could not connect to server: Cannot assign requested address
backend_1   |   Is the server running on host "localhost" (::1) and accepting
backend_1   |   TCP/IP connections on port 5432?
backend_1   | 

为什么db没有在端口5432上运行?它曾经工作,当我只有一个数据库,现在有两个似乎很困惑......?

更新 我可以在本地端口5432和5433上分别访问数据库。但是,从我的后端容器我不能。我的后端容器似乎没有收到任何在端口5432上运行的东西。如何让db容器端口5432对后端容器可见?

2 个答案:

答案 0 :(得分:2)

<强>更新

经过大量的摆弄,我得到了它的工作。正如@ Iarwa1n建议的那样,您将一个数据库映射为"5432:5432",另一个数据库映射为"5433:5432"。我遇到的错误是由于我从应用程序本身调用postgres。重要的是要认识到postgres主机不再是localhost,而是在docker-compose.yaml中为数据库服务提供的任何名称。就我而言; db的{​​{1}}和backend服务的login_db。此外,我必须将我的驱动程序从login更改为postgresql - 不确定为什么会这样......

因此,我的db_url最终在我的python后端应用程序中看起来像这样:

postgres

以这种方式定义:

postgres://ludo:password@db:5432/main_db

有两点需要注意:

1)无论您如何映射您的端口,您始终必须从应用程序中连接到postgres默认端口5432

2)如果您正在使用请求python库(就像我一样),那么请确保也适当地更改URL。例如,我有一个ppt服务,我通过请求库调用,我不得不将URL更改为: DATABASE_CONFIG = { 'driver': 'postgres', 'host': 'db', 'user': 'ludo', 'password': 'password', 'port': 5432, 'dbname': main_db } db_url = '{driver}://{user}:{password}@{host}:{port}/{dbname}'.format(database_config) 代替'http://ppt:6060/api/getPpt'

答案 1 :(得分:0)

两台机器上的postgres进程都会侦听5432,因为这是默认行为。只是通过EXPOSE,你不要改变它,这只是意味着容器公开了端口5433而不是5432.但是在这个端口上没有进程在监听。 尝试更改以下内容:

FROM postgres:9.6.3
ENV POSTGRES_USER ludo
ENV POSTGRES_PASSWORD password
ENV POSTGRES_DB login
EXPOSE 5432
ADD db_dump.sql /docker-entrypoint-initdb.d

然后像这样更改docker-compose:

login_db:
    image: login_db:27.04.2018
    restart: always
    build: ./login_db
    ports:
      - "5433:5432"

现在您可以从主机访问5432(来自主机)的“db”和5433的“login_db”。请注意,您仍然需要使用5432从另一个容器访问其中一个dbs。