From 2ae33e80bbb093dd25209206d24f96f727996045 Mon Sep 17 00:00:00 2001 From: Aitor Iturrioz Date: Fri, 16 Aug 2024 03:56:10 +0200 Subject: [PATCH 1/3] Make Docker image much smaller (65MB) The new image is built with the following properties: - It is based on the official python:alpine image - The Dockerfile is multistaged Signed-off-by: Aitor Iturrioz --- Dockerfile | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index ce3b2ae..f0caf89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,20 @@ -FROM python:3.9-slim-bookworm -WORKDIR /app +FROM python:3.12.5-alpine3.20 AS builder + +RUN apk add swig linux-headers alpine-sdk && wget http://abyz.me.uk/lg/lg.zip && unzip lg.zip && cd lg && make || true && make install || true + COPY requirements.txt ./ -RUN apt-get update && apt-get install build-essential -y -RUN pip install --upgrade pip -RUN pip install -r requirements.txt +RUN pip install --no-cache -r requirements.txt + +RUN pip uninstall -y setuptools pip +RUN rm -rf /usr/local/lib/python3.12/site-packages/__pycache__ /usr/local/lib/python3.12/site-packages/_distutils_hack/ + + +FROM python:3.12.5-alpine3.20 + +COPY --from=builder /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/ +COPY --from=builder /usr/local/lib/liblgpio* /usr/local/lib/ + +WORKDIR /app COPY src ./ + CMD ["python", "/app/main.py"] From 721376d98d0e1001cc6b1522385416065d4ff08b Mon Sep 17 00:00:00 2001 From: Aitor Iturrioz Date: Fri, 16 Aug 2024 04:05:00 +0200 Subject: [PATCH 2/3] Fix pigpio compilation hack Signed-off-by: Aitor Iturrioz --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index f0caf89..a340006 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.12.5-alpine3.20 AS builder -RUN apk add swig linux-headers alpine-sdk && wget http://abyz.me.uk/lg/lg.zip && unzip lg.zip && cd lg && make || true && make install || true +RUN apk add swig linux-headers alpine-sdk && wget http://abyz.me.uk/lg/lg.zip && unzip lg.zip && cd lg && sed -i -e 's/ldconfig/echo ldconfig disabled/g' Makefile && make && make install COPY requirements.txt ./ RUN pip install --no-cache -r requirements.txt From c21b40e0b4aa0af39e52e2e2fa47ba8e5107c3e7 Mon Sep 17 00:00:00 2001 From: Aitor Iturrioz Date: Fri, 16 Aug 2024 23:06:36 +0200 Subject: [PATCH 3/3] Add cmake dependency to dockerfile Signed-off-by: Aitor Iturrioz --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a340006..809e1f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.12.5-alpine3.20 AS builder -RUN apk add swig linux-headers alpine-sdk && wget http://abyz.me.uk/lg/lg.zip && unzip lg.zip && cd lg && sed -i -e 's/ldconfig/echo ldconfig disabled/g' Makefile && make && make install +RUN apk add swig linux-headers alpine-sdk cmake && wget http://abyz.me.uk/lg/lg.zip && unzip lg.zip && cd lg && sed -i -e 's/ldconfig/echo ldconfig disabled/g' Makefile && make && make install COPY requirements.txt ./ RUN pip install --no-cache -r requirements.txt