This is how I accomplished using the Caddy web server docker image with Laravel Sail. Feel free to use it as a guide or reference. You can also leave a comment if you have any suggestions.

This was tested with Laravel 9.21 and Caddy 2.5.

Table of Contents

Create a Laravel project with Sail

Starting with a fresh Laravel project called sail-caddy:

curl -s https://laravel.build/sail-caddy | bash
cd sail-caddy

Next we’ll start the default Sail environment and publish it’s assets:

./vendor/bin/sail up -d
./vendor/bin/sail artisan sail:publish

The sail up -d command starts Sail’s docker services in the background so that we can continue using the same command prompt.

The sail artisan sail:publish command will allow us to customize the docker image by using Sail’s Dockerfile as a starting point.

Modify docker-compose.yml

We can now edit the docker-compose.yml file with the following changes:

services:
-   laravel.test:
+   laravel.app:
        build:
            context: ./docker/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
-       image: sail-8.1/app
+       image: mysite-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
-           - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        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'
+           - 'sail-unix-sockets:/var/run'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - meilisearch

volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local
    sail-meilisearch:
        driver: local
+   sail-caddy-data:
+   sail-caddy-config:
+   sail-unix-sockets:

I recommend you change the image name because you’re going to be making changes to the Dockerfile so you’ll want that image to be unique to your application.

Next we need to add the Caddy service before the volumes section, make sure to specify a version to ensure it doesn’t break:

    caddy:
        image: caddy:2.5.2
        working_dir: /var/www/html
        command: 'caddy run'
        environment:
            APP_URL: '${APP_URL}'
        ports:
            - '${APP_PORT:-80}:80'
            - "443:443"
        volumes:
            - './:/var/www/html'
            - 'sail-caddy-data:/data'
            - 'sail-caddy-config:/config'
            - 'sail-unix-sockets:/var/run'

Modify Dockerfile

Take the moment to also update the maintainer label.

LABEL maintainer="AND1"

RUN apt-get update \
    # ... MODIFY the php-cli install line to php-fpm (change the PHP version accordingly)
-   && apt-get install -y php8.1-cli php8.1-dev \
+   && apt-get install -y php8.1-fpm php8.1-dev \

# ... ADD the PHP FPM configuration section (change the PHP version accordingly)
+ RUN echo "\n[www]" >> /etc/php/8.1/fpm/pool.d/www.conf \
+     && echo "user=sail" >> /etc/php/8.1/fpm/pool.d/www.conf \
+     && echo "clear_env=false" >> /etc/php/8.1/fpm/pool.d/www.conf

- COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini
+ COPY php.ini /etc/php/8.1/fpm/conf.d/99-sail.ini

# ... REMOVE the EXPOSE 8000 line
- EXPOSE 8000

Modify supervisord.conf

- [program:php]
- command=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80
- user=sail
+ [program:php-fpm]
+ command=/usr/sbin/php-fpm8.1 -F
environment=LARAVEL_SAIL="1"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

Create the Caddyfile

touch Caddyfile
{$APP_URL:http://laravel.test} {
    root * {$SITE_ROOT_PATH:./public}
    header {
        X-Frame-Options SAMEORIGIN
        x-Content-Type-Options nosniff
    }
    @cacheItems path_regexp \.(css|ico|js|woff2)*
    header @cacheItems Cache-Control "max-age=31536000, immutable"
    encode zstd gzip
    php_fastcgi {$PHP_FASTCGI_LISTEN:unix//var/run/php/php8.1-fpm.sock}
    file_server
}

Rebuild the docker image

./vendor/bin/sail build --no-cache

Leave a Reply

Your email address will not be published. Required fields are marked *