# 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.12.0-v0.2.0@sha256:96c500319b991cfd130f641657255b3d4febdd7cf8d01ce9090dd0d19e21a37a
# 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.3-node26-v0.2.0@sha256:1ed46620b7ed93a3295df72bee3041226fe2a70cae00e218c0a2ebebd3c51377
# https://pecl.php.net/package/uv
ARG PHP_UV_VERSION=0.3.1

# Add user/group
ARG USER_ID=9999
ARG GROUP_ID=9999

# Set by the build workflow
ARG COOLIFY_VERSION
ARG SOURCE_DATE_EPOCH
ARG CREATED
ARG GIT_SHA

# =================================================================
# Stage 1: Composer dependencies
# =================================================================
FROM ${BASE_IMAGE} AS base

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
ARG NGINX_VERSION
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

ARG USER_ID
ARG GROUP_ID

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

WORKDIR /var/www/html
COPY --chown=www-data:www-data composer.json composer.lock ./
RUN --mount=type=cache,target=/tmp/cache \
    COMPOSER_CACHE_DIR=/tmp/cache composer install --no-dev --no-interaction --no-plugins --no-scripts --prefer-dist

USER www-data

# =================================================================
# Stage 2: Frontend assets compilation
# =================================================================
FROM ${AUBE_IMAGE} AS static-assets

WORKDIR /app
COPY package.json aube-lock.yaml aube-workspace.yaml ./
RUN --mount=type=cache,target=/root/.local/share/aube \
    aube install --dev --frozen-lockfile
COPY . .
RUN aube run --no-install build

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

RUN apk add --no-cache make g++ python3 && \
    strip /usr/local/bin/node
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 && \
    rm -rf node_modules/.aube/node-addon-api@* \
        node_modules/.aube/node_modules/node-addon-api \
        node_modules/.aube/node-pty@*/node_modules/node-addon-api && \
    cd node_modules/node-pty && \
    rm -rf binding.gyp deps prebuilds scripts src third_party typings && \
    find build -type f ! -name pty.node -delete
COPY docker/coolify-terminal/terminal-server.js /terminal/terminal-server.js
COPY docker/coolify-terminal/terminal-utils.js /terminal/terminal-utils.js

# =================================================================
# Stage 4: cloudflared
# =================================================================
FROM ${CLOUDFLARED_IMAGE} AS cloudflared

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

ARG USER_ID
ARG GROUP_ID
ARG POSTGRES_VERSION
ARG NGINX_VERSION
ARG PHP_UV_VERSION
ARG COOLIFY_VERSION
ARG CI=true

ENV COOLIFY_VERSION=${COOLIFY_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 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 --mount=type=cache,target=/var/cache/apk \
    apk upgrade && \
    apk add --no-cache \
    postgresql${POSTGRES_VERSION}-client \
    libstdc++ \
    libatomic \
    openssh-client \
    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 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/production/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=1
# v5 defaults this to 0. Keep 1: app:init rebuilds the Laravel caches while php-fpm already runs.
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS=1
# v5 defaults to 256 MB with 32 MB interned strings. Every PHP CLI process has its own cache, so keep it small.
ENV PHP_OPCACHE_MEMORY_CONSUMPTION=128
ENV PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16
RUN install -d -m 700 -o www-data -g www-data /var/www/opcache

# Configure entrypoint
COPY --chmod=755 docker/production/entrypoint.d/ /etc/entrypoint.d

# Copy application files from previous stages
COPY --from=base --chown=www-data:www-data /var/www/html/vendor ./vendor
COPY --from=static-assets --chown=www-data:www-data /app/public/build ./public/build

# Copy application source code
COPY --chown=www-data:www-data composer.json composer.lock ./
COPY --chown=www-data:www-data app ./app
COPY --chown=www-data:www-data bootstrap ./bootstrap
COPY --chown=www-data:www-data config ./config
COPY --chown=www-data:www-data database ./database
COPY --chown=www-data:www-data lang ./lang
COPY --chown=www-data:www-data public ./public
COPY --chown=www-data:www-data routes ./routes
COPY --chown=www-data:www-data storage ./storage
COPY --chown=www-data:www-data templates ./templates
COPY --chown=www-data:www-data resources/views ./resources/views
COPY --chown=www-data:www-data resources/image-scan ./resources/image-scan
COPY --chown=www-data:www-data artisan artisan
COPY --chown=www-data:www-data openapi.yaml ./openapi.yaml
COPY --chown=www-data:www-data changelogs/ ./changelogs/
COPY --chown=www-data:www-data release.json upstream.json ./

RUN composer dump-autoload

# 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/production/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/production/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

ARG BASE_IMAGE
ARG DISTRO
ARG CREATED
ARG GIT_SHA

LABEL org.opencontainers.image.title="Coolify" \
    org.opencontainers.image.description="An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers." \
    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://coolify.io/docs" \
    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="runtime" \
    io.coolify.image.flavor="" \
    io.coolify.image.shell="busybox" \
    io.coolify.image.package-manager="apk"

# Switch to non-root user
USER www-data
