|
4 | 4 | # SPDX-License-Identifier: Apache-2.0
|
5 | 5 | #
|
6 | 6 |
|
7 |
| -# Use the Kapacitor 1.7.6 image as the base image |
| 7 | +# Use the Kapacitor image as the base image |
8 | 8 | ARG KAPACITOR_VERSION
|
9 | 9 | FROM kapacitor:$KAPACITOR_VERSION
|
10 | 10 |
|
11 |
| -# Install Python and other necessary packages |
| 11 | +# Install Python and necessary packages in a single layer, minimize image size |
12 | 12 | RUN apt-get update && \
|
13 |
| - apt-get install -y --no-install-recommends python3 python3-dev python3-pip python3-venv git && \ |
14 |
| - pip install -U pip && \ |
15 |
| - pip uninstall -y setuptools && \ |
16 |
| - apt-get clean && \ |
| 13 | + apt-get install -y --no-install-recommends \ |
| 14 | + python3 python3-pip git ca-certificates && \ |
| 15 | + python3 -m pip install --no-cache-dir --upgrade pip && \ |
| 16 | + apt-get purge -y --auto-remove && \ |
17 | 17 | rm -rf /var/lib/apt/lists/*
|
18 | 18 |
|
19 | 19 | WORKDIR /app
|
20 | 20 |
|
21 |
| -RUN mkdir -p /app/kapacitor-${KAPACITOR_VERSION} && \ |
22 |
| - git clone --single-branch -b v${KAPACITOR_VERSION} \ |
23 |
| - https://github.com/influxdata/kapacitor.git /app/kapacitor-${KAPACITOR_VERSION} && \ |
24 |
| - mv /app/kapacitor-${KAPACITOR_VERSION}/udf/agent/py /app/kapacitor_python && \ |
25 |
| - rm -r kapacitor-${KAPACITOR_VERSION} |
| 21 | +# Clone only the required files to reduce image size |
| 22 | +RUN git clone --depth 1 --filter=blob:none --sparse --branch v${KAPACITOR_VERSION} \ |
| 23 | + https://github.com/influxdata/kapacitor.git /tmp/kapacitor && \ |
| 24 | + cd /tmp/kapacitor && \ |
| 25 | + git sparse-checkout set udf/agent/py && \ |
| 26 | + mv /tmp/kapacitor/udf/agent/py /app/kapacitor_python && \ |
| 27 | + rm -rf /tmp/kapacitor |
26 | 28 |
|
27 | 29 | ARG TIMESERIES_UID
|
28 | 30 | ARG TIMESERIES_USER_NAME
|
29 | 31 | ARG PYTHON_VERSION
|
30 |
| -RUN groupadd $TIMESERIES_USER_NAME -g $TIMESERIES_UID && \ |
31 |
| - useradd -r -u $TIMESERIES_UID -g $TIMESERIES_USER_NAME $TIMESERIES_USER_NAME |
| 32 | + |
| 33 | +# Create non-root user and group in a single layer for smaller image and better caching |
| 34 | +RUN groupadd --gid $TIMESERIES_UID $TIMESERIES_USER_NAME && \ |
| 35 | + useradd --no-log-init --system --uid $TIMESERIES_UID --gid $TIMESERIES_UID --create-home $TIMESERIES_USER_NAME |
32 | 36 |
|
33 | 37 | COPY ./requirements.txt .
|
34 | 38 |
|
35 |
| -RUN pip3 install -r requirements.txt |
| 39 | +# Install Python dependencies efficiently and clean up cache to reduce image size |
| 40 | +RUN pip3 install --no-cache-dir -r requirements.txt |
36 | 41 |
|
37 |
| -ENV PYTHONPATH $PYTHONPATH:/tmp/py_package:/app/kapacitor_python/ |
38 |
| -ENV LD_LIBRARY_PATH $LD_LIBRARY_PATH:/app/idp/lib |
| 42 | +# Set environment variables in a single ENV instruction for better layer caching |
| 43 | +ENV PYTHONPATH="$PYTHONPATH:/tmp/py_package:/app/kapacitor_python/" \ |
| 44 | + LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/app/idp/lib" |
39 | 45 |
|
40 | 46 | # Adding classifier program
|
41 |
| -COPY ./src/classifier_startup.py /app |
42 |
| -COPY ./src/opcua_alerts.py /app |
43 |
| -COPY ./src/mr_interface.py /app |
44 |
| -COPY ./config.json /app |
45 |
| -# Add configs |
| 47 | +# Copy Python source files in a single layer for better caching |
| 48 | +COPY ./src/classifier_startup.py ./src/opcua_alerts.py ./src/mr_interface.py /app/ |
| 49 | + |
| 50 | +# Copy configuration files and directories efficiently |
| 51 | +COPY ./config.json /app/ |
46 | 52 | COPY ./config/kapacitor*.conf /app/config/
|
47 |
| -COPY ./tick_scripts /app/temperature_classifier/tick_scripts |
48 |
| -COPY ./udfs /app/temperature_classifier/udfs |
| 53 | +COPY ./tick_scripts /app/temperature_classifier/tick_scripts/ |
| 54 | +COPY ./udfs /app/temperature_classifier/udfs/ |
49 | 55 |
|
50 |
| -RUN apt-get remove --purge -y git |
| 56 | +# Remove git and clean up to reduce image size |
| 57 | +RUN apt-get purge -y --auto-remove git && \ |
| 58 | + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /root/.cache/pip |
51 | 59 |
|
| 60 | +# Switch to non-root user for security |
52 | 61 | USER $TIMESERIES_USER_NAME
|
53 | 62 |
|
| 63 | +# Simple healthcheck to verify container is running |
54 | 64 | HEALTHCHECK --interval=5m CMD exit 0
|
55 | 65 |
|
56 | 66 | ENTRYPOINT ["python3", "./classifier_startup.py"]
|
0 commit comments