Skip to content

Commit

Permalink
Containerize application (#44)
Browse files Browse the repository at this point in the history
* Containerize application
  • Loading branch information
antonkomarev authored Feb 20, 2022
1 parent 0831122 commit ae00750
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .docker/nginx/app.ghpvc.80.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
server {
listen 80;
listen [::]:80;
server_name app.ghpvc.localhost;

root /app/public;
index index.php index.html;
access_log /dev/stdout;
error_log /dev/stderr info;

charset utf-8;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;

add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

server_tokens off;
client_max_body_size 100M;

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
30 changes: 30 additions & 0 deletions .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ----------------------
# The FPM base container
# ----------------------
FROM php:7.4-fpm-alpine AS dev

RUN apk add \
freetype-dev \
libpng-dev

# Configure php extensions
RUN docker-php-ext-configure gd --with-freetype

# Install php extensions
RUN docker-php-ext-install \
gd

# Cleanup apk cache and temp files
RUN rm -rf /var/cache/apk/* /tmp/*

# ----------------------
# Composer install step
# ----------------------

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# ----------------------
# The FPM production container
# ----------------------
FROM dev
85 changes: 85 additions & 0 deletions .docker/php/www.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 9000

; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives. With this process management, there will be
; always at least 1 children.
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; ondemand - no children are created at startup. Children will be forked when
; new requests will connect. The following parameter are used:
; pm.max_children - the maximum number of children that
; can be alive at the same time.
; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 5

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 2

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 1

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 3

; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
;pm.process_idle_timeout = 10s;

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
docker-app-exec = docker exec -it ghpvc-app /bin/sh -c

ssh-app: ## Connect to app container
docker exec -it ghpvc-app /bin/sh

setup-dev: ## Setup project for development
make start
make composer-install

start: ## Start application silently
docker-compose up -d

stop: ## Stop application
docker-compose down

restart: ## Restart the application
make stop
make start

composer-install: ## Install composer dependencies
$(docker-app-exec) 'composer install'

composer-update: ## Update composer dependencies
$(docker-app-exec) 'composer update $(filter-out $@,$(MAKECMDGOALS))'

copy-env: ## Copy .env.example as .env
cp .env.example .env

cleanup-docker: ## Remove old docker images
docker rmi $$(docker images --filter "dangling=true" -q --no-trunc)

run: ## Run command in the container
$(docker-app-exec) '$(filter-out $@,$(MAKECMDGOALS))'

help: # Show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help
35 changes: 35 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: "3.9"
services:
app:
container_name: ${PROJECT_NAME}-app
image: ghpvc-app
build:
context: ./
dockerfile: ./.docker/php/Dockerfile
restart: unless-stopped
working_dir: /app
volumes:
- ./:/app
- ./.docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
networks:
- ghpvc

nginx:
container_name: ${PROJECT_NAME}-nginx
image: nginx:1.21-alpine
restart: unless-stopped
depends_on:
- app
ports:
- "80:80"
environment:
VIRTUAL_HOST: app.ghpvc.localhost
volumes:
- ./.docker/nginx/app.ghpvc.80.conf:/etc/nginx/conf.d/app.ghpvc.80.conf:ro
- ./public:/app/public:ro
networks:
- ghpvc

networks:
ghpvc:
driver: bridge
2 changes: 1 addition & 1 deletion public/file-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

$httpUserAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';

if (!isset($_ENV['FILE_STORAGE_PATH']) || $_ENV['FILE_STORAGE_PATH'] === null) {
if (!isset($_ENV['FILE_STORAGE_PATH']) || $_ENV['FILE_STORAGE_PATH'] === '') {
$storagePath = $basePath . '/storage';
} else {
$storagePath = $_ENV['FILE_STORAGE_PATH'];
Expand Down

0 comments on commit ae00750

Please sign in to comment.