Skip to content

Commit

Permalink
build: add Caddy load balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlcidesRC committed Sep 11, 2024
1 parent 4b39dfe commit 76fcfc5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 63 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ FROM common AS build-production

ENV ENV=PRODUCTION

# Setup the FPM servie name and port
RUN sed -i -r "s/LISTEN/${LISTEN}/g" /healthcheck.sh

# Add __ONLY__ compiled extensions & their config files
COPY --from=extensions-builder-common /usr/local/lib/php/extensions/*/* /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
COPY --from=extensions-builder-common /usr/local/etc/php/conf.d/* /usr/local/etc/php/conf.d/
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ endif

#---

SERVICE_APP = app
SERVICE_APP = app1
SERVICE_CADDY = caddy

#---
Expand Down Expand Up @@ -143,7 +143,7 @@ down: ## Docker: stops the service <env=[dev|prod]>
.PHONY: logs
logs: ## Docker: exposes the service logs <env=[dev|prod]>
@$(eval env ?= 'dev')
@$(eval service ?= 'app')
@$(eval service ?= 'caddy')
@$(DOCKER_COMPOSE) logs $(service)
$(call taskDone)

Expand Down
20 changes: 10 additions & 10 deletions build/dev/Caddyfile → build/Caddyfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(common-server) {
tls internal

(generic) {
encode zstd gzip

respond /healthcheck 200

php_fastcgi app:9000

root * /var/www/html/public

file_server
}

localhost {
import common-server
(ssl) {
tls internal
}

{$HOSTNAME} {
import generic
import ssl

php_fastcgi {$PHP_FPM_GATEWAYS}
}
2 changes: 1 addition & 1 deletion build/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

output=$(SCRIPT_NAME=/status/ping SCRIPT_FILENAME=/status/ping REQUEST_METHOD=GET cgi-fcgi -bind -connect app:9000 | tail -1)
output=$(SCRIPT_NAME=/status/ping SCRIPT_FILENAME=/status/ping REQUEST_METHOD=GET cgi-fcgi -bind -connect :9000 | tail -1)

if [ "$output" = "pong" ]; then
exit 0
Expand Down
17 changes: 0 additions & 17 deletions build/prod/Caddyfile

This file was deleted.

21 changes: 12 additions & 9 deletions docker-compose.override.dev.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
services:
app:
build:
target: build-development
image: app:development
volumes:
- ./coverage:/coverage

caddy:
volumes:
- ./build/dev/Caddyfile:/etc/caddy/Caddyfile:ro
environment:
- HOSTNAME=website.localhost
- PHP_FPM_GATEWAYS=app1:9000 app2:9000
healthcheck:
test: ["CMD", "wget", "--spider", "https://website.localhost/healthcheck"]
depends_on:
- app1
- app2

app2:
extends:
file: docker-compose.yml
service: app1
container_name: app2
15 changes: 2 additions & 13 deletions docker-compose.override.prod.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
services:
app:
extends:
file: docker-compose.yml
service: app
build:
target: build-production
image: app:production

caddy:
extends:
file: docker-compose.yml
service: caddy
volumes:
- ./build/prod/Caddyfile:/etc/caddy/Caddyfile:ro
environment:
- HOSTNAME=acme.com
healthcheck:
test: ["CMD", "wget", "--spider", "https://acme.com/healthcheck"]
29 changes: 18 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@ volumes:
caddy_config:

services:
app:
container_name: app
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
tty: true
volumes:
- ./src:/var/www/html

caddy:
container_name: caddy
image: caddy:2.8.4-alpine
restart: unless-stopped
environment:
- HOSTNAME=localhost
- PHP_FPM_GATEWAYS=app1:9000
volumes:
- caddy_data:/data
- caddy_config:/config
- ./src:/var/www/html
- ./build/Caddyfile:/etc/caddy/Caddyfile:ro
ports:
- 80:80
- 443:443
- 443:443/udp
healthcheck:
test: ["CMD", "wget", "--spider", "https://localhost/healthcheck"]
interval: 10s
timeout: 1s
retries: 3
depends_on:
- app
- app1

app1:
container_name: app1
build:
context: .
dockerfile: Dockerfile
target: build-development
image: app:development
restart: unless-stopped
tty: true
volumes:
- ./src:/var/www/html

0 comments on commit 76fcfc5

Please sign in to comment.