Search code examples
phplaraveldockerdocker-composemeilisearch

Laravel Scout (meilisearch) with Docker


I am trying to use Laravel Scout with docker I followed along with the docs Laravel Scout Docs I made all required steps for installation and everything is up and running as expected. but whenever I try to search data I received this error cURL error 7: Failed to connect to 127.0.0.1 port 7720: Connection refused. I am sending the request from insomnia to laravel route web.

  • Note:- when I visit http://127.0.0.1:7720/ the meilisearch default page shows up. also tried to curl -kvs --http2 --request GET 'http://localhost:7720/indexes' it works fine and returns Connected to localhost (127.0.0.1) port 7720 (#0) but whenever I send from insomnia I got the error

steps to reproduce the error

  1. trying to search Event model by keword PENDING.

    \App\Models\Event::search('PENDING')->get()

  2. Event model class

<?php

namespace App\Models;

use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use Database\Factories\EventFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Event extends Model
{
    use HasFactory;

    use Searchable;

    public $incrementing = false;

    protected $keyType   = 'string';

    protected $fillable = ['event_type_id', 'status'];

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            $model->id = Str::uuid()->toString();
        });
    }

    public static function newFactory(): EventFactory
    {
        return EventFactory::new();
    }
}

  1. Laravel .env file
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7720
MEILISEARCH_KEY=masterKey
MEILISEARCH_NO_ANALYTICS=false
MEILISEARCH_NO_SENTRY=true
  1. docker-compose.yml
version: "3.7"
services:
    app:
        build:
            context: ./
            dockerfile: Dockerfile
        image: app:latest
        container_name: app
        restart: unless-stopped
        working_dir: /var/www/
        volumes:
            - ./:/var/www
        ports:
            - 9000:9000
        networks:
            - app-network

    db:
        image: mysql:8.0
        container_name: db
        restart: unless-stopped
        environment:
            MYSQL_DATABASE: ${DB_DATABASE}
            MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
            MYSQL_PASSWORD: ${DB_PASSWORD}
            MYSQL_USER: ${DB_USERNAME}
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql
        volumes:
            - ./docker-compose/mysql:/docker-entrypoint-initdb.d
        networks:
            - app-network
        ports:
            - 3307:3306

    nginx:
        image: nginx:alpine
        container_name: nginx
        restart: unless-stopped
        ports:
            - 8000:80
        volumes:
            - ./:/var/www
            - ./docker-compose/nginx:/etc/nginx/conf.d/
        networks:
            - app-network

    phpmyadmin:
        image: phpmyadmin
        container_name: pma
        restart: unless-stopped
        ports:
            - 8283:80
        environment:
            PMA_HOSTS: ${DB_HOST}
            PMA_ARBITRARY: 1
            PMA_USER: ${DB_USERNAME}
            PMA_PASSWORD: ${DB_PASSWORD}
        networks:
            - app-network

    meilisearch:
        image: getmeili/meilisearch:latest
        container_name: meilisearch
        restart: unless-stopped
        ports:
            - 7720:7700
        environment:
            MEILI_NO_ANALYTICS: ${MEILISEARCH_NO_ANALYTICS}
            MEILI_NO_SENTRY: ${MEILISEARCH_NO_SENTRY}
            MEILI_MASTER_KEY: ${MEILISEARCH_KEY}
        networks:
            - app-network
        volumes:
            - ./meilisearch-data:/meilisearch-data

networks:
    app-network:
        driver: bridge


Solution

  • I figured out the issue was that I am using meilisearch as a docker service so I should specify the MEILISEARCH_HOST env variable to point to the service name like DB_HOST=db (docker service name). so in this case I changed the MEILISEARCH_HOST from MEILISEARCH_HOST=http://127.0.0.1:7720 to MEILISEARCH_HOST=meilisearch:7700 now everything is working fine as expected.