Skip to content

Commit d251cad

Browse files
authored
microservices: Optimized Time Series Analytics Dockerfile (open-edge-platform#347)
Optimizes the Dockerfile for the time-series analytics service by consolidating layers, minimizing installed packages, and cleaning up caches to reduce image size and improve security. Consolidated and streamlined package installation and cleanup steps Switched to sparse checkout for Kapacitor Python plugin and removed unneeded files Added non-root user setup and improved healthcheck Signed-off-by: B, Vinod K <vinod.k.b@intel.com>
1 parent e26bec1 commit d251cad

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

microservices/time-series-analytics/Dockerfile

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,63 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66

7-
# Use the Kapacitor 1.7.6 image as the base image
7+
# Use the Kapacitor image as the base image
88
ARG KAPACITOR_VERSION
99
FROM kapacitor:$KAPACITOR_VERSION
1010

11-
# Install Python and other necessary packages
11+
# Install Python and necessary packages in a single layer, minimize image size
1212
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 && \
1717
rm -rf /var/lib/apt/lists/*
1818

1919
WORKDIR /app
2020

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
2628

2729
ARG TIMESERIES_UID
2830
ARG TIMESERIES_USER_NAME
2931
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
3236

3337
COPY ./requirements.txt .
3438

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
3641

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"
3945

4046
# 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/
4652
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/
4955

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
5159

60+
# Switch to non-root user for security
5261
USER $TIMESERIES_USER_NAME
5362

63+
# Simple healthcheck to verify container is running
5464
HEALTHCHECK --interval=5m CMD exit 0
5565

5666
ENTRYPOINT ["python3", "./classifier_startup.py"]

0 commit comments

Comments
 (0)