Skip to content

Commit

Permalink
🎉 Add PHP Image
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammed Hussein Karimi <[email protected]>
  • Loading branch information
mhkarimi1383 committed Oct 26, 2024
1 parent dac1e58 commit 58c5b6b
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Containers

Publicly available Container Images (published on `ghcr.io`)
214 changes: 214 additions & 0 deletions php/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
ARG PHP_VERSION
ARG DEBIAN_VERSION
ARG PHP_RUNTIME
ARG START_RUNTIME=${PHP_RUNTIME}
FROM docker.io/library/php:${PHP_VERSION}-${PHP_RUNTIME}-${DEBIAN_VERSION} AS builder

WORKDIR /var/www/html/

ENV DEBIAN_FRONTEND=noninteractive
ENV LC_ALL=C.UTF-8
ENV TERM=xterm-color
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV PATH /var/www/html/vendor/bin:/composer/vendor/bin:$PATH

RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
RUN pecl channel-update pecl.php.net

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
set -eux; \
apt-get update; \
apt-get install --no-install-recommends -y apt-transport-https && \
apt-get install -y --no-install-recommends \
apt-utils \
gnupg \
gosu \
git \
curl \
ca-certificates \
supervisor \
libmemcached-dev \
libz-dev \
libbrotli-dev \
libc-ares-dev \
libpq-dev \
libjpeg-dev \
libpng-dev \
libfreetype6-dev \
libssl-dev \
libwebp-dev \
libmcrypt-dev \
libonig-dev \
libzip-dev zip \
unzip \
libargon2-1 \
libidn2-0 \
libpcre2-8-0 \
libpcre3 \
libxml2 \
libzstd1 \
procps \
libcurl4-openssl-dev \
vim \
nano \
net-tools \
libmagickwand-dev \
libgmp-dev \
libxslt1-dev \
zlib1g-dev \
libicu-dev \
g++ \
libffi-dev

RUN export MAKE="make -j$(nproc)" && \
docker-php-source extract && \
pecl install -f \
igbinary \
redis \
grpc \
protobuf \
opentelemetry && \
rm -rf /tmp/pear && \
docker-php-ext-enable \
igbinary \
redis \
grpc \
protobuf \
opentelemetry && \
docker-php-ext-install -j$(nproc) \
bz2 \
calendar \
curl \
exif \
gettext \
gmp \
iconv \
pdo \
pdo_pgsql \
shmop \
soap \
sysvmsg \
sysvsem \
sysvshm \
xml \
xsl \
pdo_mysql \
mbstring \
opcache \
pcntl \
bcmath \
sockets && \
set -xe; \
docker-php-ext-configure zip && \
docker-php-ext-install -j$(nproc) zip && \
docker-php-ext-configure gd \
--prefix=/usr \
--with-jpeg \
--with-webp \
--with-freetype && \
docker-php-ext-install -j$(nproc) gd && \
docker-php-ext-install -j$(nproc) ffi && \
pecl install \
-D 'enable-openssl="yes" enable-http2="yes" enable-swoole-curl="yes" enable-cares="yes" enable-mysqlnd="yes" enable-swoole-pgsql="yes" enable-brotli="yes" enable-sockets="yes"' \
swoole && \
docker-php-ext-enable \
swoole && \
rm -rf /tmp/pear && \
docker-php-ext-configure intl && \
docker-php-ext-install -j$(nproc) intl && \
set -eux; \
docker-php-source delete; \
rm -rf /usr/local/bin/phpdbg; \
rm -rf /usr/local/lib/php/test && \
find /usr/local/bin /usr/local/sbin /usr/local/lib -type f -perm /0111 -exec strip --strip-all '{}' + || true

RUN set -eux; \
find /usr/local/lib/php /usr/local/bin /usr/local/sbin /usr/lib/x86_64-linux-gnu/ -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { print $(NF-1) }' \
| xargs -P $(nproc) -I {} dpkg-query --search {} | awk -F ":" '{print $1}' \
| uniq >> /PACKAGES
RUN find /usr/lib/x86_64-linux-gnu/ -type f \
| xargs -P $(nproc) -I {} dpkg-query --search {} | awk -F ":" '{print $1}' \
| uniq >> /PACKAGES

RUN mkdir -p /etc/apache2 /usr/lib/apache2

FROM docker.io/library/debian:${DEBIAN_VERSION}-slim
ARG START_RUNTIME

ENV DEBIAN_FRONTEND=noninteractive
ENV LC_ALL=C.UTF-8
ENV TERM=xterm-color
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV PATH /var/www/html/vendor/bin:/composer/vendor/bin:$PATH
ENV PHP_INI_DIR /usr/local/etc/php
ENV START_RUNTIME ${START_RUNTIME}
ENV DOCUMENT_ROOT=/var/www/html

COPY --from=builder /usr/local/include/ /usr/local/include/
COPY --from=builder /usr/local/lib/php/ /usr/local/lib/php/
COPY --from=builder /usr/local/bin /usr/local/bin
COPY --from=builder /usr/local/sbin /usr/local/sbin
COPY --from=builder /usr/local/etc /usr/local/etc
COPY --from=builder /PACKAGES /

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && \
apt-get install --no-install-recommends \
-y $(cat PACKAGES | sort | uniq) \
ca-certificates \
nginx \
vim \
tmux \
net-tools \
curl \
git \
gnupg2 \
supervisor \
unzip \
procps

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
if [ ${START_RUNTIME} = "apache" ]; then \
apt-get update && \
apt-get install -y apache2; \
fi

COPY --from=builder /etc/apache2 /etc/apache2
COPY --from=builder /usr/lib/apache2 /usr/lib/apache2

COPY --from=docker.io/library/composer:2 /usr/bin/composer /usr/local/bin/composer
RUN composer global require bamarni/symfony-console-autocomplete

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update \
&& . /etc/os-release \
&& echo "deb http://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
&& curl -sL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& apt-get update -y \
&& apt-get install -y --no-install-recommends --show-progress postgresql-client-16 postgis \
&& curl -sL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get install --no-install-recommends -y yarn

WORKDIR /var/www/html/

RUN mkdir -p /opt/parmincloud/php && \
rm -rf /var/www/html/*
COPY --chmod=0775 ./start.sh /opt/parmincloud/php/start-container
COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx-supervisord.conf /opt/parmincloud/php/nginx-supervisord.conf
COPY index.php /var/www/html/index.php

EXPOSE 80
STOPSIGNAL SIGQUIT

CMD ["/opt/parmincloud/php/start-container"]
13 changes: 13 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# All in One PHP Container

## Variants

* apache: Based on Apache and serves your code using Apache2
* nginx: Based on PHP FPM and NGINX
* fpm: Based on PHP FPM can be used as FactCGI server
* cli: Contains no builtin webserver (Swoole is pre-installed)

## ENVs

* `DOCUMENT_ROOT`: Defaults to `/var/www/html` and sets root dir of servers (works on apache and nginx)
> In laravel you might set it to `/var/www/html/public`
16 changes: 16 additions & 0 deletions php/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>ParminCloud PHP Container Image</title>
</head>
<body>
<h1>ParminCloud PHP Container Image</h1>
<p>
If you see this page it means that you have not copied the source code at <code><?php echo getcwd(); ?></code>.
<br />
<br />
Running PHP with <code><?php echo getenv("START_RUNTIME"); ?></code>
</p>
</body>
</html>

34 changes: 34 additions & 0 deletions php/nginx-supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid

[program:fpm]
command=php-fpm
autostart=true
stderr_logfile=/dev/stderr
stdout_logfile=/dev/stdout
autorestart=true
priority=999
exitcodes=0
startretries=3
stdout_maxbytes=0
stderr_maxbytes=0
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0

[program:nginx]
command=nginx
autostart=true
stderr_logfile=/dev/stderr
stdout_logfile=/dev/stdout
autorestart=true
priority=999
exitcodes=0
startretries=3
stdout_maxbytes=0
stderr_maxbytes=0
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0

52 changes: 52 additions & 0 deletions php/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
worker_processes auto;
daemon off;

error_log /dev/stderr;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;

log_format main $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$http_x_real_ip" "$http_x_client_ip";

access_log /dev/stdout main;

sendfile on;
keepalive_timeout 65;
server {
listen 80;

access_log /dev/stdout main;
error_log /dev/stderr;

charset utf-8;
client_max_body_size 0;

error_page 404 /index.php;
root /var/www/html;

index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~* ^/.+\.(jpg|jpeg|png|gif|ico|css|js|otf|ttf|eot|woff|svg|json|yml|yaml|woff2)$ {
expires max;
}

location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param X-Real-IP $http_x_real_ip;
fastcgi_param X-Forwarded-For $http_x_forwarded_for;
}
}
}
42 changes: 42 additions & 0 deletions php/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

case ${START_RUNTIME} in
fpm)
exec php-fpm
;;

cli)
exec php -a
;;

apache)
sed -ri -e "s!/var/www/html!${DOCUMENT_ROOT}!g" /etc/apache2/sites-available/*.conf
sed -ri -e "s!/var/www/!${DOCUMENT_ROOT}!g" /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
export APACHE_ENVVARS=/etc/apache2/envvars
. "$APACHE_ENVVARS"
for dir in "$APACHE_LOCK_DIR" "$APACHE_RUN_DIR" "$APACHE_LOG_DIR" "$APACHE_RUN_DIR/socks"; do
rm -rvf "$dir"
mkdir -p "$dir"
chown "$APACHE_RUN_USER:$APACHE_RUN_GROUP" "$dir"
chmod 1777 "$dir"
done
ln -sfT /dev/stderr "$APACHE_LOG_DIR/error.log"
ln -sfT /dev/stdout "$APACHE_LOG_DIR/access.log"
ln -sfT /dev/stdout "$APACHE_LOG_DIR/other_vhosts_access.log"
rm -f /var/run/apache2/apache2.pid
a2dismod mpm_event
a2enmod mpm_prefork
a2enconf docker-php
exec apache2 -DFOREGROUND
;;

nginx)
sed -ri -e "s!/var/www/html!${DOCUMENT_ROOT}!g" /etc/nginx/nginx.conf
exec /usr/bin/supervisord -c /opt/parmincloud/php/nginx-supervisord.conf
;;

*)
exec "$@"
;;
esac

0 comments on commit 58c5b6b

Please sign in to comment.