Skip to content

Commit

Permalink
fix docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 authored Apr 26, 2024
1 parent 78d9c07 commit 55daf2f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
47 changes: 36 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
30 changes: 30 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pytest-asyncio == 0.23.6
aioresponses == 0.7.6

# Linter
ruff == 0.4.1
ruff == 0.4.2

0 comments on commit 55daf2f

Please sign in to comment.