Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docker build #43

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading