skip to Main Content

I know that this question has a lot answers on stackoverflow but I didn’t found a solution for my case!

I moving the Laravel app to the containers.

I CAN CONNECT TO MARIADB INSTANCE OUTSIDE THE DOCKER NETWORK BUT NOT INSIDE!

(I can connect via MySQL Workbench, locally (via docker exec), I can restore the dump locally from container console and access to DB data outside)

What’s wrong?

Why the app is not working (PHP has no access to the mariadb via internal app_network) but in the same time I can get access to DB outside and inside container itself???

OS: CentOS 7.9.2009
Docker: 20.10.12 (e91ed57)
Docker-compose: 1.29.2 (5becea4c)

The same configs works fine on Windows 10.

DOCKER COMPOSE CONFIG:

version: '3.9'

networks:
  app_network:
    driver: bridge
    name: ${NETWORK_NAME}

volumes:
  app:
    name: ${APP_VOLUME_NAME}
  mysql_database:
    name: ${MYSQL_DATABASE_VOLUME_NAME}
  mysql_dumps:
    name: ${MYSQL_DATABASE_DUMPS_VOLUME_NAME}

services:

  mariadb:
    image: mariadb
    env_file:
      - ./.env
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - ${MYSQL_EXTERNAL_PORT}:3306
    volumes:
      - mysql_database:/var/lib/mysql
      - mysql_dumps:/var/mysqldump
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    networks:
      app_network:
        aliases:
          - mariadb
    profiles:
      - dev
      - prod

  php:
    restart: always
    env_file:
      - ./.env
    build:
      context: ../../
      dockerfile: ./.environment/cs/php/Dockerfile
      args:
        - USER_ID=${PHP_USER_ID}
        - GROUP_ID=${PHP_GROUP_ID}
        - DEFAULT_CONFIG_FILE=${PHP_DEFAULT_CONFIG_FILE}
        - CUSTOM_CONFIG_FILE=${PHP_CUSTOM_CONFIG_FILE}
        - PROJECT_FOLDER=${PHP_PROJECT_FOLDER}
    volumes:
      - ./php/logs:/var/log
      - ../../:${PHP_PROJECT_FOLDER}
    networks:
      app_network:
        aliases:
          - php
    depends_on:
      - memcached
      - mariadb
    profiles:
      - dev
      - prod

  nginx:
    restart: always
    env_file:
      - ./.env
    build:
      context: ../../
      dockerfile: ./.environment/cs/nginx/Dockerfile
      args:
        - CONFIG_FILE=${WEB_CONFIG_FILE}
        - PROJECT_FOLDER=${WEB_PROJECT_FOLDER}
    ports:
      - ${WEB_EXTERNAL_PORT}:80
    volumes:
      - ./nginx/logs:/var/log/nginx
      - ../../public:${WEB_PROJECT_FOLDER}:cached
    networks:
      app_network:
        aliases:
          - nginx
    depends_on:
      - php
    profiles:
      - dev
      - prod

Docker .ENV

NETWORK_NAME=CS

APP_VOLUME_NAME=CS_APP_STORAGE

MYSQL_DATABASE_VOLUME_NAME=CS_DATABASE
MYSQL_DATABASE_DUMPS_VOLUME_NAME=CS_DATABASE_DUMPS
MYSQL_EXTERNAL_PORT=3317
MYSQL_ROOT_PASSWORD=root
MYSQL_USER=client
MYSQL_PASSWORD=client

PHP_USER_ID=1000
PHP_GROUP_ID=1000
PHP_DEFAULT_CONFIG_FILE=php.ini-production
PHP_CUSTOM_CONFIG_FILE=./.environment/cs/php/custom.prod.ini
PHP_PROJECT_FOLDER=/var/www/app

WEB_EXTERNAL_PORT=127.0.0.1:8091
WEB_CONFIG_FILE=./.environment/cs/nginx/nginx.dev.conf
WEB_PROJECT_FOLDER=/var/www/app/public

Laravel .ENV

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=client
DB_USERNAME=client
DB_PASSWORD=client

2

Answers


  1. Chosen as BEST ANSWER

    This is the issue of the MariaDB container. I connected to the MariaDB via MySQL Workbench, fully remove the user, create a new one and give scheme privileges. After that all works fine.


  2. try to add the expose config key in the maria db service

    mariadb:
     //...
      expose:
          - 3306
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search