Dockerize php application with caddy

59 views Asked by At

I'm trying to get my php application running in docker using Caddy. I have the following files and when I run docker-compose up everything starts, if I then visit localhost the SSL from caddy is set however all I get is a white page. No errors show either suggesting where the issue is.

Dockerfile:

FROM php:8.2-fpm-alpine

WORKDIR /var/www/html

COPY . .

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

EXPOSE 9000

CMD ["php-fpm"]

Caddyfile:

localhost {
  root * /var/www/html/public
  php_fastcgi app:9000
  file_server
}

docker-compose.yml:

version: '3.8'

services:
  app:
    image: app-image
    container_name: app
    expose:
      - "9000:9000"
    networks:
      - app-connect

  caddy:
    image: caddy:latest
    container_name: web_server
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./caddy_data:/data
    networks:
      - app-connect

networks:
  app-connect:

volumes:
  caddy_data:

The structure of the app is like

|-- Controllers
|-- Core
|-- Views
|-- Public
    |-- index.php

I have tired to get this working with even a simple 1 page index.php and have not been able to.

Any help or suggestions would be appreciated.

EDIT: The index.php is:

<?php
use Core\Session;
use Core\ValidationException;

const BASE_PATH = __DIR__.'/../';

session_name("user_session");
session_start();

require BASE_PATH . 'vendor/autoload.php';
require BASE_PATH . 'Core/functions.php';

$router = new \Core\Router();
require BASE_PATH . 'routes.php';

$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$method = $_POST['_method'] ?? $_SERVER['REQUEST_METHOD'];

try {
        $router->route($uri, $method);
} catch (ValidationException $exception) {
        Session::flash('errors', $exception->errors);
        Session::flash('old', $exception->old);

        return redirect($router->previousUrl());
}

Session::unflash();

I have tried removing everything from this file and replacing with which still just returns a white screen. I've also tried removing the root * /var/www/html/public from the Caddyfile and then placing a index.php with phpinfo() in the root of the project and the white screen is still displayed.

1

There are 1 answers

4
Paul Allsopp On

First of all, a white page is usually the sign of a 500 error, which means something on the server-side broke. If you are getting a white page, it means you have no visually exception logging, in which case you need to check the caddy logs to find out why the 500 was thrown.

I'm not sure of your skill-set so I can only give you basic advice.

In your index.php, get rid of whatever is in there, and replace it with

<?php phpinfo();

...that's it.

Now go to the URL and you should see a page of information about your installed version of PHP. If you do not, the error is not a PHP error, it's in your setup. Again without knowing your skill-set or the content of your index.php file (before the suggested change), it's difficult to help. So posting your original index.php content might help.