Skip to content

Commit

Permalink
Replace CDN-based assets with local assets
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 5, 2022
1 parent 22a899b commit db2136f
Show file tree
Hide file tree
Showing 1,607 changed files with 23,951 additions and 7 deletions.
141 changes: 141 additions & 0 deletions DOCKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Docker

This project relies in the following 3 application images:

* [php.package.health/nginx](docker/nginx.Dockerfile): Serves static assets (css, fonts etc) and routes dynamic traffic to php-fpm;
* [php.package.health/php-fpm](docker/php.Dockerfile): Serves dynamic traffic based in the application code;
* [php.package.health/php-cli](docker/php.Dockerfile): Used to perform maintenance tasks, such as database migration.

## Building the base images

From the root directory of this repository:

```bash
docker build --file docker/nginx.Dockerfile --tag php.package.health/nginx:latest .
docker build --file docker/php.Dockerfile --target fpm --tag php.package.health/php-fpm:latest .
docker build --file docker/php.Dockerfile --target cli --tag php.package.health/php-cli:latest .
```

## Creating a network

The network will be shared by the containers so they can communicate without exposing ports to the host machine.

```bash
docker network create php-package-health-network
```

## Starting the containers

Start the database container:

```bash
docker run \
--detach \
--env-file "$(pwd -P)/.env" \
--volume "$(pwd -P)/var/db":/var/lib/postgresql/data \
--network php-package-health-network \
--name pph-postgres \
postgres:14.2-alpine3.15
```

Start the PHP-FPM container:

```bash
docker run \
--rm \
--detach \
--env-file "$(pwd -P)/.env" \
--network php-package-health-network \
--name pph-php-fpm \
php.package.health/php-fpm:latest
```

Start the NGINX container:

```bash
docker run \
--rm \
--detach \
--network php-package-health-network \
--publish 8080:80/tcp \
--name pph-nginx \
php.package.health/nginx:latest
```

## Database Migration

Start the PHP-CLI container:

```bash
docker run \
--rm \
--interactive \
--tty \
--env-file "$(pwd -P)/.env" \
--network php-package-health-network \
--name pph-php-cli \
php.package.health/php-cli:latest \
sh
```

Check migration status:

```bash
../vendor/bin/phinx status --configuration ../phinx.php --environment "${PHP_ENV}"
```

Run migrations:

```bash
../vendor/bin/phinx migrate --configuration ../phinx.php --environment "${PHP_ENV}"
```

## Accessing the application

Open your browser and head to [http://localhost:8080/](http://localhost:8080/).

## Maintenance

The following sections are dedicated to maintenance tasks only.

### Application Console

Start the PHP-CLI container:

```bash
docker run \
--rm \
--interactive \
--tty \
--env-file "$(pwd -P)/.env" \
--network php-package-health-network \
--name pph-php-cli \
php.package.health/php-cli:latest \
sh
```

List console commands:

```bash
php console.php
```

### PostgreSQL Command Line Interface (PSQL)

Execute `sh` in the running `pph-postgres` container:

```bash
docker exec \
--interactive \
--tty \
pph-postgres \
sh
```

Run `psql`:

```bash
psql \
--username "${POSTGRES_USER}" \
--dbname "${POSTGRES_DB}"
```
30 changes: 30 additions & 0 deletions docker/nginx.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM nginx:mainline-alpine

# https://blog.packagecloud.io/eng/2017/02/21/set-environment-variable-save-thousands-of-system-calls/
ENV TZ=:/etc/localtime

# nginx settings
COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf

# static assets
COPY public/css/ /usr/share/nginx/html/css/
COPY public/fonts/ /usr/share/nginx/html/fonts/
COPY public/sprites/ /usr/share/nginx/html/sprites/
COPY public/svg/ /usr/share/nginx/html/svg/
COPY public/robots.txt /usr/share/nginx/html/

#============================================
# Healthcheck
#============================================
HEALTHCHECK --interval=1m30s --timeout=10s --retries=3 --start-period=40s CMD curl -f http://localhost/status || exit 1

#============================================
# Metadata
#============================================
LABEL org.opencontainers.image.authors="[email protected]" \
org.opencontainers.image.title="PHP-Package-Health-NGINX" \
org.opencontainers.image.url="https://github.com/package-health/php" \
org.opencontainers.image.vendor="Package Health"

WORKDIR /usr/share/nginx/html
29 changes: 29 additions & 0 deletions docker/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
server {
listen 80 default_server;

keepalive_timeout 70;

index index.php;

location / {
root /usr/share/nginx/html;
try_files $uri /index.php$is_args$args;
}

location ~ \.php {
root /var/www/html/public;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass php-fpm:9000;
}

location /status {
stub_status on;
allow 127.0.0.1;
deny all;
access_log off;
}
}
82 changes: 82 additions & 0 deletions docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
user nginx;
worker_processes auto;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
multi_accept on;
use epoll;
}

http {

##
# Basic Settings
##

# cache informations about FDs, frequently accessed files
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

# copies data between one FD and other from within the kernel
# faster then read() + write()
sendfile on;
# send headers in one peace, its better then sending them one by one
tcp_nopush on;
# don't buffer data sent, good for small data bursts in real time
tcp_nodelay on;
# server will close connection after this time
keepalive_timeout 30;
# number of requests client can make over keep-alive
keepalive_requests 100000;
types_hash_max_size 2048;
server_tokens off;

client_max_body_size 100m;
client_body_buffer_size 1m;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

# request timed out
client_body_timeout 10;

# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
}
Loading

0 comments on commit db2136f

Please sign in to comment.