Python-Docker 容器无法连接到 MariaDB-Docker 容器
Python-Docker container cannot connect to MariaDB-Docker container
我正在尝试让我的 docker 化 python 脚本从同样 docker 化的 mariadb 中获取数据。
我知道这应该可以通过网络或链接实现。但是,由于不推荐使用链接(根据 the Docker documentation),我宁愿不使用链接。
docker-compose
:
version: "3.7"
services:
[...]
mariadb:
build: ./db
container_name: maria_db
expose:
- 3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: user
MYSQL_PASSWORD: user
restart: always
networks:
- logrun_to_mariadb
[...]
logrun_engine:
build: ./logrun_engine
container_name: logrun_engine
restart: always
networks:
- logrun_to_mariadb
networks:
logrun_to_mariadb:
external: false
name: logrun_to_mariadb
logrun_engine
容器在启动时执行 python 脚本:
import mysql.connector as mariadb
class DBConnector:
def __init__(self, dbname):
self.mariadb_connection = mariadb.connect(host='mariadb', port='3306', user='root', password='root', database=dbname)
self.cursor = self.mariadb_connection.cursor()
def get_Usecases(self):
self.cursor.execute("SELECT * FROM Test")
tests = []
for test in self.cursor:
print(test)
print("Logrun-Engine running...")
test = DBConnector('test_db')
test.get_Usecases()
每当我 运行 docker-compose up -d
,我的 logrun_engine 日志中充满了错误消息:
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'mariadb' (111)
当我在本地 运行 python 脚本并连接到本地 mariadb 时,它没有问题,因此脚本应该是正确的。
我找到的关于此错误消息的大多数答案是人们使用 localhost
或 127.0.0.1
而不是我已经拥有的 docker 容器。
我尝试了桥接网络、主机网络、链接等,但显然我还没有找到正确的东西。
知道如何连接这两个容器吗?
好的,所以我太不耐烦了,在查询数据库之前没有让 mysql 正常启动,感谢@DanielFarrel 指出这一点。
当我在查询数据库之前在 python 脚本中添加 10 秒的延迟时,它神奇地起作用了...
睡觉也许是一种解决方法。但是,如果 db 上升缓慢,则可能会出现问题。
作为替代方法,您可以使用 agent that will make sure db is up before connecting to it similar to solution here。
运行:
docker-compose up -d agent
代理启动后,您确定数据库已启动,您的应用程序可能 运行:
docker-compose up -d logrun_engine
该解决方案确实使用 --links
,但是可以轻松修改它以使用 docker 网络。
我正在尝试让我的 docker 化 python 脚本从同样 docker 化的 mariadb 中获取数据。
我知道这应该可以通过网络或链接实现。但是,由于不推荐使用链接(根据 the Docker documentation),我宁愿不使用链接。
docker-compose
:
version: "3.7"
services:
[...]
mariadb:
build: ./db
container_name: maria_db
expose:
- 3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: user
MYSQL_PASSWORD: user
restart: always
networks:
- logrun_to_mariadb
[...]
logrun_engine:
build: ./logrun_engine
container_name: logrun_engine
restart: always
networks:
- logrun_to_mariadb
networks:
logrun_to_mariadb:
external: false
name: logrun_to_mariadb
logrun_engine
容器在启动时执行 python 脚本:
import mysql.connector as mariadb
class DBConnector:
def __init__(self, dbname):
self.mariadb_connection = mariadb.connect(host='mariadb', port='3306', user='root', password='root', database=dbname)
self.cursor = self.mariadb_connection.cursor()
def get_Usecases(self):
self.cursor.execute("SELECT * FROM Test")
tests = []
for test in self.cursor:
print(test)
print("Logrun-Engine running...")
test = DBConnector('test_db')
test.get_Usecases()
每当我 运行 docker-compose up -d
,我的 logrun_engine 日志中充满了错误消息:
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'mariadb' (111)
当我在本地 运行 python 脚本并连接到本地 mariadb 时,它没有问题,因此脚本应该是正确的。
我找到的关于此错误消息的大多数答案是人们使用 localhost
或 127.0.0.1
而不是我已经拥有的 docker 容器。
我尝试了桥接网络、主机网络、链接等,但显然我还没有找到正确的东西。
知道如何连接这两个容器吗?
好的,所以我太不耐烦了,在查询数据库之前没有让 mysql 正常启动,感谢@DanielFarrel 指出这一点。 当我在查询数据库之前在 python 脚本中添加 10 秒的延迟时,它神奇地起作用了...
睡觉也许是一种解决方法。但是,如果 db 上升缓慢,则可能会出现问题。
作为替代方法,您可以使用 agent that will make sure db is up before connecting to it similar to solution here。
运行:
docker-compose up -d agent
代理启动后,您确定数据库已启动,您的应用程序可能 运行:
docker-compose up -d logrun_engine
该解决方案确实使用 --links
,但是可以轻松修改它以使用 docker 网络。