skip to Main Content

When I try to run the command php artisan migrate, an error occurs:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = docker and table_name = migrations and table_type = ‘BASE TABLE’)

Dockerfile:

FROM php:8.1-fpm-alpine

RUN docker-php-ext-install pdo pdo_mysql mysqli

docker-compose.yml:

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "8088:80"
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  mysql:
    image: mysql:8.0
    container_name: db
    ports:
      - "3307:3306"
    expose:
      - "3306"
    volumes:
      - ./cache/mysql:/var/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: docker
      MYSQL_USER: root
      MYSQL_PASSWORD: root
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

  composer:
    image: composer:latest
    container_name: composer
    depends_on:
      - nginx
      - php
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    networks:
      - laravel

.env:

DB_CONNECTION=mysql
DB_HOST="db"
DB_PORT=3306
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=root

I try too:

DB_HOST=127.0.0.1
DB_HOST=localhost

thank you very good

2

Answers


  1. In docker-compose.yml

    services:
        ## -----------------------------------------------
        ##           MySql database
        ## -----------------------------------------------
        mysql:
          image: mysql:8.0
          restart: always
          volumes:
            - db_mysql:/var/lib/mysql
            - ./mysql:/docker-entrypoint-initdb.d
          command: --default-authentication-plugin=mysql_native_password
          environment:
            MYSQL_ROOT_PASSWORD: root
          networks:
            - app-network
          deploy:
            mode: global
          ports:
            - "3306:3306"
    
        php:
          ..
          ..
          environment:
            DB_HOST: mysql
            DB_NAME: db_name
            DB_USERNAME: root
            DB_PASSWORD: root
          networks:
            - laravel
    
        // map volume
        volumes:
          db_mysql:
    

    In MySQL Dockerfile

    FROM mysql:8.0
    
    Login or Signup to reply.
  2. please chnage the ports in docker-compose.yml File , it works for me
    ports:
    – 8889:3306

    —-My-> docker-compose.yml File —

    version: ‘3.8’

    services:

    Web Server Service

    nginx:
    image: nginx:alpine
    container_name: nginx
    ports:
    – "8080:80"
    volumes:
    – ./src:/var/www
    – ./nginx/conf.d/:/etc/nginx/conf.d/
    depends_on:
    – app
    – db

    #Database Service
    db:
    image : mysql
    container_name : mysql
    restart: unless-stopped
    tty: true
    ports:
    – 8889:3306

    volumes:

    # - ./mysql/data:/var/lib/mysql
    

    environment:

     # MYSQL_DATABASE: laravel_blogs
    ## MYSQL_ROOT_PASSWORD: password
    #  MYSQL_ROOT_HOST: localhost
    #  SERVICE_NAME: mysql
    #  SERVICE_TAGS: dev
    environment:
      - MYSQL_DATABASE=${DB_DATABASE} 
      - MYSQL_USER=${DB_USERNAME}  
      - MYSQL_PASSWORD=${DB_PASSWORD}  
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD} 
    volumes:
       -  ~/mysql:/var/lib/mysql 
    

    app:
    container_name: app
    build:
    context: ./php
    dockerfile: Dockerfile
    volumes:
    – ./src:/var/www
    ports:
    – "9000:9000"
    working_dir: /var/www

    phpMyAdmin:
    # phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
    – "3400:80"
    depends_on:
    – db
    links:
    – db
    #pgadmin:
    #image: dpage/pgadmin4
    #container_name: pgAdmin
    #ports:
    # – "5050:80"
    #depends_on:
    # – db
    #environment:
    # PGADMIN_DEFAULT_EMAIL: [email protected]

    PGADMIN_DEFAULT_PASSWORD: password

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search