9

I have a laravel 8 project created with composer. Then I added laravel sail by composer require laravel/sail and then executed command php artisan sail:install. After sail up command, only mysql container was created and everything works well. But now I want to add redis to my docker container. How can I do this? I have no knowledge with Docker but laravel sail has made it very easy to use Docker. I want to add redis with the help of laravel sail. Is there any way to do that?

Yeasir Arafat
  • 1,425
  • 1
  • 13
  • 28

3 Answers3

14

you can edit docker-compose.yml

In services:

laravel.test:
    ...
    depends_on:
        - mysql
        - redis
...
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]

then finally, in volumes:

volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local

You can also start a fresh new project with

curl -s "https://laravel.build/example-app?with=mysql,redis" | bash

then refer the generated docker-compose.yml

after you edit, save it then run ./vendor/bin/sail build then only sail up -d

TheBlckbird
  • 87
  • 1
  • 9
Kidd Tang
  • 1,821
  • 1
  • 14
  • 17
  • 5
    The command `php artisan sail:add service` now also works. So `php artisan sail:add redis` for Redis if not installed earlier. – rhand Feb 11 '23 at 01:47
10

You can just issue the command php artisan sail:install again

Now select the services you need and separate them with ,

So for example 0, 3, 7 This will update your docker-compose.yml file and add the lines for MySQL, Redis, Mailhog. It might overwrite some of your old settings though!

Once you start sail again it will pull the services and you're good to go.

Dharman
  • 30,962
  • 25
  • 85
  • 135
floGalen
  • 262
  • 2
  • 17
0

I know it is not the best solution but I think it's so easy. Update your docker-compose.yml file using following lines:

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/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
            - redis
    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:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local