# ms.gov_ked production image — PHP 8.1, dependencies baked in.
#
# NOTE: no `# syntax=` directive on purpose. It makes the builder load an
# external frontend image before parsing; this file uses nothing that needs
# it, and a bad/mismatched frontend can silently change COPY semantics
# (symptom we hit: directory sources flattened into WORKDIR). Kaniko ignores
# the directive anyway.
#
# KANIKO + LIBC CONSTRAINT (bit us 2026-09-09): Kaniko executes RUN by
# unpacking a stage's rootfs onto ITS OWN container filesystem. Our runner
# image is Ubuntu (glibc) and the official composer:2 image is Alpine
# (musl) — unpacking it made a franken rootfs (musl loader + glibc libs)
# and every RUN died with "Error relocating ... symbol not found". Rule
# that keeps CI builds alive: NEVER RUN anything in a musl-based stage.
# composer:2 now only DONATES its binary via COPY (COPY executes nothing);
# every RUN lives on php:8.1-cli (Debian glibc — same family as the runner
# image and the runtime stage). Local Docker never sees this problem
# because each stage's RUN gets its own container there.
#
# Why standalone instead of building on the Sail runtime: CI must build this
# without a prior composer install on the runner, and Sail's runtime
# Dockerfile lives inside vendor/ — chicken and egg. Same PHP version, only
# the extensions the app actually uses (soap for ΚΕΔ, sockets for the
# RabbitMQ library, pdo_mysql, opcache). `artisan serve` stands in for Sail's
# own dev server; one process per container, compose handles the rest.
#
# The private egov/rabbitmq-classes package is fetched over SSH (port 222)
# from Demokritos' GitLab at build time. The key arrives as a file named
# .ssh-build-key in the build context and is COPYed into the BUILDER STAGE
# ONLY — intermediate stages are never pushed, so the key cannot end up in
# the image in ECR. (BuildKit's --mount=type=secret would be marginally
# cleaner but is not usable under Kaniko, which is what CI builds with.)
#
#   CI:     the key comes from a File-type CI variable; the job drops it
#           into the context as .ssh-build-key (that name is .gitignore'd).
#           It MUST be type=File — as a plain Variable the key content gets
#           expanded onto the command line (and once was, into a job log).
#   local:  cp ~/.ssh/id_ed25519_demokritos .ssh-build-key
#           docker build -t ms-gov-laravel . && rm .ssh-build-key

# --- stage 0: composer binary donor — NO RUN in here (musl!), see above -----
FROM composer:2 AS composer-bin

# --- stage 1: vendor/ (the Demokritos SSH landmine lives here) ---------------
FROM php:8.1-cli AS vendor

# Composer itself, donated from the alpine image above. It is a PHAR (a PHP
# script), so it runs happily on this stage's glibc PHP.
COPY --from=composer-bin /usr/bin/composer /usr/local/bin/composer

# git + ssh for the Demokritos fetch; unzip for packagist dist installs.
# The composer image used to ship these — php:8.1-cli does not.
RUN apt-get update \
 && apt-get install -y --no-install-recommends git openssh-client unzip \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY composer.json composer.lock ./

# Resolve for the 8.1 runtime exactly. The two --ignore-platform-req flags
# mirror bin/install-deps.sh (this stage lacks ext-sockets; the real runtime
# builds it in the next stage).
RUN composer config platform.php 8.1.31

COPY .ssh-build-key /tmp/ssh_key

# --no-scripts: this stage has only composer.json + lock, no artisan, so the
# post-autoload-dump `artisan package:discover` cannot run here. The provider
# manifest it would write is rebuilt lazily on first boot (bootstrap/cache is
# www-data-writable in the runtime stage), so nothing is lost.
RUN chmod 600 /tmp/ssh_key \
 && mkdir -p ~/.ssh \
 && ssh-keyscan -p 222 gitlab.skel.iit.demokritos.gr >> ~/.ssh/known_hosts \
 && git config --global url."ssh://git@gitlab.skel.iit.demokritos.gr:222/".insteadOf "https://gitlab.skel.iit.demokritos.gr/" \
 && GIT_SSH_COMMAND="ssh -i /tmp/ssh_key -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts" \
    composer install --no-dev --no-scripts --no-interaction --no-progress \
        --ignore-platform-req=php --ignore-platform-req=ext-sockets --ignore-platform-req=ext-bcmath \
 && rm -f /tmp/ssh_key

# --- stage 2: runtime --------------------------------------------------------
FROM php:8.1-cli

RUN apt-get update \
 && apt-get install -y --no-install-recommends libxml2-dev \
 && rm -rf /var/lib/apt/lists/* \
 && docker-php-ext-install soap sockets pdo_mysql opcache bcmath \
 && mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

WORKDIR /var/www/html

# vendor first (cacheable), then source. The source copy is an EXPLICIT
# allowlist rather than `COPY . .` on purpose: the context also carries
# .ssh-build-key (stage 1 needs it), and a blanket copy would ship the key
# into this stage — the one that gets pushed. Anything new at repo root must
# be added here to reach the image.
#
# One source per line with an explicit destination. A multi-source COPY that
# mixes dirs and files flattened its directory sources on the dev machine's
# Docker daemon (every dir's children landed in WORKDIR) — `COPY dir/ ./dir/`
# is invariant under that broken semantics AND the standard one, so local
# builds and Kaniko CI builds cannot diverge. Per-dir layers also cache better.
COPY --from=vendor /app/vendor ./vendor
COPY app/ ./app/
COPY bin/ ./bin/
COPY bootstrap/ ./bootstrap/
COPY config/ ./config/
COPY database/ ./database/
COPY public/ ./public/
COPY resources/ ./resources/
COPY routes/ ./routes/
COPY storage/ ./storage/
COPY artisan composer.json composer.lock ./

# .dockerignore keeps host logs/caches out of the context, but its patterns
# match at every depth — the bare `.gitignore` line strips the keeper
# dotfiles inside storage/ and bootstrap/cache, and an emptied dir does not
# survive COPY. Recreate every dir Laravel writes to (error pages need
# storage/framework/views; provider manifests go to bootstrap/cache) instead
# of trusting the context.
RUN mkdir -p storage/framework/cache/data storage/framework/sessions \
             storage/framework/testing storage/framework/views storage/logs \
             bootstrap/cache \
 && chown -R www-data:www-data storage bootstrap/cache

USER www-data
EXPOSE 8080
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8080"]
