Example: Docker laravel

Minimal setup with docker and Caddy 2 for local or production environment:

Caddyfile

:80 {
	root * /srv/public
	encode gzip
	php_fastcgi php:9000
	file_server
}

docker-compose.yml

caddy:
    image: caddy
    restart: unless-stopped
    volumes:
      - caddy_data:/data
      - caddy_config:/config
      - ./Caddyfile:/etc/caddy/Caddyfile
      - .:/srv
    ports:
      - 80:80
      - 443:443

php:
    image: php:7.4-fpm-alpine
    restart: unless-stopped
    working_dir: /srv
    volumes:
      - .:/srv
1 Like

FYI @milewski I updated your post to fix a few misleading issues.

  • Using /data for the site is not a good idea, /data is where Caddy stores its certificates and other information. You should not be putting your site there. /srv is better.
  • You didn’t have proper volumes for /data and /config, so you would lose your certificates if you tear down your containers. This is not good, and you may hit rate limits if something goes wrong and you get stuck in a reboot loop.
  • You should use the caddy docker image, not caddy/caddy. The former is the official docker image, the later is the “old official”. Both work, but it’s best to just use the actual official image.
  • I added encode gzip, there’s never a situation where you don’t want it on, really.
  • The header_up stuff is not necessary. If your Laravel app is triggering a redirect, it would be due to an app misconfiguration. Is your APP_URL in your .env not set correctly?

@francislavoie

Using /data for the site is not a good idea, /data is where Caddy stores its certificates and other information. You should not be putting your site there. /srv is better.

Thanks, I had literally just upgraded from caddy 1 to 2 so I haven’t looked carefully through the docs yet good to know

You didn’t have proper volumes for /data and /config , so you would lose your certificates if you tear down your containers. This is not good, and you may hit rate limits if something goes wrong and you get stuck in a reboot loop.

Uhm… well yeah that’s correct, but the way I am using it currently it doesn’t really matter saving the settings as I literally just use it as a php cgi / web server, the certificates / gzip, etc are handled by another reverse proxy I have in front of it Traefik that’s why I actually forgot to create volumes. But yeah if using caddy as the main entrance you’d better keep that data!

You should use the caddy docker image, not caddy/caddy . The former is the official docker image, the later is the “old official”. Both work, but it’s best to just use the actual official image.

Thanks again! It moved so fast didn’t notice it had become an official docker image. whoever would still recommend using the alpine version or at least setting up a fixed tag

I added encode gzip , there’s never a situation where you don’t want it on, really.

As I said on my setup Traefilk does that for me you probably woulnt want double zip! but yeah if nothing else in at front that option should be always on

The header_up stuff is not necessary. If your Laravel app is triggering a redirect, it would be due to an app misconfiguration. Is your APP_URL in your .env not set correctly?

That probably might be because my Traefilk already adds the headers… and then Caddy somehow gets confused! I will investigate further Thanks!