Why caddy + php-fpm configuration in docker-compose doesn't work?

Viewed 1626

I'm trying to set up a development environment on my local machine which consists of caddy + PHP.

docker-compose.yaml:

version: '3'

services:
    app:
        build:
            context: .
        container_name: app
        restart: unless-stopped
        tty: true
        environment:
            SERVICE_NAME: app
            SERVICE_TAGS: dev
        volumes:
            - ./:/var/www
            - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini

    webserver:
        image: caddy
        container_name: webserver
        restart: unless-stopped
        tty: true
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./:/srv
            - ./docker/caddy/Caddyfile/:/etc/caddy/Caddyfile

    db:
        image: mysql:8.0
        container_name: db
        restart: unless-stopped
        tty: true
        ports:
            - "3306:3306"
        environment:
            MYSQL_USER: pingr
            MYSQL_DB: pingr
            MYSQL_PASSWORD: pingr
            MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        volumes:
            - dbdata:/var/lib/mysql

volumes:
    dbdata:
        driver: local

Dockerfile for app service:

FROM php:7.4-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libzip-dev \
    libicu-dev

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl intl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Caddyfile:

:80 {
    root * /srv/public
    php_fastcgi app:9000

    file_server /uploads/*
    encode gzip zstd
}

This results in "File not found" when I open http://localhost.

I'm 100% sure that the problem is because root in caddyfile and volumes in app and webserver serivces are different.

When I make them the same, looks like it works. Moreover, it even doesn't matter which path I choose, I can have /var/bar for example.

But then I don't understand how it works.

  1. In caddy image they say that the working directory is /srv. So as I get it, this is where site files should be placed.

  2. In the Caddyfile I specify that Caddy should look at /srb/public (because I use Laravel)..

  3. In the php-fpm image, they say that the working directory is /var/www. Which means that.. I'm not sure what does it mean. If I don't use Docker, then in my local machine I'd have caddy + php, only caddy "knows" where the source .php files are located. In case the requests asks for a .php file Caddy proxies it to php. Maybe I don't need a volume here at all?

0 Answers
Related