Skip to content

Commit

Permalink
build(Dockerfile): standarize the way images are built
Browse files Browse the repository at this point in the history
  • Loading branch information
Alcides Ramos committed Oct 5, 2024
1 parent 0bf4c86 commit 496826e
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 106 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# STAGE: BASE-IMAGE
#----------------------------------------------------------

FROM php:8.3.10-fpm-alpine AS base-image
FROM php:8.3.12-fpm-alpine AS base-image

#----------------------------------------------------------
# STAGE: CADDY-BUILDER
Expand All @@ -25,12 +25,13 @@ FROM base-image AS common
RUN apk update && apk add --no-cache \
caddy \
fcgi \
libzip \
nss-tools

# Add a custom HEALTHCHECK script
# Ensure the `healthcheck.sh` can be executed inside the container
COPY --chmod=777 build/healthcheck.sh /healthcheck.sh
HEALTHCHECK --interval=10s --timeout=1s --retries=3 CMD /healthcheck.sh
HEALTHCHECK --interval=30s --timeout=1s --retries=3 --start-period=10s --start-interval=2s CMD /healthcheck.sh

WORKDIR /var/www/html

Expand All @@ -51,7 +52,7 @@ FROM base-image AS extensions-builder-common

# Add, compile and configure PHP extensions
RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
apcu
zip

#----------------------------------------------------------
# STAGE: EXTENSIONS-BUILDER-DEV
Expand Down Expand Up @@ -96,7 +97,6 @@ COPY --from=composer /usr/bin/composer /usr/bin/composer

# Add OS dependencies related with development
RUN apk update && apk add --no-cache \
bash \
git \
make \
ncurses \
Expand Down
156 changes: 100 additions & 56 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ endif

#---

SERVICE_APP = app
SERVICE_APP = custom-caddy

#---

WEBSITE_URL = https://website.localhost
WEBSITE_URL = https://localhost

#---

Expand All @@ -43,15 +43,12 @@ HOST_GROUP_NAME := $(shell id --group --name)

#---

DOCKER_COMPOSE = docker compose
DOCKER_COMPOSE = docker compose --file docker-compose.yml --file docker-compose.override.$(env).yml

DOCKER_RUN = $(DOCKER_COMPOSE) run --rm $(SERVICE_APP)
DOCKER_EXEC = $(DOCKER_COMPOSE) exec $(SERVICE_APP)
DOCKER_BUILD_ARGUMENTS = --build-arg="HOST_USER_ID=$(HOST_USER_ID)" --build-arg="HOST_USER_NAME=$(HOST_USER_NAME)" --build-arg="HOST_GROUP_ID=$(HOST_GROUP_ID)" --build-arg="HOST_GROUP_NAME=$(HOST_GROUP_NAME)"

DOCKER_RUN = $(DOCKER_COMPOSE) run --rm $(SERVICE_APP)
DOCKER_RUN_AS_USER = $(DOCKER_COMPOSE) run --rm --user $(HOST_USER_ID):$(HOST_GROUP_ID) $(SERVICE_APP)
DOCKER_EXEC_AS_USER = $(DOCKER_COMPOSE) exec --user $(HOST_USER_ID):$(HOST_GROUP_ID) $(SERVICE_APP)

DOCKER_BUILD_ARGUMENTS = --build-arg="HOST_USER_ID=$(HOST_USER_ID)" --build-arg="HOST_USER_NAME=$(HOST_USER_NAME)" --build-arg="HOST_GROUP_ID=$(HOST_GROUP_ID)" --build-arg="HOST_GROUP_NAME=$(HOST_GROUP_NAME)"

###
# FUNCTIONS
Expand Down Expand Up @@ -90,77 +87,102 @@ define orderedList
@echo ""
endef

# $(1)=PADDING NUMBER
define pad
$(shell printf "%-$(1)s" " ")
endef

###
# HELP
###

.PHONY: help
help:
@clear
@echo "╔══════════════════════════════════════════════════════════════════════════════╗"
@echo "║ ║"
@echo "${YELLOW}.:${RESET} AVAILABLE COMMANDS ${YELLOW}:.${RESET}"
@echo "║ ║"
@echo "╚══════════════════════════════════════════════════════════════════════════════╝"
@echo ""
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "· ${YELLOW}%-30s${RESET} %s\n", $$1, $$2}'
@echo ""

###
# MISCELANEOUS
###

.PHONY: show-context
show-context: ## Setup: show context
$(call showInfo,"Showing context")
@echo " · Domain : ${YELLOW}${WEBSITE_URL}${RESET}"
@echo " · Host user : (${YELLOW}${HOST_USER_ID}${RESET}) ${YELLOW}${HOST_USER_NAME}${RESET}"
@echo " · Host group : (${YELLOW}${HOST_GROUP_ID}${RESET}) ${YELLOW}${HOST_GROUP_NAME}${RESET}"
@echo " · Service(s) : ${YELLOW}${SERVICE_APP}${RESET}"
@echo "${BLACK}"
@echo "╔════════════════════════════════════════════════════════════════════════════════════════════════════════╗"
@echo "$(call pad,96)"
@echo "$(call pad,32) ${YELLOW}.:${RESET} AVAILABLE COMMANDS ${YELLOW}:.${BLACK} $(call pad,32)"
@echo "$(call pad,96)"
@echo "╚════════════════════════════════════════════════════════════════════════════════════════════════════════╝"
@echo "${BLACK}·${RESET} ${MAGENTA}DOMAIN(s)${BLACK} .... ${CYAN}$(WEBSITE_URL)${BLACK}"
@echo "${BLACK}·${RESET} ${MAGENTA}SERVICE(s)${BLACK} ... ${CYAN}$(shell docker ps --format "{{.Names}}")${BLACK}"
@echo "${BLACK}·${RESET} ${MAGENTA}USER${BLACK} ......... ${WHITE}(${CYAN}$(HOST_USER_ID)${WHITE})${BLACK} ${CYAN}$(HOST_USER_NAME)${BLACK}"
@echo "${BLACK}·${RESET} ${MAGENTA}GROUP${BLACK} ........ ${WHITE}(${CYAN}$(HOST_GROUP_ID)${WHITE})${BLACK} ${CYAN}$(HOST_GROUP_NAME)${BLACK}"
@echo "${RESET}"
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "${BLACK}·${RESET} ${YELLOW}%-35s${RESET} %s\n", $$1, $$2}'
@echo ""
$(call showInfo,"SSL")
@echo " · Please execute [ ${YELLOW}make install-caddy-certificate${RESET} ] to register ${CYAN}Caddy's Root Certificate${RESET} on your browser"
$(call taskDone)

###
# DOCKER RELATED
###

.PHONY: build
build: ## Docker: builds the service
build: ## Docker: builds the service <env=[dev|prod]>
@$(eval env ?= 'dev')
$(call showInfo,"Building Docker image\(s\)...")
@echo ""
@$(DOCKER_COMPOSE) build $(DOCKER_BUILD_ARGUMENTS)
$(call taskDone)

.PHONY: up
up: ## Docker: starts the service
up: ## Docker: starts the service <env=[dev|prod]>
@$(eval env ?= 'dev')
$(call showInfo,"Starting service\(s\)...")
@echo ""
@$(DOCKER_COMPOSE) up --remove-orphans --detach
$(call taskDone)

.PHONY: restart
restart: ## Docker: restarts the service
restart: ## Docker: restarts the service <env=[dev|prod]>
@$(eval env ?= 'dev')
$(call showInfo,"Restarting service\(s\)...")
@echo ""
@$(DOCKER_COMPOSE) restart
$(call taskDone)

.PHONY: down
down: ## Docker: stops the service
@$(DOCKER_COMPOSE) down --remove-orphans
down: ## Docker: stops the service <env=[dev|prod]>
@$(eval env ?= 'dev')
$(call showInfo,"Stopping service\(s\)...")
@echo ""
@$(DOCKER_COMPOSE) down $(DOCKER_COMPOSE_FILES) --remove-orphans
$(call taskDone)

.PHONY: logs
logs: ## Docker: exposes the service logs
@$(DOCKER_COMPOSE) logs
logs: ## Docker: exposes the service logs <env=[dev|prod]> <service=[app1|caddy]>
@$(eval env ?= 'dev')
@$(eval service ?= $(SERVICE_APP))
$(call showInfo,"Exposing service\(s\) logs...")
@echo ""
@$(DOCKER_COMPOSE) logs -f $(service)
$(call taskDone)

.PHONY: bash
bash: ## Docker: establish a bash session into main container
$(DOCKER_RUN_AS_USER) bash
.PHONY: shell
shell: ## Docker: establish a shell session into main container
@$(eval env ?= 'dev')
$(call showInfo,"Establishing a shell terminal with main service...")
@echo ""
@$(DOCKER_RUN_AS_USER) sh
$(call taskDone)

.PHONY: inspect
inspect: ## Docker: inspect the health for specific service <service=[app1|caddy]>
@$(eval service ?= $(SERVICE_APP))
$(call showInfo,"Inspecting the health for a specific service...")
@echo ""
@docker inspect --format "{{json .State.Health}}" $(service) | jq
@echo ""
$(call taskDone)

###
# CADDY
###

.PHONY: extract-caddy-certificate
extract-caddy-certificate: up ## Setup: extracts the Caddy Local Authority certificate
.PHONY: install-caddy-certificate
install-caddy-certificate: up ## Setup: extracts the Caddy Local Authority certificate
$(call showInfo,"Extracting Caddy Certificate Authority file...")
@echo ""
@echo "How to install [ $(YELLOW)Caddy Local Authority - 20XX ECC Root$(RESET) ] as a valid Certificate Authority"
$(call orderedList,1,"Copy the root certificate from Caddy Docker container")
@docker cp $(SERVICE_APP):/data/caddy/pki/authorities/local/root.crt ./caddy-root-ca-authority.crt
Expand All @@ -184,27 +206,49 @@ extract-caddy-certificate: up ## Setup: extracts the Caddy Local Authority certi
# APPLICATION
###

.PHONY: uninstall
uninstall: require-confirm ## Application: removes the PHP application
$(call showInfo,"Uninstalling PHP Application")
@find ./src -type f -delete
@rm -Rf ./src/*
$(call taskDone)

.PHONY: install-skeleton
install-skeleton: ## Application: installs PHP Skeleton
$(call showInfo,"Installing PHP Skeleton")
$(DOCKER_RUN_AS_USER) composer create-project alcidesrc/php-skeleton .
@$(eval env ?= 'dev')
$(call showInfo,"Installing PHP Skeleton...")
@echo ""
@$(DOCKER_RUN_AS_USER) composer create-project alcidesrc/php-skeleton .
$(call taskDone)

.PHONY: install-laravel
install-laravel: ## Application: installs Laravel
$(call showInfo,"Installing Laravel")
$(DOCKER_RUN_AS_USER) composer create-project laravel/laravel .
@$(eval env ?= 'dev')
$(call showInfo,"Installing Laravel...")
@echo ""
@$(DOCKER_RUN_AS_USER) composer create-project laravel/laravel .
$(call taskDone)

.PHONY: install-symfony
install-symfony: ## Application: installs Symfony
$(call showInfo,"Installing Symfony")
$(DOCKER_RUN_AS_USER) composer create-project symfony/skeleton .
@$(eval env ?= 'dev')
$(call showInfo,"Installing Symfony...")
@echo ""
@$(DOCKER_RUN_AS_USER) composer create-project symfony/skeleton .
$(call taskDone)

.PHONY: uninstall
uninstall: require-confirm ## Application: removes the PHP application
$(call showInfo,"Uninstalling PHP application...")
@rm -Rf ./src && mkdir ./src
$(call taskDone)

###
# MISCELANEOUS
###

.PHONY: open-website
open-website: ## Application: open the application website
$(call showInfo,"Opening web application...")
@echo ""
@xdg-open $(WEBSITE_URL)
@$(call showAlert,"Press Ctrl+C to resume your session")
$(call taskDone)

.PHONY: init
init: build install-caddy-certificate ## Application: initializes the application
$(call showInfo,"When ready just execute [ make open-website ] to visit the website with your preferred browser")
$(call taskDone)
60 changes: 26 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,15 @@

## Summary

This repository contains a _dockerized_ environment for building PHP applications based on **php:8.3.10-fpm-alpine** with Caddy support.
This repository contains a _dockerized_ environment for building PHP applications based on **php:8.3.12-fpm-alpine** with Caddy support.

### Highlights

- Unified environment to build <abbr title="Command Line Interface">CLI</abbr>, <u>web applications</u> and/or <u>micro-services</u> based on **PHP8**.
- Allows you to create an optimized **development environment** Docker image
- Allows you to create an optimized **production-ready** Docker image
- Using **Caddy** web server.
- PHP application is loaded into Caddy as a module.

### Infrastructure

| Docker Infrastructure | Value | Size |
| --------------------- | ----- | ----- |
| Containers | 1 | 201Mb |
| Services | 1 | N/A |
- Multi-stage Dockerfile to allows you to create an optimized **development** or **production-ready** Docker images
- Uses **Caddy webserver**.
- PHP-FPM is **managed by Caddy**.
- **Everything in one single Docker service**.



Expand All @@ -44,7 +37,7 @@ To use this repository you need:
- [Docker](https://www.docker.com/) - An open source containerization platform.
- [Git](https://git-scm.com/) - The free and open source distributed version control system.
- [Make](https://www.gnu.org/software/make/) - A command to automate the build/manage process.

- [jq](https://jqlang.github.io/jq/download/) - A lightweight and flexible command-line JSON processor.


------
Expand All @@ -53,14 +46,15 @@ To use this repository you need:

## Built with

| Type | Component | Description |
| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| Infrastructure | [Docker](https://www.docker.com/) | Containerization platform |
| Type | Component | Description |
| -------------- | -------------------------------------------------------- | ------------------------------------------------------------ |
| Infrastructure | [Docker](https://www.docker.com/) | Containerization platform |
| Service | [Caddy Server](https://caddyserver.com/) | Open source web server with automatic HTTPS written in Go |
| Service | [Caddy Supervisor](https://github.com/baldinof/caddy-supervisor) | A module to run and supervise background processes from Caddy |
| Service | [PHP-FPM](https://www.php.net/manual/en/install.fpm.php) | PHP with FastCGI Process Manager |
| Miscelaneous | [Bash](https://www.gnu.org/software/bash/) | Allows to create an interactive shell within containerized service |
| Miscelaneous | [Make](https://www.gnu.org/software/make/) | Allows to execute commands defined on a _Makefile_ |
| Service | [PHP-FPM](https://www.php.net/manual/en/install.fpm.php) | PHP with FastCGI Process Manager |
| Miscelaneous | [Bash](https://www.gnu.org/software/bash/) | Allows to create an interactive shell within containerized service |
| Miscelaneous | [Make](https://www.gnu.org/software/make/) | Allows to execute commands defined on a _Makefile_ |
| Miscelaneous | [jq](https://jqlang.github.io/jq/download/) | Allows to beautify the Docker inspections in JSON format |



Expand Down Expand Up @@ -101,41 +95,39 @@ $ mkdir -p ~/path/to/my-new-project && cd ~/path/to/my-new-project
$ git clone [email protected]:alcidesrc/dockerized-php-caddy.git .
```

### Quickstart

```bash
$ make init
```

### TL;DR

### Building the container
#### Building the container

```bash
$ make build
```

### Starting the container service
#### Starting the container service

```bash
$ make up
```

### Extracting Caddy Local Authority - 20XX ECC Root
#### Extracting Caddy Local Authority - 20XX ECC Root

```bash
$ make extract-caddy-certificate
$ make install-caddy-certificate
```



> [!WARNING]
>
> Just follow the instructions provided in order to properly install the certificate into your preferred browser.


### Accessing to web application
#### Accessing to web application

```bash
$ xdg-open https://website.localhost
$ make open-website
```

### Stopping the container service
#### Stopping the container service

```bash
$ make down
Expand Down
Loading

0 comments on commit 496826e

Please sign in to comment.