Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

Revert "perf(docker): shrink monolith image and speed up container CI" (#62502)

James Greenhill9dec2fdd5cd9
diff --git a/.dockerignore b/.dockerignore
index aa50fc754719..625099bbe752 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -94,15 +94,3 @@ rust/target
!docker-compose.base.yml
!docker-compose.dev.yml
!docker
-
-# Test / story / spec sources are never needed to build or run any image. Excluding them keeps them
-# out of the COPYed layers and stops test-only edits from busting the collectstatic + frontend-build
-# cache. Shared by all Dockerfiles — no image build (main, node, llm-analytics, etc.) needs them.
-products/**/test_*.py
-products/**/*_test.py
-products/**/*.test.*
-products/**/*.spec.*
-products/**/*.stories.*
-frontend/src/**/*.test.*
-frontend/src/**/*.spec.*
-frontend/src/**/*.stories.*
diff --git a/.github/actions/build-n-cache-image/action.yml b/.github/actions/build-n-cache-image/action.yml
index e684a58a4b50..244ecbe49918 100644
--- a/.github/actions/build-n-cache-image/action.yml
+++ b/.github/actions/build-n-cache-image/action.yml
@@ -54,6 +54,9 @@ runs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
+ - name: Set up QEMU
+ uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
+
- name: Set up Depot CLI
uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1
diff --git a/.github/workflows/container-images-cd.yml b/.github/workflows/container-images-cd.yml
index 07fac32305cf..9ef287641d00 100644
--- a/.github/workflows/container-images-cd.yml
+++ b/.github/workflows/container-images-cd.yml
@@ -41,6 +41,9 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
+ - name: Set up QEMU
+ uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
+
- name: Set up Depot CLI
uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1
@@ -61,7 +64,6 @@ jobs:
continue-on-error: true
uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
with:
- context: . # match the CI build's context so master reuses the shared Depot layer cache
buildx-fallback: false # the fallback is so slow it's better to just fail
push: true
tags: ${{ steps.docker-meta.outputs.tags }}
@@ -69,6 +71,7 @@ jobs:
platforms: linux/arm64,linux/amd64
build-args: |
COMMIT_HASH=${{ github.sha }}
+ BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
secrets: |
posthog_upload_sourcemaps_cli_api_key=${{ secrets.POSTHOG_UPLOAD_SOURCEMAPS_CLI_API_KEY }}
@@ -104,7 +107,6 @@ jobs:
continue-on-error: true
uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
with:
- context: . # match the CI build's context so master reuses the shared Depot layer cache
buildx-fallback: false # the fallback is so slow it's better to just fail
push: true
tags: ${{ steps.docker-meta.outputs.tags }}
@@ -112,6 +114,7 @@ jobs:
platforms: linux/arm64,linux/amd64
build-args: |
COMMIT_HASH=${{ github.sha }}
+ BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
secrets: |
posthog_upload_sourcemaps_cli_api_key=${{ secrets.POSTHOG_UPLOAD_SOURCEMAPS_CLI_API_KEY }}
diff --git a/Dockerfile b/Dockerfile
index 2dfe8cfe232a..373524551ead 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -4,7 +4,7 @@
# PostHog has sunset support for self-hosted K8s deployments.
# See: https://posthog.com/blog/sunsetting-helm-support-posthog
#
-# Note: PostHog Cloud uses this same image (re-tagged to ECR posthog-cloud); there is no separate Dockerfile.cloud.
+# Note: for PostHog Cloud remember to update ‘Dockerfile.cloud’ as appropriate.
#
# The stages are used to:
#
@@ -62,31 +62,22 @@ ARG COMMIT_HASH
COPY --from=frontend-build /code/frontend/dist /code/frontend/dist
-# Upload sourcemaps to error tracking, recording whether it GENUINELY succeeded so the later strip
-# only deletes the .map files when an uploaded copy exists. The build never fails on a sourcemap
-# problem (missing secret, CLI download / network / auth failure): in that case the status is
-# "retained" and the .map files are kept in the image. Uses explicit && chaining rather than `set -e`,
-# which bash ignores inside a `||`-guarded subshell — any failing link drops us into the retained branch.
RUN --mount=type=secret,id=posthog_upload_sourcemaps_cli_api_key \
- if ( \
- [ -f /run/secrets/posthog_upload_sourcemaps_cli_api_key ] && \
- apt-get update && \
- apt-get install -y --no-install-recommends ca-certificates curl && \
- curl --proto '=https' --tlsv1.2 -LsSf https://download.posthog.com/cli | sh && \
- export PATH="/root/.posthog:$PATH" && \
- export POSTHOG_CLI_TOKEN="$(cat /run/secrets/posthog_upload_sourcemaps_cli_api_key)" && \
- export POSTHOG_CLI_ENV_ID=2 && \
- posthog-cli sourcemap process \
- --directory /code/frontend/dist \
- --public-path-prefix /static \
- --project posthog \
- --version "${COMMIT_HASH:-unknown}" \
- ); then \
- echo uploaded > /tmp/.sourcemaps-status; \
- else \
- echo "WARNING: sourcemaps not uploaded (no secret or upload failed); .map files will be retained in the image" >&2; \
- echo retained > /tmp/.sourcemaps-status; \
- fi && \
+ ( \
+ if [ -f /run/secrets/posthog_upload_sourcemaps_cli_api_key ]; then \
+ apt-get update && \
+ apt-get install -y --no-install-recommends ca-certificates curl && \
+ curl --proto '=https' --tlsv1.2 -LsSf https://download.posthog.com/cli | sh && \
+ export PATH="/root/.posthog:$PATH" && \
+ export POSTHOG_CLI_TOKEN="$(cat /run/secrets/posthog_upload_sourcemaps_cli_api_key)" && \
+ export POSTHOG_CLI_ENV_ID=2 && \
+ posthog-cli --no-fail sourcemap process \
+ --directory /code/frontend/dist \
+ --public-path-prefix /static \
+ --project posthog \
+ --version "${COMMIT_HASH:-unknown}"; \
+ fi \
+ ) || true && \
touch /tmp/.sourcemaps-processed
@@ -109,16 +100,6 @@ RUN --mount=type=cache,id=pnpm,target=/tmp/pnpm-store-v24 \
NODE_OPTIONS="--max-old-space-size=4096" CI=1 pnpm --filter=@posthog/plugin-transpiler... install --frozen-lockfile --store-dir /tmp/pnpm-store-v24 && \
NODE_OPTIONS="--max-old-space-size=4096" bin/turbo --filter=@posthog/plugin-transpiler build
-# The transpiler bundle externalizes @babel/standalone (its only external runtime require — a
-# self-contained 24MB package with no deps). Materialize it as real files inside the transpiler's
-# own node_modules, replacing the pnpm symlink that pointed into the root node_modules. The final
-# image then carries just this package instead of the entire ~469MB root /code/node_modules.
-RUN cd /code/common/plugin_transpiler && \
- BABEL_REAL=$(node -e "process.stdout.write(require('path').dirname(require.resolve('@babel/standalone/package.json')))") && \
- rm -rf node_modules/@babel/standalone && \
- mkdir -p node_modules/@babel && \
- cp -rL "$BABEL_REAL" node_modules/@babel/standalone
-
#
# ---------------------------------------------------------
@@ -177,18 +158,6 @@ COPY --from=frontend-build /code/frontend/src/products.json /code/frontend/src/p
# Make sure we build the static files
RUN SKIP_SERVICE_VERSION_REQUIREMENTS=1 STATIC_COLLECTION=1 DATABASE_URL='postgres:///' REDIS_URL='redis:///' python manage.py collectstatic --noinput
-# Strip JS sourcemaps (~2.8GB) from the artifacts that ship in the final image — but ONLY when the
-# isolated sourcemap-upload stage confirmed a real upload to error tracking (status "uploaded"). If the
-# upload failed or no secret was provided, the .map files are kept so the only remaining copy isn't
-# lost. Done here (not in the final stage) so the bytes never enter the COPYed layers.
-COPY --from=sourcemap-upload /tmp/.sourcemaps-status /tmp/.sourcemaps-status
-RUN if [ "$(cat /tmp/.sourcemaps-status)" = uploaded ]; then \
- echo "sourcemaps uploaded — stripping .map files from the image"; \
- find /code/staticfiles /code/frontend/dist -name '*.map' -delete; \
- else \
- echo "sourcemaps NOT uploaded — retaining .map files in the image"; \
- fi
-
#
@@ -266,30 +235,29 @@ RUN apt-get update && \
# Install OS runtime dependencies.
# Note: please add in this stage runtime dependences only!
-# Runtime-only shared libs: lxml/xmlsec are compiled --no-binary in the build stage (which keeps
-# its own -dev headers), so the final image needs the runtime .so, not the -dev headers/static libs.
-# libxmlsec1-openssl provides the OpenSSL crypto backend that libxmlsec1-dev used to pull in.
RUN apt-get update && \
apt-get install -y --no-install-recommends --allow-downgrades \
"chromium" \
"chromium-driver" \
"gettext-base" \
- "libpq5" \
+ "libpq-dev" \
"libxmlsec1=1.2.37-2" \
- "libxmlsec1-openssl=1.2.37-2" \
+ "libxmlsec1-dev=1.2.37-2" \
"libxml2" \
+ "libssl-dev=3.0.19-1~deb12u2" \
"libssl3=3.0.19-1~deb12u2" \
"libjemalloc2" \
&& \
rm -rf /var/lib/apt/lists/*
-# Note: no MS SQL ODBC driver is installed — the data-warehouse MSSQL source uses pymssql, which
-# bundles FreeTDS in its wheel and does not use msodbcsql18/unixodbc (there is no pyodbc in the tree).
+# Install MS SQL dependencies
+RUN curl https://packages.microsoft.com/keys/microsoft.asc | tee /etc/apt/trusted.gpg.d/microsoft.asc && \
+ curl https://packages.microsoft.com/config/debian/11/prod.list | tee /etc/apt/sources.list.d/mssql-release.list && \
+ apt-get update && \
+ ACCEPT_EULA=Y apt-get install -y msodbcsql18 && \
+ rm -rf /var/lib/apt/lists/*
-# Install Node.js 24.13.0 for standalone scripts with architecture detection and verification.
-# Only the `node` binary is used at runtime (the plugin transpiler subprocess), so npm/npx/corepack/
-# headers are stripped after install. Note: the dev-only `create_channel_definitions_file` management
-# command shells out to `npx prettier` to regenerate a checked-in file; it is not run in this image.
+# Install Node.js 24.13.0 for standalone scripts with architecture detection and verification
ENV NODE_VERSION 24.13.0
RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
@@ -328,7 +296,7 @@ RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
&& rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs \
&& node --version \
- && rm -rf /usr/local/lib/node_modules/npm /usr/local/bin/npm /usr/local/bin/npx /usr/local/bin/corepack /usr/local/include/node \
+ && npm --version \
&& rm -rf /tmp/*
# Install and use a non-root user.
@@ -348,19 +316,21 @@ COPY --from=posthog-build --chown=posthog:posthog /python-runtime /python-runtim
ENV PATH=/python-runtime/bin:$PATH \
PYTHONPATH=/python-runtime
-# Install the system dependencies (fonts, shared libs) that headless Chromium screenshots need,
-# but NOT the Playwright-bundled Chromium binary. Heatmap screenshots drive the system chromium
-# (CHROME_BIN) — the same engine the selenium image-export path uses — so we avoid shipping a
-# second, duplicate ~170MB browser. (See _launch_local_browser in heatmap_screenshot.py.)
+# Install Playwright Chromium browser for heatmap screenshots (as root for system deps)
+# Use cache mount for browser binaries to avoid re-downloading on every build
USER root
-RUN /python-runtime/bin/python -m playwright install-deps chromium && \
- rm -rf /var/lib/apt/lists/*
+ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
+RUN --mount=type=cache,id=playwright-browsers,target=/tmp/playwright-cache,sharing=locked \
+ PLAYWRIGHT_BROWSERS_PATH=/tmp/playwright-cache \
+ /python-runtime/bin/python -m playwright install --with-deps chromium && \
+ mkdir -p /ms-playwright && \
+ cp -r /tmp/playwright-cache/* /ms-playwright/ && \
+ chown -R posthog:posthog /ms-playwright
USER posthog
-# frontend/dist is read at runtime (Django template DIR in settings/web.py + the array.js disk
-# fallback in js_snippet_versioning.py), so this COPY is load-bearing. Sourced from posthog-build,
-# where the JS sourcemaps have already been stripped.
-COPY --from=posthog-build --chown=posthog:posthog /code/frontend/dist /code/frontend/dist
+# Copy the frontend assets from the frontend-build stage.
+# TODO: this copy should not be necessary, we should remove it once we verify everything still works.
+COPY --from=frontend-build --chown=posthog:posthog /code/frontend/dist /code/frontend/dist
# Ensure sourcemap-upload stage runs (the file itself is not needed in the final image).
COPY --from=sourcemap-upload /tmp/.sourcemaps-processed /tmp/.sourcemaps-processed
@@ -371,9 +341,9 @@ COPY --from=frontend-build --chown=posthog:posthog /code/frontend/src/products.j
# Copy the GeoLite2-City database from the fetch-geoip-db stage.
COPY --from=fetch-geoip-db --chown=posthog:posthog /code/share/GeoLite2-City.mmdb /code/share/GeoLite2-City.mmdb
-# Copy plugin transpiler (used by Django for site destinations/apps). The transpiler dist is a
-# self-contained esbuild bundle (build.mjs uses bundle:true), so only its own dist + node_modules
-# are needed at runtime — the full root /code/node_modules COPY was redundant.
+# Copy plugin transpiler (used by Django for site destinations/apps).
+# pnpm stores packages in node_modules/.pnpm/, workspace node_modules contain symlinks there.
+COPY --from=node-scripts-build --chown=posthog:posthog /code/node_modules /code/node_modules
COPY --from=node-scripts-build --chown=posthog:posthog /code/common/plugin_transpiler/dist /code/common/plugin_transpiler/dist
COPY --from=node-scripts-build --chown=posthog:posthog /code/common/plugin_transpiler/node_modules /code/common/plugin_transpiler/node_modules
COPY --from=node-scripts-build --chown=posthog:posthog /code/common/plugin_transpiler/package.json /code/common/plugin_transpiler/package.json
@@ -392,16 +362,13 @@ COPY --chown=posthog:posthog products products/
# Validate browser dependencies
RUN /python-runtime/bin/python -c "import playwright; print('Playwright package imported successfully')"
RUN /python-runtime/bin/python -c "from playwright.sync_api import sync_playwright; print('Playwright sync API available')"
-# Build-time smoke test: prove the system Chromium actually launches through Playwright and renders a
-# page (the heatmap path uses executable_path=$CHROME_BIN). A missing shared lib or broken chromium
-# fails the build here instead of silently at runtime now that no Playwright browser is bundled.
-RUN /python-runtime/bin/python -c "from playwright.sync_api import sync_playwright; p=sync_playwright().start(); b=p.chromium.launch(headless=True, executable_path='/usr/bin/chromium', args=['--no-sandbox','--disable-gpu','--disable-dev-shm-usage']); pg=b.new_page(); pg.set_content('<h1>ok</h1>'); assert pg.screenshot(), 'empty screenshot'; b.close(); p.stop(); print('chromium launch smoke test OK')"
# Setup ENV.
ENV NODE_ENV=production \
CHROME_BIN=/usr/bin/chromium \
CHROME_PATH=/usr/lib/chromium/ \
- CHROMEDRIVER_BIN=/usr/bin/chromedriver
+ CHROMEDRIVER_BIN=/usr/bin/chromedriver \
+ PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
# Expose container port and run entry point script.
EXPOSE 8000
diff --git a/products/web_analytics/backend/tasks/heatmap_screenshot.py b/products/web_analytics/backend/tasks/heatmap_screenshot.py
index 83d1cd4bf7f9..8cb3e196567a 100644
--- a/products/web_analytics/backend/tasks/heatmap_screenshot.py
+++ b/products/web_analytics/backend/tasks/heatmap_screenshot.py
@@ -4,7 +4,6 @@ import time
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
from django.conf import settings
-from django.core.exceptions import ImproperlyConfigured
import requests
import structlog
@@ -491,22 +490,8 @@ def _launch_local_browser(p: Playwright) -> Browser:
launch_args.append("--no-sandbox")
proxy_url = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
proxy_config = ProxySettings(server=proxy_url) if proxy_url else None
- # In the production image CHROME_BIN points at the only installed browser (/usr/bin/chromium);
- # Playwright's bundled browser is intentionally NOT installed. So when CHROME_BIN is set but the
- # path is missing, fail loudly with a config error rather than silently falling through to a
- # non-existent bundled browser. When CHROME_BIN is unset (local dev), executable_path stays None
- # and Playwright uses its own bundled Chromium. NOTE: Playwright skips browser-version enforcement
- # when executable_path is set, so re-check system-chromium compatibility when bumping `playwright`.
- executable_path = os.environ.get("CHROME_BIN") or None
- if executable_path and not os.path.exists(executable_path):
- logger.error("heatmap_screenshot.chrome_bin_invalid", chrome_bin=executable_path)
- raise ImproperlyConfigured(
- f"CHROME_BIN is set to '{executable_path}' but no executable exists there. The production "
- "image installs Chromium at /usr/bin/chromium and does not bundle a Playwright browser."
- )
return p.chromium.launch(
headless=True, # TIP: for debugging, set to False
- executable_path=executable_path,
args=launch_args,
proxy=proxy_config,
)