forked from PostHog/posthog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
production.Dockerfile
184 lines (149 loc) · 4.94 KB
/
production.Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#
# 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.8-alpine3.16 AS frontend
WORKDIR /code
COPY package.json yarn.lock ./
RUN yarn config set network-timeout 300000 && \
yarn install --frozen-lockfile
COPY frontend/ frontend/
COPY ./bin/ ./bin/
COPY babel.config.js tsconfig.json webpack.config.js ./
RUN yarn build
#
# Build the plugin-server artifacts. Note that we still need to install the
# runtime deps in the main image
#
FROM node:18.8-alpine3.16 AS plugin-server
WORKDIR /code/plugin-server
# Install python, make and gcc as they are needed for the yarn install
RUN apk --update --no-cache add \
"make~=4.3" \
"g++~=11.2" \
"gcc~=11.2" \
"python3~=3.10"
# Compile and install Yarn 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/yarn.lock ./plugin-server/tsconfig.json ./
RUN yarn config set network-timeout 300000 && \
yarn 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 yarn 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/yarn.lock ./
# Switch to root and install yarn so we can install runtime deps. Node that we
# still need yarn to run the plugin-server so we do not remove it.
USER root
RUN apk --update --no-cache add "yarn~=1"
# 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 \
&& yarn install --frozen-lockfile --production=true \
&& yarn cache clean \
&& 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"]