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 returnsConnected to localhost (127.0.0.1) port 7720 (#0)
but whenever I send frominsomnia
I got the error
steps to reproduce the error
-
trying to search
Event
model by kewordPENDING
.AppModelsEvent::search('PENDING')->get()
-
Event model class
<?php
namespace AppModels;
use IlluminateSupportStr;
use LaravelScoutSearchable;
use DatabaseFactoriesEventFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentFactoriesHasFactory;
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();
}
}
- 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
- 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
4
Answers
I figured out the issue was that I am using
meilisearch
as a docker service so I should specify theMEILISEARCH_HOST
env variable to point to the service name likeDB_HOST=db
(docker service name). so in this case I changed theMEILISEARCH_HOST
fromMEILISEARCH_HOST=http://127.0.0.1:7720
toMEILISEARCH_HOST=meilisearch:7700
now everything is working fine as expected.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 yourapi.php
and try againBrother, first thing first, the container isn’t exposing environment variables. If you can specify something like
env_file
or use thesecrets
methodology then you can access your environment variables inside the container. And, the environment variables likeSCOUT_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.Since your Meilisearch’s service is called "meilisearch", could you try
MEILISEARCH_HOST=http://meilisearch:7720
?