# Versions
# https://hub.docker.com/r/serversideup/php/tags?name=8.4-fpm-nginx-alpine
ARG BASE_IMAGE=docker.io/serversideup/php:8.4-fpm-nginx-alpine-v5.2.2@sha256:04c4f61d2d5d8d9f01f34cfbf137606bbdf8f082dcafc4d4162e62401ecad976
ARG DISTRO=alpine-3.24
# https://hub.docker.com/r/peakimages/cloudflared/tags
ARG CLOUDFLARED_IMAGE=docker.io/peakimages/cloudflared:2026.11.2-v0.1.4@sha256:66a2432a4d4f848d5bb9d88bb214202ed268236ced98bfa757e7d52d1df8b7a2
# https://www.postgresql.org/support/versioning/
# Note: We are using version 18 of the postgres client (while still using postgres 15 for the postgres server) as version 15 has been removed from Alpine 3.23+ https://pkgs.alpinelinux.org/packages?name=postgresql*-client&branch=v3.23&repo=&arch=x86_64&origin=&flagged=&maintainer=
ARG POSTGRES_VERSION=18
# https://nginx.org/en/linux_packages.html
ARG NGINX_VERSION=1.31.10-r1
# https://hub.docker.com/r/peakimages/aube/tags?name=node26
ARG AUBE_IMAGE=docker.io/peakimages/aube:2.11.0-node26-v0.1.0@sha256:77780eeb44d92b07585af01d089b678e2410b513014d0d136749d91dc7aa4e31
# https://pecl.php.net/package/uv
ARG PHP_UV_VERSION=0.3.1

# =================================================================
# Terminal dependencies (match the development Node.js ABI and libc)
# =================================================================
FROM ${AUBE_IMAGE} AS terminal-dependencies

RUN apk add --no-cache make g++ python3
WORKDIR /terminal
COPY package.json aube-lock.yaml aube-workspace.yaml ./
RUN --mount=type=cache,target=/root/.local/share/aube \
    aube install --prod --frozen-lockfile
COPY docker/coolify-terminal/terminal-server.js /terminal/terminal-server.js
COPY docker/coolify-terminal/terminal-utils.js /terminal/terminal-utils.js

# =================================================================
# Get MinIO client
# =================================================================
FROM ghcr.io/coollabsio/mx:0.2.1 AS minio-client

# =================================================================
# Get cloudflared
# =================================================================
FROM ${CLOUDFLARED_IMAGE} AS cloudflared

# =================================================================
# Final Stage: Production image
# =================================================================
FROM ${BASE_IMAGE}

ARG USER_ID
ARG GROUP_ID
ARG TARGETARCH
ARG POSTGRES_VERSION
ARG NGINX_VERSION
ARG PHP_UV_VERSION

WORKDIR /var/www/html

USER root

# Install patched Nginx from the official nginx.org Alpine repository
# nginx.org no longer serves this key for download; it still signs the Alpine packages.
COPY docker/nginx/nginx_signing.rsa.pub /etc/apk/keys/nginx_signing.rsa.pub
RUN set -eux; \
    apk add --no-cache ca-certificates curl; \
    NGINX_ALPINE_VERSION="$(egrep -o '^[0-9]+\.[0-9]+' /etc/alpine-release)"; \
    NGINX_REPOSITORY="https://nginx.org/packages/mainline/alpine/v${NGINX_ALPINE_VERSION}/main"; \
    sed -i 's|http://nginx.org/packages|https://nginx.org/packages|g' /etc/apk/repositories; \
    grep -qxF "@nginx ${NGINX_REPOSITORY}" /etc/apk/repositories || echo "@nginx ${NGINX_REPOSITORY}" >> /etc/apk/repositories; \
    apk add --no-cache --upgrade "nginx@nginx=${NGINX_VERSION}"; \
    rm -f /etc/nginx/nginx.conf /etc/nginx/conf.d/default.conf; \
    nginx -v

RUN docker-php-serversideup-set-id www-data $USER_ID:$GROUP_ID && \
    docker-php-serversideup-set-file-permissions --owner $USER_ID:$GROUP_ID --service nginx

# Install PostgreSQL repository and keys
RUN apk upgrade --no-cache && \
    apk add --no-cache gnupg && \
    mkdir -p /usr/share/keyrings && \
    curl -fSsL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor > /usr/share/keyrings/postgresql.gpg


# Install system dependencies
RUN apk add --no-cache \
    postgresql${POSTGRES_VERSION}-client \
    libstdc++ \
    libatomic \
    openssh-client \
    openssl \
    git \
    git-lfs \
    jq \
    lsof \
    procps \
    vim

# Install PHP extensions
# uv: Reverb uses an ext-uv event loop instead of stream_select(), which fails at 1024 open files (FD_SETSIZE)
RUN install-php-extensions sockets uv-${PHP_UV_VERSION}

# Configure shell aliases
RUN echo "alias ll='ls -al'" >> /etc/profile && \
    echo "alias a='php artisan'" >> /etc/profile && \
    echo "alias logs='tail -F storage/logs/laravel-\$(date -u +%F).log'" >> /etc/profile

# Install cloudflared, used by SSH connections through Cloudflare Tunnel
COPY --from=cloudflared /usr/local/bin/cloudflared /usr/local/bin/cloudflared

# Configure PHP
COPY docker/development/etc/php/conf.d/zzz-custom-php.ini /usr/local/etc/php/conf.d/zzz-custom-php.ini
COPY docker/production/etc/php-fpm.d/zzzz-coolify-listen.conf /usr/local/etc/php-fpm.d/zzzz-coolify-listen.conf
ENV PHP_OPCACHE_ENABLE=0

# Install terminal WebSocket server dependencies
COPY --from=terminal-dependencies /usr/local/bin/node /usr/local/bin/node
COPY --from=terminal-dependencies --chown=www-data:www-data /terminal /terminal

# Configure the Nginx site of the base image
ENV NGINX_CLIENT_MAX_BODY_SIZE=2048M
ENV NGINX_FASTCGI_BUFFERS="16 16k"
ENV NGINX_FASTCGI_BUFFER_SIZE=32k
ENV TRUSTED_PROXY=off

# Configure Nginx and S6 overlay
COPY docker/development/etc/nginx/conf.d/custom.conf /etc/nginx/conf.d/custom.conf
COPY docker/production/etc/nginx/server-opts.d/coolify.conf /etc/nginx/server-opts.d/coolify.conf
# Drop HSTS, which would force HTTPS on every subdomain of the dashboard domain, and the
# year-long caching of static files, which keeps files without a hash in their name stale after an upgrade
RUN sed -i '/Strict-Transport-Security/d' /etc/nginx/server-opts.d/security.conf && \
    rm /etc/nginx/server-opts.d/performance.conf
COPY --chmod=755 docker/development/etc/s6-overlay/ /etc/s6-overlay/

RUN mkdir -p /etc/nginx/conf.d && \
    chown -R www-data:www-data /etc/nginx && \
    chmod -R 755 /etc/nginx

# Install MinIO client
COPY --from=minio-client /usr/bin/mc /usr/bin/mc
RUN chmod +x /usr/bin/mc

ARG BASE_IMAGE
ARG DISTRO
ARG COOLIFY_VERSION
ARG CREATED
ARG GIT_SHA

LABEL org.opencontainers.image.title="Coolify Development" \
    org.opencontainers.image.description="The development image of Coolify, it runs the checkout that Docker Compose mounts into it." \
    org.opencontainers.image.version="${COOLIFY_VERSION}" \
    org.opencontainers.image.revision="${GIT_SHA}" \
    org.opencontainers.image.created="${CREATED}" \
    org.opencontainers.image.licenses="Apache-2.0" \
    org.opencontainers.image.authors="coolLabs" \
    org.opencontainers.image.vendor="coolLabs" \
    org.opencontainers.image.url="https://coolify.io" \
    org.opencontainers.image.documentation="https://github.com/coollabsio/coolify/blob/main/CONTRIBUTING.md" \
    org.opencontainers.image.source="https://github.com/coollabsio/coolify" \
    org.opencontainers.image.base.name="${BASE_IMAGE%@*}" \
    org.opencontainers.image.base.digest="${BASE_IMAGE#*@}" \
    io.coolify.image.version="${COOLIFY_VERSION}" \
    io.coolify.image.distro="${DISTRO}" \
    io.coolify.image.variant="development" \
    io.coolify.image.flavor="" \
    io.coolify.image.shell="busybox" \
    io.coolify.image.package-manager="apk"

# Stay as root for s6 init so bind-mounted workspaces work even when the host
# tree is root-owned (CI, Jean, rootful Docker). PHP-FPM/nginx still run as
# www-data via their service configs. Do NOT set USER www-data here — that
# makes init-setup (composer install) fail with "vendor could not be created".
USER root
