如何使用docker-compose从其他容器连接到cockroachdb容器?

时间:2019-11-06 03:37:50

标签: docker-compose cockroachdb

我已经尝试模仿[MySQL方式] [1]来做到这一点,但对我没有用。 我还尝试了多种变体,范围从:添加网络接口到 明确指定容器IP,但它们都不起作用(因为容器IP 总是改变)。

错误消息是:

"could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket \"/tmp/.s.PGSQL.5432\"
}

这些是我的代码:

我的烧瓶应用程序:

connection = psycopg2.connect(
    database=os.environ.get("DB_NAME"),
    user=os.environ.get("DB_USER"),
    password=os.environ.get("DB_PASSWORD"),
    sslmode=os.environ.get("DB_SSL"),
    host=os.environ.get("DB_HOST"),
    port=os.environ.get("DB_PORT"),
)

我的docker-compose文件:

version: '3'
services:
    flask-api:
        image: flask-api:0.7.0
        ports:
            - '5000:5000'
        environment:
            - DB_NAME = knotdb
            - DB_HOST = roach1
            - DB_PORT = 26257
            - DB_USER = root
            - DB_SSL = disable
        links:
            - roach1
    roach1:
        image: cockroachdb/cockroach:v1.1.3
        command: start --insecure --host=127.0.0.1
        ports:
          - "26257:26257"
        volumes:
          - ./cockroach-data/roach1:/cockroach/cockroach-data

我其他各种尝试:

# adding network to both service : didn't work

# using ip instead of alias: didn't work
links:
  - roach1:127.0.0.1

1:How to handle IP addresses when linking docker containers with each other using docker-compose?

0 个答案:

没有答案