Search code examples
phpdocker-composecaddy

base config for docker-compose, caddy and php-fpm


I'm trying to build a base config with caddy and php-fpm on docker-compose. Problem is, I get a "404 file not found" when I try to reach for my index.php file. Here is my config.

docker-compose.yml

version: "3.8"

services:
  caddy:
    image: caddy:alpine
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - $PWD/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/www:/srv/www
      - $PWD/caddy/data:/data
      - $PWD/caddy/config:/config
      - $PWD/caddy/log:/var/log
    depends_on:
      - app
  app:
    image: php:fpm-alpine
    ports:
      - "9000:9000"
    volumes:
      - "$PWD/www:/var/www/html"

Caddyfile

localhost:80 {
    root * /srv/www
    php_fastcgi app:9000
    file_server
}

Finally I have a www folder containing index.php and test.html - http://localhost/test.html works, but http://localhost/index.php gives me a 404.

What am I doing wrong?

EDIT : here is what I tried :

  • I checked that I can ping from one container to the other
  • port 9000 is effectively opened on the php container

It looks like the php files are not mounted at the right place inside the php container, but /var/www/html is the WorkingDir.

I don't know where to go next to troubleshoot this.


Solution

  • Finally, I found the answer!

    php-fpm needs the absolute path of the file, so you either have to have the same path in both containers or add a root directive inside the php_fastcgi block.

    localhost:80 {
        root * /srv/www
        encode gzip
        php_fastcgi php:9000 {
            root /var/www/html
        }
        file_server
        log
    }