From 010376a86b51cc4d0ef40723b25377e295bc6279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Skowron?= Date: Mon, 20 May 2024 15:02:15 +0200 Subject: [PATCH 1/3] Initial Docker support --- .dockerignore | 34 ++++++++++++++++++++++++++++++++++ Dockerfile | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 3 +++ README.md | 19 +++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..03a268b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.DS_Store +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8e6abbb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +# syntax=docker/dockerfile:1 + +ARG PYTHON_VERSION=3.12.3 +FROM python:${PYTHON_VERSION}-slim as base + +# Prevents Python from writing pyc files. +ENV PYTHONDONTWRITEBYTECODE=1 + +# Keeps Python from buffering stdout and stderr to avoid situations where +# the application crashes without emitting any logs due to buffering. +ENV PYTHONUNBUFFERED=1 + +ENV POETRY_VERSION=1.8.2 + +WORKDIR /app + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds. +# Leverage a bind mount to requirements.txt to avoid having to copy them into +# into this layer. +RUN --mount=type=cache,target=/root/.cache/pip \ + python3 -m pip install "poetry==$POETRY_VERSION" + +COPY pyproject.toml poetry.lock . +RUN poetry export -f requirements.txt | python3 -m pip install -r /dev/stdin + +# Switch to the non-privileged user to run the application. +USER appuser + +# Copy the source code into the container. +COPY . . + +FROM base as final + +# Run the application. +ENTRYPOINT ["python3", "-m", "aiocomfoconnect"] diff --git a/Makefile b/Makefile index f3b2334..8d2d472 100644 --- a/Makefile +++ b/Makefile @@ -13,4 +13,7 @@ codefix: test: @poetry run pytest +build: + docker build -t aiocomfoconnect . + .PHONY: check codefix test diff --git a/README.md b/README.md index 8fa1c23..ba80d38 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,25 @@ $ python -m aiocomfoconnect get-property --host 192.168.1.213 1 1 8 9 # Unit 0x - `async cmd_rpdo_request(pdid, type, zone, timeout)`: Send a RPDO request. - `async cmd_keepalive()`: Send a keepalive message. +## Docker + +### Description +Docker with aiocomfoconnect allow to experiment and develop on local machine using same image and same environment on any platform where we can run container. +This allow us to skip some platform issues and work on similar things and python version by easly swapping them in Dockerfile. + +### Building + +When you're ready, start your application by running: +`make build`. + +### Running +Now after we build our images is called `aiocomfoconnect` + +To run any command we can just +`docker run aiocomfoconnect --help` + +Any args from `aiocomfoconnect` can be passed int othis image just like for `python3 -m aiocomfoconnect` command in local build. + ## Examples ### Discovery of ComfoConnect LAN C Bridges From ae1bff0749cada0753110dd4ff74b86e011f1db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Skowron?= Date: Mon, 20 May 2024 15:08:25 +0200 Subject: [PATCH 2/3] Dockerfile cleanup --- Dockerfile | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8e6abbb..d2ffd51 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,22 +10,9 @@ ENV PYTHONDONTWRITEBYTECODE=1 # the application crashes without emitting any logs due to buffering. ENV PYTHONUNBUFFERED=1 +# Choose poetry version ENV POETRY_VERSION=1.8.2 -WORKDIR /app - -# Create a non-privileged user that the app will run under. -# See https://docs.docker.com/go/dockerfile-user-best-practices/ -ARG UID=10001 -RUN adduser \ - --disabled-password \ - --gecos "" \ - --home "/nonexistent" \ - --shell "/sbin/nologin" \ - --no-create-home \ - --uid "${UID}" \ - appuser - # Download dependencies as a separate step to take advantage of Docker's caching. # Leverage a cache mount to /root/.cache/pip to speed up subsequent builds. # Leverage a bind mount to requirements.txt to avoid having to copy them into @@ -33,12 +20,10 @@ RUN adduser \ RUN --mount=type=cache,target=/root/.cache/pip \ python3 -m pip install "poetry==$POETRY_VERSION" +# Install all dependencies for aiocomfoconnect COPY pyproject.toml poetry.lock . RUN poetry export -f requirements.txt | python3 -m pip install -r /dev/stdin -# Switch to the non-privileged user to run the application. -USER appuser - # Copy the source code into the container. COPY . . From 6c7c4ed40892158d813bdea1a5542091359ebeb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Skowron?= Date: Mon, 20 May 2024 15:11:32 +0200 Subject: [PATCH 3/3] Readme cleanup --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ba80d38..967c889 100644 --- a/README.md +++ b/README.md @@ -82,16 +82,19 @@ This allow us to skip some platform issues and work on similar things and python ### Building -When you're ready, start your application by running: -`make build`. +Build the image command +``` +make build +``` ### Running Now after we build our images is called `aiocomfoconnect` -To run any command we can just -`docker run aiocomfoconnect --help` - -Any args from `aiocomfoconnect` can be passed int othis image just like for `python3 -m aiocomfoconnect` command in local build. +To run aiocomfoconnect we can use bellow command in this case with `--help` arg. +``` +docker run aiocomfoconnect --help +``` +Any args from `aiocomfoconnect` can be passed into this image run just like for `python3 -m aiocomfoconnect` command in local build. ## Examples