skip to Main Content

this is my docker.yml file

services:
  web:
    image: app-name:latest
    build:
      target: web
      context: .
    env_file:
      - .env
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    ports:
      - 3307:3307
    expose:
      - "3307"
    cap_add:
      - SYS_NICE 
    command: --skip-grant-tables
    volumes:
      - ./cache/mysql:/var/lib/mysql
      - ./conf-mysql.cnf:/etc/mysql/conf.d/mysql.cnf
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=chat

    restart: unless-stopped

volumes:
  dbdata:



this is my database file config in .env


DB_CONNECTION=mysql
DB_HOST=host.docker.internal
# DB_HOST=db
DB_PORT=3307
DB_DATABASE=chat
DB_USERNAME=root
DB_PASSWORD=password

i have tried db as well host.docker.internal. but unable to resolve this issue your text

SQLSTATE[HY000] [2006] MySQL server has gone away

i want to connect of my docter app to database`your text“

3

Answers


  1. I see a couple of issues. When using docker, you should use the service name as the host. In your case, that would be ‘db’. So in your .env file, use:

    DB_HOST=db
    

    You’re exposing MySQL on port 3307, but the standard MySQL port is 3306. Change the MySQL port to 3306

    db:
      ports:
        - 3306:3306
      expose:
        - "3306"
    

    then in your .env file:

    DB_PORT=3306
    
    Login or Signup to reply.
  2. Use localhost as DB Host.

    DB_CONNECTION=mysql
    DB_HOST=localhost
    # DB_HOST=db
    DB_PORT=3307
    DB_DATABASE=chat
    DB_USERNAME=root
    DB_PASSWORD=password
    

    check container-id

    docker container ls 
    

    only first 4 characters required.

    check logs using this command. Eg.

    docker log a1bcd
    
    Login or Signup to reply.
  3. Try

    
    services:
      web:
        image: app-name:latest
        build:
          target: web
          context: .
        env_file:
          - .env
        ports:
          - "8000:80"
        volumes:
          - .:/var/www/html
        depends_on:
          - db
        networks:
          my_network:
            ipv4_address: 172.18.0.11
    
      db:
        image: mysql:8.0
        ports:
          - "3306:3306"
        expose:
          - "3306"
        cap_add:
          - SYS_NICE 
        command: --skip-grant-tables
        volumes:
          - ./cache/mysql:/var/lib/mysql
          - ./conf-mysql.cnf:/etc/mysql/conf.d/mysql.cnf
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=chat
        restart: unless-stopped
        networks:
          my_network:
            ipv4_address: 172.18.0.10
    
    networks:
      my_network:
        driver: bridge
        ipam:
          config:
            - subnet: 172.18.0.0/16
    
    volumes:
      dbdata:
    

    .env

    DB_HOST=172.18.0.10
    DB_PORT=3306
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search