Laravel Scout (meilisearch) with Docker
Asked Answered
D

5

6

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

Deputize answered 14/9, 2022 at 1:28 Comment(0)
D
8

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.

Deputize answered 14/9, 2022 at 14:5 Comment(3)
Thank you so much! I was getting the same error while tryint to run artisan scout:import "App\Models\Foo" and couldn't figure out what was going on. Your solution worked for me!Beguin
Glad to hear that <3Deputize
Also, if your mailisearch has a dedicated IP v4 address, use as follows: MEILISEARCH_HOST=http://123.45.67.89:7720Undervest
D
3

Since your Meilisearch's service is called "meilisearch", could you try MEILISEARCH_HOST=http://meilisearch:7720?

Deer answered 14/9, 2022 at 7:58 Comment(1)
Still not workingDeputize
C
-1

Brother, first thing first, the container isn't exposing environment variables. If you can specify something like env_file or use the secrets methodology then you can access your environment variables inside the container. And, the environment variables like SCOUT_DRIVER=meilisearch MEILISEARCH_HOST=http://127.0.0.1:7720 aren't assigned to any of the services. Maybe that's preventing it to initiate.

Compromise answered 14/9, 2022 at 5:35 Comment(2)
If I got you, you mean my defined env variables are not used by the container, if this is what you mean, I tried to check the env variables inside the container and it's already thereDeputize
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Highchair
C
-1

Add this into your .env file

MEILISEARCH_HOST=http://meilisearch:7720

Cointreau answered 1/12, 2023 at 5:51 Comment(0)
M
-2

I noticed you’re making an external API request to a route in the web.php and that might be the cause of your problem. Setup your routes in your api.php and try again

Mayman answered 14/9, 2022 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.