Home > Net >  Access mariadb docker-compose container from host-machine
Access mariadb docker-compose container from host-machine

Time:09-01

I'm trying to access a mariadb-container from a python script on my host-machine (MacOS). 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.

Here is my docker-compose.yml with the attempt on network_mode host:

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?

Thanks!

CodePudding user response:

Every thing seems okay, just check the params when trying to connect to the db:

  • host: 0.0.0.0
  • port: 3306 (as in the docker-compose)
  • user: test (as in the docker-compose)
  • password: mysqlpw (as in the docker-compose)
  • database: test1 (as in the docker-compose)

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

  • Related