Dockerfile 2.3 KB

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