How can I connect to MariaDB on the host from within a container?

Hello,
I have a Laravel project that connects to a MariaDB database. I don’t want to use a MariaDB container. I have installed MariaDB on the host and added the following line to the /etc/mysql/mariadb.conf.d/50-server.cnf file:

bind-address = 0.0.0.0

MariaDB is running on the host:

# netstat -tulnp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      3172972/mariadbd 

I edited the .env file as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=123456

I also changed the database.php file of the Laravel project as follows:

 'mariadb' => [
            'driver' => 'mariadb',
            'url' => env('DB_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'laravel_db'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => env('DB_CHARSET', 'utf8mb4'),
            'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

The compose file looks like this:

services:
  frontend:
    container_name: frontend
    build:
      context: /home/dev/frontend
      dockerfile: Dockerfile
    environment:
      - NODE_ENV=development
      - CHOKIDAR_USEPOLLING=true  
      - WATCHPACK_POLLING=true   
    volumes:
      - /home/dev/frontend:/app
      - /app/node_modules          
    ports:
      - "127.0.0.1:3000:3000"              
    networks:
      - app_network
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M

  # Laravel Backend
  backend:
    build:
      context: /home/dev/portal
      dockerfile: Dockerfile
    container_name: backend
    entrypoint: ["/usr/local/bin/entrypoint.sh"]
    command: ["php-fpm"]
    environment:
      APP_ENV: local
      APP_DEBUG: "true"
      DB_HOST: host.docker.internal
      DB_PORT: 3306
      DB_DATABASE: laravel_db
      DB_USERNAME: root
      DB_PASSWORD: 123456
      XDEBUG_CONFIG: "client_host=host.docker.internal client_port=9003"
    volumes:
      - /home/dev/portal:/var/www
    networks:
      - app_network
    ports:
      - "9000:9000"
    extra_hosts:
      - "host.docker.internal:host-gateway"

networks:
  app_network:
    driver: bridge

After these settings, I ran the containers and connected to the backend container:

# docker exec -u root -it backend bash
#
# nc HOST_IP -v 80
Ncat: Version 7.93 ( https://nmap.org/ncat )
Ncat: Connected to 172.20.2.58:80.
#
# nc HOST_IP -v 3306
Ncat: Version 7.93 ( https://nmap.org/ncat )
Ncat: No route to host.

As you can see, from inside the container I can connect to port 80 which is for Nginx, but I can’t connect to MariaDB.

Which part of the settings is wrong?

Thank you.

Hello!
Did you have a fix on this issue?

from inside the container

Which container? MariaDB or another one?
Any other container will not be able to connect to MariaDB through localhost because each container is its own host

This means, when creating a request to localhost from the context of your laravel container, localhost directs the request - to the Laravel container

For containers to access each other you can add them to the same network, then access them using service/container names mariadb://mariadb

Hello,
Thank you so much for your reply.
From backend container. As you can see, I have connected the containers via the app_network network. By the way, from inside the backend container I can see Nginx on the host and just can’t see MariaDB.

Still, you’re trying to access using localhost, localhost does NOT redirect to the host machine running docker, but to the container sending this request

So, how to solve it?