Skip to content

Commit

Permalink
Merge pull request #16 from OpenPlaceGuide/14-optimize-build
Browse files Browse the repository at this point in the history
#14 optimize build
  • Loading branch information
amenk authored Jul 22, 2024
2 parents d7a6cfe + c2c9314 commit 1bc2e83
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Dockerfile
vendor/
.git
public/build/
storage/app/repositories/*
storage/framework/*/*
storage/logs/*
public/assets/
storage/app/repositories/**
storage/framework/**/*
99 changes: 73 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,40 +1,87 @@
FROM composer:2.6.6 as composer
FROM composer:latest as composer

FROM php:8.1-apache-bookworm
COPY . /var/www/html
RUN cd /var/www/html && composer install --no-dev --no-scripts


FROM node:22 as node

COPY . /var/www/html
WORKDIR /var/www/html
RUN npm install && npm run build


FROM cgr.dev/chainguard/wolfi-base:latest

ARG PHP_VERSION=8.1

RUN apt-get update && apt-get install -y zip
RUN <<EOF
set -eo pipefail
apk add --no-cache \
php-${PHP_VERSION}-fpm
adduser -u 82 www-data -D
mkdir -p /var/www/html
chown www-data:www-data /var/www/html
EOF

RUN apt-get -y update \
&& apt-get install -y libicu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl
WORKDIR /var/www/html
ENV PHP_FPM_USER=www-data \
PHP_FPM_GROUP=www-data \
PHP_FPM_ACCESS_LOG=/proc/self/fd/2 \
PHP_FPM_LISTEN=[::]:9000 \
PHP_FPM_PM=dynamic \
PHP_FPM_PM_MAX_CHILDREN=5 \
PHP_FPM_PM_START_SERVERS=2 \
PHP_FPM_PM_MIN_SPARE_SERVERS=1 \
PHP_FPM_PM_MAX_SPARE_SERVERS=3 \
PHP_FPM_PM_MAX_REQUESTS=0 \
PHP_FPM_PM_STATUS_PATH=/-/fpm/status \
PHP_FPM_PING_PATH=/-/fpm/ping \
PHP_ERROR_REPORTING=E_ALL\
PHP_UPLOAD_MAX_FILESIZE=2M \
PHP_POST_MAX_SIZE=2M \
PHP_MAX_EXECUTION_TIME=30 \
PHP_MEMORY_LIMIT=128M \
PHP_SESSION_HANDLER=files \
PHP_SESSION_SAVE_PATH= \
PHP_SESSION_GC_PROBABILITY=1

RUN apt-get install -y \
nodejs npm \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libwebp-dev \
libxpm-dev \
zlib1g-dev && \
docker-php-ext-configure gd --enable-gd --with-webp --with-jpeg \
--with-xpm --with-freetype && \
docker-php-ext-install gd
COPY --link docker/rootfs /

RUN a2enmod rewrite

ENV LOG_CHANNEL=stderr
ENV PHP_FPM_LISTEN=/tmp/php-fpm.sock \
PHP_FPM_ACCESS_LOG=/dev/null

RUN sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!/var/www/html/public!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN <<EOF
set -eo pipefail
apk add --no-cache \
hivemind \
nginx
EOF

RUN sed -ri -e 's!^</!<Directory "/var/www/html/public">\nAllowOverride all\n</Directory>\n</!g' /etc/apache2/sites-available/*.conf
EXPOSE 8000


RUN apk add --no-cache zip php-8.1 php-8.1-intl php-8.1-gd php-8.1-cgi php-8.1-phar php-8.1-iconv php-8.1-mbstring php-8.1-openssl php-8.1-dom

#ENV LOG_CHANNEL=stderr

COPY . /var/www/html

RUN mkdir -p storage/framework/cache/data storage/framework/sessions storage/framework/views
RUN chown -R www-data:www-data /var/www/html/storage || true
RUN chown -R www-data:www-data /var/www/html/bootstrap/cache || true

COPY --from=composer /var/www/html/vendor /var/www/html/vendor
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN composer install --no-dev
RUN npm install
RUN npm run build
COPY --from=node /var/www/html/public/build /var/www/html/public/build

RUN composer dump-autoload

# RUN npm install
# RUN npm run build
# RUN mkdir -p public/assets \
# ln -s storage/app/repositories/opg-data-ethiopia/places public/assets/ethiopia

CMD ["/usr/bin/hivemind", "/etc/Procfile"]
# CMD ["php", "artisan"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,7 @@ docker compose up -d

Access http://localhost:3000


## Credits

* Parts of Wolfi-PHP based Dockerfile taken from https://github.com/shyim/wolfi-php/
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
}
},
"config": {
"platform": {
"php": "8.1.0"
},
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
Expand Down
5 changes: 4 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3.9"
services:
opg-pages:
image: opg-pages
Expand All @@ -8,6 +7,7 @@ services:
- ASSET_URL=http://localhost:3000/
volumes:
- ./storage/app/repositories/opg-data-ethiopia:/var/www/html/storage/app/repositories/opg-data-ethiopia/
- ./storage/app/repositories/opg-data-ethiopia/places:/var/www/html/public/assets/ethiopia
web:
image: osmapp
ports:
Expand Down
9 changes: 9 additions & 0 deletions docker-test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM cgr.dev/chainguard/wolfi-base:latest

WORKDIR /var/www/html

COPY . .

RUN apk add --no-cache php-8.1

CMD [ "php", "./test_error.php" ]
2 changes: 2 additions & 0 deletions docker/rootfs/etc/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nginx: /usr/sbin/nginx
php-fpm: /usr/sbin/php-fpm
46 changes: 46 additions & 0 deletions docker/rootfs/etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
worker_processes auto;
pid /tmp/nginx.pid;
daemon off;
user www-data;
error_log /dev/fd/2 error;

events {
worker_connections 1024;
}

http {
client_body_temp_path /tmp/client_body_temp;
proxy_temp_path /tmp/proxy_temp;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
access_log /dev/fd/1;

server {
listen 80;
server_name localhost;

root /var/www/html/public;
index index.php;
include /etc/nginx/mime.types;

location / {
try_files $uri /index.php$is_args$args;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 300s;
client_body_buffer_size 128k;
fastcgi_pass unix:/tmp/php-fpm.sock;
}

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
}
11 changes: 11 additions & 0 deletions docker/rootfs/etc/php/conf.d/default.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}
post_max_size = ${PHP_POST_MAX_SIZE}
max_execution_time = ${PHP_MAX_EXECUTION_TIME}
memory_limit = ${PHP_MEMORY_LIMIT}

session.save_handler = ${PHP_SESSION_HANDLER}
session.save_path = ${PHP_SESSION_SAVE_PATH}
session.gc_probability = ${PHP_SESSION_GC_PROBABILITY}

error_log = /dev/fd/2
log_errors = On
16 changes: 16 additions & 0 deletions docker/rootfs/etc/php/php-fpm.d/zz-b-default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[www]
user = ${PHP_FPM_USER}
group = ${PHP_FPM_GROUP}
access.log = ${PHP_FPM_ACCESS_LOG}
listen = ${PHP_FPM_LISTEN}
listen.owner = ${PHP_FPM_USER}
listen.group = ${PHP_FPM_GROUP}
pm = ${PHP_FPM_PM}
pm.max_children = ${PHP_FPM_PM_MAX_CHILDREN}
pm.start_servers = ${PHP_FPM_PM_START_SERVERS}
pm.min_spare_servers = ${PHP_FPM_PM_MIN_SPARE_SERVERS}
pm.max_spare_servers = ${PHP_FPM_PM_MAX_SPARE_SERVERS}
pm.max_requests = ${PHP_FPM_PM_MAX_REQUESTS}

[global]
error_log = /proc/self/fd/2
2 changes: 2 additions & 0 deletions docker/rootfs/var/www/html/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
phpinfo();

0 comments on commit 1bc2e83

Please sign in to comment.