From 55daf2f53070e3a5b5e0262ff1b12bef0b2dcde4 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Fri, 26 Apr 2024 13:01:26 +0200 Subject: [PATCH] fix docker build --- Dockerfile | 47 +++++++++++++++++++++++++++++++++----------- docker/entrypoint.sh | 30 ++++++++++++++++++++++++++++ requirements.txt | 2 +- 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 docker/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index e3300f5..9d10e6d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,41 @@ -FROM python:3.11-alpine +FROM python:3.12-alpine as buildimage -VOLUME /sml2mqtt +COPY . /tmp/app_install -COPY . /tmp/sml2mqtt_src +RUN set -eux; \ +# Install required build dependencies + apk add --no-cache python3 py3-wheel py3-pip python3-dev gcc musl-dev cargo; \ +# wheel all required packages + cd /tmp/app_install; \ + pip wheel --wheel-dir=/root/wheels . -RUN apk add --no-cache python3 py3-wheel py3-pip gcc musl-dev python3-dev && \ - # install sml2mqtt from local dir - pip install --no-cache-dir /tmp/sml2mqtt_src && \ - # cleanup - pip install --no-cache-dir pyclean && pyclean /usr && pip uninstall -y pyclean setuptools wheel pip && \ - apk del py3-wheel py3-pip gcc musl-dev python3-dev && \ - rm -fr /tmp/* +FROM python:3.12-alpine + +COPY --from=buildimage /root/wheels /root/wheels +COPY docker/entrypoint.sh /entrypoint.sh + +ENV SML2MQTT_FOLDER=/sml2mqtt \ + USER_ID=9001 \ + GROUP_ID=${USER_ID} + +RUN set -eux; \ +# Install required build dependencies + apk add --no-cache su-exec tini; \ +# install sml2mqtt + pip install \ + --no-index \ + --find-links=/root/wheels \ + sml2mqtt; \ +# clean up + rm -rf /root/wheels; \ + rm -fr /tmp/*; \ +# mkdir + mkdir -p ${SML2MQTT_FOLDER}; \ +# prepare entrypoint script + chmod +x /entrypoint.sh; WORKDIR /sml2mqtt -CMD [ "sml2mqtt", "--config", "/sml2mqtt/config.yml"] +VOLUME ["${SML2MQTT_FOLDER}"] +ENTRYPOINT ["/entrypoint.sh"] + +CMD ["su-exec", "sml2mqtt", "tini", "--", "python", "-m", "sml2mqtt", "--config", "/sml2mqtt/config.yml"] diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..8f982fa --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,30 @@ +#!/bin/ash + +set -euo pipefail + +NEW_USER_ID=${USER_ID} +NEW_GROUP_ID=${GROUP_ID:-$NEW_USER_ID} + +echo "Starting with sml2mqtt with user id: $NEW_USER_ID and group id: $NEW_GROUP_ID" +if ! id -u sml2mqtt >/dev/null 2>&1; then + if [ -z "$(getent group "${NEW_GROUP_ID}")" ]; then + echo "Create group sml2mqtt with id ${NEW_GROUP_ID}" + addgroup -g "${NEW_GROUP_ID}" sml2mqtt + else + group_name=$(getent group "${NEW_GROUP_ID}" | cut -d: -f1) + echo "Rename group $group_name to sml2mqtt" + groupmod --new-name sml2mqtt "${group_name}" + fi + echo "Create user sml2mqtt with id ${NEW_USER_ID}" +# -u UID User id +# -D Don't assign a password +# -g GECOS GECOS field +# -H Don't create home directory +# -G GRP Group + adduser -u "${NEW_USER_ID}" -D -g '' -H -G sml2mqtt sml2mqtt +fi + +chown -R sml2mqtt:sml2mqtt "${SML2MQTT_FOLDER}" +sync + +exec "$@" diff --git a/requirements.txt b/requirements.txt index db81569..1334503 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,4 @@ pytest-asyncio == 0.23.6 aioresponses == 0.7.6 # Linter -ruff == 0.4.1 +ruff == 0.4.2