Files
posthog/production.Dockerfile
Thomas Obermüller 4a30e78b22 chore: use pnpm to manage dependencies (closes #12635) (#13190)
* chore: use pnpm to manage dependencies

* Fix CI errors

* Don't report Docker image size for external PRs

* Fix pnpm-lock.yaml formatting

* Fix module versions

* Ignore pnpm-lock.yaml

* Upgrade Cypress action for pnpm support

* Set up node and pnpm before Cypress

* Fix typescript issues

* Include patches directory in Dockerfile

* Fix Jest tests in CI

* Update lockfile

* Update lockfile

* Clean up Dockerfile

* Update pnpm-lock.yaml to reflect current package.json files

* remove yarn-error.log from .gitignore

* formatting

* update data exploration readme

* type jest.config.ts

* fix @react-hook issues for jest

* fix react-syntax-highlighter issues for jest

* fix jest issues from query-selector-shadow-dom

* fix transform ignore patterns and undo previous fixes

* add missing storybook peer dependencies

* fix nullish coalescing operator for storybook

* reorder storybook plugins

* update editor-update-tsd warning to new npm script

* use legacy ssl for chromatic / node 18 compatibility

* use pnpm for visual regression testing workflow

* use node 16 for chromatic

* add @babel/plugin-proposal-nullish-coalescing-operator as direct dependency

* try fix for plugin-server

* cleanup

* fix comment and warning

* update more comments

* update playwright dockerfile

* update plugin source types

* conditional image size reporting

* revert react-native instructions

* less restrictive pnpm verions

* use ref component name in line with style guide

Co-authored-by: Jacob Gillespie <jacobwgillespie@gmail.com>
2022-12-12 10:28:06 +01:00

184 lines
4.9 KiB
Docker
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#
# This Dockerfile is used for self-hosted production builds.
#
# Note: for PostHog Cloud remember to update 'Dockerfile.cloud' as appropriate.
#
#
# Build the frontend artifacts
#
FROM node:18.12.1-alpine3.16 AS frontend
WORKDIR /code
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --frozen-lockfile
COPY frontend/ frontend/
COPY ./bin/ ./bin/
COPY babel.config.js tsconfig.json webpack.config.js ./
RUN pnpm build
#
# Build the plugin-server artifacts. Note that we still need to install the
# runtime deps in the main image
#
FROM node:18.12.1-alpine3.16 AS plugin-server
WORKDIR /code/plugin-server
# Install python, make and gcc as they are needed for the pnpm install
RUN apk --update --no-cache add \
"make~=4.3" \
"g++~=11.2" \
"gcc~=11.2" \
"python3~=3.10"
# Compile and install pnpm dependencies.
#
# Notes:
#
# - we explicitly COPY the files so that we don't need to rebuild
# the container every time a dependency changes
COPY ./plugin-server/package.json ./plugin-server/pnpm-lock.yaml ./plugin-server/tsconfig.json ./
RUN corepack enable && pnpm install
# Build the plugin server
#
# Note: we run the build as a separate actions to increase
# the cache hit ratio of the layers above.
COPY ./plugin-server/src/ ./src/
RUN pnpm build
# Build the posthog image, incorporating the Django app along with the frontend,
# as well as the plugin-server
FROM python:3.8.14-alpine3.16
ENV PYTHONUNBUFFERED 1
WORKDIR /code
# Install OS dependencies needed to run PostHog
#
# Note: please add in this section runtime dependences only.
# If you temporary need a package to build a Python or npm
# dependency take a look at the sections below.
RUN apk --update --no-cache add \
"libpq~=14" \
"libxslt~=1.1" \
"nodejs-current~=18" \
"chromium~=102" \
"chromium-chromedriver~=102" \
"xmlsec~=1.2"
# Curl the GeoLite2-City database that will be used for IP geolocation within Django
#
# Notes:
#
# - We are doing this here because it makes sense to ensure the stack will work
# even if the database is not available at the time of boot.
# It's better here to fail at build then it is to fail at boot time.
RUN apk --update --no-cache --virtual .geolite-deps add \
"curl~=7" \
"brotli~=1.0.9" \
&& \
mkdir share \
&& \
( curl -L "https://mmdbcdn.posthog.net/" | brotli --decompress --output=./share/GeoLite2-City.mmdb ) \
&& \
chmod -R 755 ./share/GeoLite2-City.mmdb \
&& \
apk del .geolite-deps
# Compile and install Python dependencies.
#
# Notes:
#
# - we explicitly COPY the files so that we don't need to rebuild
# the container every time a dependency changes
#
# - we need few additional OS packages for this. Let's install
# and then uninstall them when the compilation is completed.
COPY requirements.txt ./
RUN apk --update --no-cache --virtual .build-deps add \
"bash~=5.1" \
"g++~=11.2" \
"gcc~=11.2" \
"cargo~=1.60" \
"git~=2" \
"make~=4.3" \
"libffi-dev~=3.4" \
"libxml2-dev~=2.9" \
"libxslt-dev~=1.1" \
"xmlsec-dev~=1.2" \
"postgresql13-dev~=13" \
"libmaxminddb~=1.6" \
&& \
pip install -r requirements.txt --compile --no-cache-dir \
&& \
apk del .build-deps
RUN addgroup -S posthog && \
adduser -S posthog -G posthog
RUN chown posthog.posthog /code
USER posthog
# Add in Django deps and generate Django's static files
COPY manage.py manage.py
COPY posthog posthog/
COPY ee ee/
COPY --from=frontend /code/frontend/dist /code/frontend/dist
RUN SKIP_SERVICE_VERSION_REQUIREMENTS=1 SECRET_KEY='unsafe secret key for collectstatic only' DATABASE_URL='postgres:///' REDIS_URL='redis:///' python manage.py collectstatic --noinput
# Add in the plugin-server compiled code, as well as the runtime dependencies
WORKDIR /code/plugin-server
COPY ./plugin-server/package.json ./plugin-server/pnpm-lock.yaml ./
# Switch to root and install pnpm so we can install runtime deps. Note that we
# still need pnpm to run the plugin-server so we do not remove it.
USER root
# NOTE: we need make and g++ for node-gyp
# NOTE: npm is required for re2
RUN apk --update --no-cache add "make~=4.3" "g++~=11.2" "npm~=8" --virtual .build-deps \
&& corepack enable \
&& mkdir /tmp/pnpm-store \
&& pnpm install --frozen-lockfile --prod --store-dir /tmp/pnpm-store \
&& rm -rf /tmp/pnpm-store \
&& apk del .build-deps
USER posthog
# Add in the compiled plugin-server
COPY --from=plugin-server /code/plugin-server/dist/ ./dist/
WORKDIR /code/
USER root
COPY ./plugin-server/package.json ./plugin-server/
# We need bash to run the bin scripts
RUN apk --update --no-cache add "bash~=5.1"
COPY ./bin ./bin/
USER posthog
ENV CHROME_BIN=/usr/bin/chromium-browser \
CHROME_PATH=/usr/lib/chromium/ \
CHROMEDRIVER_BIN=/usr/bin/chromedriver
COPY gunicorn.config.py ./
ENV NODE_ENV=production
# Expose container port and run entry point script
EXPOSE 8000
# Expose the port from which we serve OpenMetrics data
EXPOSE 8001
CMD ["./bin/docker"]