0

I have an issue with my app when attempting database connection from php app in cli I receive this error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

When I run the website I get:

Illuminate\Database\QueryException SQLSTATE[HY000] [2002] Connection refused (SQL: select * from sessions where id = TKks6w5zbHk8bZsm6BEgFyKLJGXj05eyYHQfJsaB limit 1)

I have my docker image setup with docker-compose.yml and .Dockerfile also nginx folder with app.conf and mysql folder with my.cnf I will pop these here

version: '3.4'

services:
  mariadb: 
    image: mariadb/server:latest
    container_name: db
    restart: unless-stopped
    tty: true
    ports: 
      - "3306:3306"
    environment:
      MYSQL_DATABASE: PiApp
      MYSQL_ROOT_PASSWORD: rootpass
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes: 
      - dbdata:/var/lib/mysql/
      - .\mysql/my.cnf:/etc/mysql/my.cnf
    networks: 
      - Pi-Wallet-Network

  phpmyadmin: 
    image: phpmyadmin/phpmyadmin
    ports:
      - "8080:80"
    restart: unless-stopped
    environment:
      - PMA_HOST=mariadb
    links:
      - mariadb
    networks: 
      - Pi-Wallet-Network

  app:
    image: php:7.4-fpm
    build:
      context: .
      dockerfile: ./Dockerfile
    environment:
      NODE_ENV: production
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - .\:/var/www
      - .\php/local.ini:/usr/local/etc/php/conf.d/local.ini
    links:
      - mariadb
    networks:
      - Pi-Wallet-Network

  webserver:
    image: nginx:latest
    container_name: webserver 
    tty: true
    ports: 
      - "80:80"
      - "443:443"
    volumes:
      - .\:/var/www
      - .\nginx/conf.d/:/etc/nginx/conf.d/
    links:
      - mariadb
    networks:
      - Pi-Wallet-Network

networks:
  Pi-Wallet-Network:
    driver: bridge

volumes:
  dbdata:
   driver: local

my .Dockerfile:

FROM php:7.4-fpm
# FROM mariadb/server:latest

# Copy composer.lock and composer.json into the working directory
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www/

# Install dependencies for the operating system software
RUN printf '#!/bin/sh\nexit 0' > /usr/sbin/policy-rc.d
RUN apt-get update && apt-get upgrade -y 
RUN apt-get install -y mariadb-server 
RUN apt-get install -y build-essential 
RUN apt-get install -y libpng-dev
RUN apt-get install -y libjpeg-dev
RUN apt-get install -y libjpeg62-turbo-dev
RUN apt-get install -y libfreetype6-dev 
RUN apt-get install -y locales 
RUN apt-get install -y zip 
RUN apt-get install -y jpegoptim optipng pngquant gifsicle 
RUN apt-get install -y vim 
RUN apt-get install -y libzip-dev 
RUN apt-get install -y unzip 
RUN apt-get install -y git 
RUN apt-get install -y libonig-dev 
RUN apt-get install -y curl

# Install extensions for php
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Install composer (php package manager)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin 
filename=composer

# Copy existing application directory contents to the working directory
COPY . /var/www/

# Assign permissions of the working directory to the www-data user
# RUN chown -R www-data:www-data /var/www/*
RUN chown -R www-data:www-data /var/www//storage

# Expose port 9000 and start php-fpm server (for FastCGI Process Manager)
EXPOSE 9000
CMD ["php-fpm"]

1 Answers1

0

I fixed this by changing DB_HOST=127.0.0.1 to DB_HOST=host.docker.internal

as this Quesion