3

I'm trying to serve a PHP app using Caddy, all Dockerized. Here's my Caddyfile:

http://mydomain.test
root * /app/public
php_fastcgi localhost:9000
rewrite * /index.php?{query}&p={path}
file_server

my Dockerfile:

FROM php:7.0-fpm
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/
COPY --from=caddy:2.3.0-alpine /usr/bin/caddy /usr/local/bin/
RUN apt-get update -y && apt-get install -y libpng-dev libpq-dev
RUN docker-php-ext-install gd
RUN docker-php-ext-install zip
RUN docker-php-ext-install pdo_mysql
WORKDIR /app
COPY composer.* ./
RUN composer install
COPY . ./
CMD ["caddy", "run"]

and my docker-compose.yml:

version: "3"

services:
  app:
    build: .
    ports:
      - 8080:80

All three files are in the root of my PHP project. However when I start this using docker-compose and then visit mydomain.test, I get a 502 HTTP status code. In the stdout of the Docker container I see:

{
  "level": "error",
  "ts": 1624230589.230253,
  "logger": "http.log.error",
  "msg": "dialing backend: dial tcp 127.0.0.1:9000: connect: connection refused",
  "request": {
    "remote_addr": "192.168.112.1:54528",
    "proto": "HTTP/1.1",
    "method": "GET",
    "host": "mydomain.test:8080",
    "uri": "/",
    "headers": {
      "Upgrade-Insecure-Requests": [
        "1"
      ],
      "User-Agent": [
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
      ],
      "Accept": [
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
      ],
      "Accept-Encoding": [
        "gzip, deflate"
      ],
      "Accept-Language": [
        "en-GB,en;q=0.9,es;q=0.8,en-US;q=0.7"
      ],
      "Connection": [
        "keep-alive"
      ],
      "Cache-Control": [
        "max-age=0"
      ]
    }
  },
  "duration": 0.000814111,
  "status": 502,
  "err_id": "rm3277ykx",
  "err_trace": "reverseproxy.statusError (reverseproxy.go:783)"
}

so it seems from the msg that Caddy has problems connecting to php-fpm. But they're both running within the same container, so I don't see why it's not accessible at localhost:9000?

cb7
  • 493
  • 4
  • 10

1 Answers1

1

Your php-fpm isn't running yet this is because you are trying to start caddy in the same Dockerfile. You need to split caddy and php-fpm in your docker-compose file. I got it to work with the following configuration.

Caddyfile:

example.com {
    root * /app/public
    php_fastcgi php-fpm:9000
    file_server
}

docker-compose.yml:

version: '3.3'
services:
  caddy:
    container_name: caddy
    image: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - $PWD/Caddyfile:/etc/caddy/Caddyfile
      - /home/websites:/app/public
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - php-fpm
  php-fpm:
    container_name: php-fpm
    build: .
    restart: unless-stopped
    volumes:
      - /home/websites:/app/public

volumes:
  caddy_data:
  caddy_config:

networks:
  default:
    external:
      name: main

Dockerfile:

FROM php:7.0-fpm

RUN apt-get update -y
RUN docker-php-ext-install pdo_mysql
Niels Prins
  • 535
  • 2
  • 11