Dockerfile 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # sync with https://github.com/fphammerle/systemctl-mqtt/blob/master/Dockerfile
  2. # not using python:3.*-alpine cause glib-dev package depends on python3
  3. # https://pkgs.alpinelinux.org/package/v3.18/main/aarch64/glib-dev
  4. ARG BASE_IMAGE=docker.io/alpine:3.18.4
  5. ARG SOURCE_DIR_PATH=/switchbot-mqtt
  6. # hadolint ignore=DL3006
  7. FROM $BASE_IMAGE as build
  8. RUN apk add --no-cache \
  9. cargo `# cryptography build` \
  10. gcc \
  11. git `# setuptools_scm` \
  12. glib-dev \
  13. jq `# edit Pipfile.lock` \
  14. make \
  15. musl-dev \
  16. openssl-dev `# cryptography build` \
  17. py3-certifi `# pipenv` \
  18. py3-pip `# pipenv install` \
  19. py3-virtualenv `# pipenv` \
  20. python3-dev `# Python.h for cffi build` \
  21. rust `# cryptography build` \
  22. && adduser -S build
  23. USER build
  24. RUN pip3 install --user --no-cache-dir pipenv==2023.6.18
  25. ARG SOURCE_DIR_PATH
  26. COPY --chown=build:nobody Pipfile Pipfile.lock $SOURCE_DIR_PATH/
  27. WORKDIR $SOURCE_DIR_PATH
  28. ENV PIPENV_CACHE_DIR=/tmp/pipenv-cache \
  29. PIPENV_VENV_IN_PROJECT=yes-please \
  30. PATH=/home/build/.local/bin:$PATH
  31. # `sponge` is not pre-installed
  32. RUN jq 'del(.default."switchbot-mqtt", .default."sanitized-package")' Pipfile.lock > Pipfile.lock~ \
  33. && mv Pipfile.lock~ Pipfile.lock \
  34. && pipenv install --deploy
  35. COPY --chown=build:nobody . $SOURCE_DIR_PATH
  36. RUN if ! git status; then export SETUPTOOLS_SCM_PRETEND_VERSION=0.0.0; fi \
  37. && pipenv install --deploy \
  38. && pipenv graph \
  39. && pipenv run pip freeze \
  40. && rm -rf .git/ $PIPENV_CACHE_DIR \
  41. && chmod -cR a+rX .
  42. # workaround for broken multi-stage copy
  43. # > failed to copy files: failed to copy directory: Error processing tar file(exit status 1): Container ID ... cannot be mapped to a host ID
  44. USER 0
  45. RUN chown -R 0:0 $SOURCE_DIR_PATH
  46. USER build
  47. # hadolint ignore=DL3006
  48. FROM $BASE_IMAGE
  49. RUN apk add --no-cache \
  50. glib \
  51. python3 \
  52. tini \
  53. && find / -xdev -type f -perm /u+s -exec chmod -c u-s {} \; \
  54. && find / -xdev -type f -perm /g+s -exec chmod -c g-s {} \;
  55. #RUN apk add bluez-deprecated `# hcitool`
  56. USER nobody
  57. ARG SOURCE_DIR_PATH
  58. COPY --from=build $SOURCE_DIR_PATH $SOURCE_DIR_PATH
  59. ARG VIRTUALENV_PATH=$SOURCE_DIR_PATH/.venv
  60. ENV PATH=$VIRTUALENV_PATH/bin:$PATH
  61. ENV MQTT_HOST ""
  62. ENV MQTT_PORT "1883"
  63. ENV MQTT_USERNAME ""
  64. ENV MQTT_PASSWORD ""
  65. ENTRYPOINT ["tini", "--"]
  66. CMD ["sh", "-c", "switchbot-mqtt --mqtt-host \"$MQTT_HOST\" --mqtt-port \"$MQTT_PORT\" --mqtt-username \"$MQTT_USERNAME\" --mqtt-password \"$MQTT_PASSWORD\""]