0

I have built a project based on Docker Sail (Ubuntu on windows), the project is working fine, except when PHPUnit connects with the Database.

enter image description here

I tried to install mysql using "sudo apt-get install php-mysql"

now I get this error enter image description here

here is my docker file

# For more information: https://laravel.com/docs/sail
version: '3'
services:
 laravel.test:
    build:
        context: ./vendor/laravel/sail/runtimes/8.1
        dockerfile: Dockerfile
        args:
            WWWGROUP: '${WWWGROUP}'
    image: sail-8.1/app
    extra_hosts:
        - 'host.docker.internal:host-gateway'
    ports:
        - '${APP_PORT:-80}:80'
    environment:
        WWWUSER: '${WWWUSER}'
        LARAVEL_SAIL: 1
        XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
        XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
    volumes:
        - '.:/var/www/html'
    networks:
        - sail
    depends_on:
        - mysql

 mysql:
    image: 'mysql:8.0'
    ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_DATABASE: '${DB_DATABASE}'
        MYSQL_USER: '${DB_USERNAME}'
        MYSQL_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    volumes:
        - './_dockerdata/sailmysql:/var/lib/mysql'
    networks:
        - sail
    healthcheck:
        test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
        retries: 3
        timeout: 5s

#phpmyadmin
phpmyadmin:
    image: phpmyadmin
    container_name: phpmyadmin
    restart: unless-stopped
    ports: 
    - 8085:80
    environment:
    - PMA_ARBITRARY=1
    networks:
    - sail

 networks:
  sail:
    driver: bridge
 volumes:
  sailmysql:
    driver: local

here is my phpunitxml regarding using tests

    <server name="APP_ENV" value="testing"/>
    <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
    <!-- <server name="DB_DATABASE" value=":memory:"/> -->

here is .env file regarding db connection

 DB_CONNECTION=mysql
 DB_HOST=mysql
 DB_PORT=3306
  • you must execute all commands, including `phpunit` inside the container, else it has no idea how to resolve `mysql` to the docker container – matiaslauriti Dec 05 '22 at 00:42

1 Answers1

0

Please try to change DB_HOST to localhost. Mysql service should run at localhost/127.0.0.1 inside docker container.

Daniel
  • 2,621
  • 2
  • 18
  • 34