简体   繁体   English

从主机访问 mariadb docker-compose 容器

[英]Access mariadb docker-compose container from host-machine

I'm trying to access a mariadb-container from a python script on my host-machine (MacOS).我正在尝试从我的主机(MacOS)上的 python 脚本访问 mariadb 容器。 I tried all network_modes (host, bridge, default), but nothing works.我尝试了所有网络模式(主机、桥接、默认),但没有任何效果。 I was able to connect to the container through phpmyadmin, but only if both containers are in the same docker-compose-network.我能够通过 phpmyadmin 连接到容器,但前提是两个容器都在同一个 docker-compose-network 中。

Here is my docker-compose.yml with the attempt on network_mode host:这是我在 network_mode 主机上尝试的docker-compose.yml

version: '3.9'

services:
  mariadb:
    image: mariadb:10.9.1-rc
    container_name: mariadb 
    network_mode: bridge
    ports:
      - 3306:3306
    volumes:
      - ...
    environment:
      - MYSQL_ROOT_PASSWORD=mysqlroot
      - MYSQL_PASSWORD=mysqlpw
      - MYSQL_USER=test
      - MYSQL_DATABASE=test1
      - TZ=Europe/Berlin

  phpmyadmin:
    image: phpmyadmin:5.2.0
    network_mode: bridge
    container_name: pma
    # links:
    #   - mariadb
    environment:
      - PMA_HOST=mariadb
      - PMA_PORT=3306
      - TZ=Europe/Berlin
    ports:
      - 8081:80

Any tips on how I get access to the container through the python mariadb package?关于如何通过 python mariadb package 访问容器的任何提示?

Thanks!谢谢!

Every thing seems okay, just check the params when trying to connect to the db:一切似乎都很好,只需在尝试连接到数据库时检查参数:

  • host: 0.0.0.0主机:0.0.0.0
  • port: 3306 (as in the docker-compose)端口:3306(在 docker-compose 中)
  • user: test (as in the docker-compose)用户:测试(如在 docker-compose 中)
  • password: mysqlpw (as in the docker-compose)密码:mysqlpw(在 docker-compose 中)
  • database: test1 (as in the docker-compose)数据库:test1(如在 docker-compose 中)

example: db = MySQLdb.connect("0.0.0.0","test","mysqlpw","test1")例如: db = MySQLdb.connect("0.0.0.0","test","mysqlpw","test1")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker-compose:如何使用相同的网络地址从容器和主机访问 Localstack 资源 - Docker-compose: How to access Localstack resources both from container and host, using same network address 文件未在docker-compose中从容器同步到主机 - File not being synced from container to host in docker-compose 无法从 docker-compose 的另一个容器访问 api 的 docker 容器 - docker container of api not accessible from another container of docker-compose Output 来自 Python 在 Docker 上运行并返回到主机 ZBAEDB53E845AE71F13945AE9Z05 - Output from Python running on Docker back to host with docker-compose 使用docker-compose up与docker-compose run从容器内部执行容器中的命令 - executing commands in containers from within a container using docker-compose up vs docker-compose run 从 docker-compose 运行时容器以代码 0 退出 - Container exited with code 0 when run from docker-compose docker-compose exec导致[Errno 2]没有此类文件或目录:docker容器中的'docker-compose':'docker-compose' - docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container Docker-Compose 输出文件到本地主机 - Docker-Compose Output File To Local Host docker-compose 如何从另一个项目访问模块? - docker-compose how to access module from another project? 如何使用 docker-compose 设置一个容器以允许它的整个卷访问另一个容器 - how to set one container to allow it's entire volume access to another container using docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM