docker 使用laravel sail添加新服务

dw1jzc5e  于 2023-05-06  发布在  Docker
关注(0)|答案(3)|浏览(125)

我用composer创建了一个laravel 8项目。然后我通过composer require laravel/sail添加了laravel sail,然后执行了php artisan sail:install命令。在执行sail up命令后,只创建了mysql容器,一切正常。但是现在我想将redis添加到我的docker容器中。我该怎么做?我对Docker没有什么了解,但Laravel Sail让使用Docker变得非常容易。我想在laravel sail的帮助下添加redis。有什么办法吗?

dgenwo3n

dgenwo3n1#

您可以编辑docker-compose.yml
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"]

最后,在volumes中:

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

您还可以使用以下命令启动一个全新的项目

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

然后引用生成的docker-compose.yml
编辑后,保存它,然后运行./vendor/bin/sail build,然后只运行sail up -d

7qhs6swi

7qhs6swi2#

您只需再次发出php artisan sail:install命令即可
现在,选择您需要的服务,并使用,将它们分开
例如0, 3, 7这将更新您的docker-compose.yml文件并添加MySQL, Redis, Mailhog的行。它可能会覆盖你的一些旧的设置虽然!
一旦你再次起航,它会拉动服务,你就可以走了。

oipij1gg

oipij1gg3#

我知道这不是最好的解决办法,但我认为这很容易。使用以下行更新docker-compose.yml文件:

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

相关问题