From 80b858c5785468003c4df010ba206be2407c616e Mon Sep 17 00:00:00 2001 From: Vitaly Artemyev Date: Tue, 26 Sep 2023 08:56:16 +0500 Subject: [PATCH] build: slim down the docker container (#60) Co-authored-by: Ushie --- .dockerignore | 6 ++++++ .gitignore | 2 +- Dockerfile | 36 ++++++++++++++++++++++++++++-------- docker/run-backend.sh | 1 + docker/run-healthcheck.sh | 1 + 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 .dockerignore create mode 100755 docker/run-backend.sh create mode 100755 docker/run-healthcheck.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..283963c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.idea +.devcontainer +.git +.gitignore +.github +.vscode diff --git a/.gitignore b/.gitignore index bb862c8..6c05dd9 100644 --- a/.gitignore +++ b/.gitignore @@ -157,7 +157,7 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ # custom env.sh diff --git a/Dockerfile b/Dockerfile index fcdafb6..94fd1ed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,34 @@ -FROM python:3.11-slim - -ARG GITHUB_TOKEN -ENV GITHUB_TOKEN $GITHUB_TOKEN +## Build dependencies +FROM python:3.11-slim as dependencies WORKDIR /usr/src/app +RUN apt-get update && \ + apt-get install -y --no-install-recommends gcc \ + && rm -rf /var/lib/apt/lists/* + +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +COPY requirements.txt . + +RUN pip install -r requirements.txt + +## Image +FROM python:3.11-slim + +WORKDIR /usr/src/app + +RUN apt-get update && \ + apt-get install -y --no-install-recommends curl \ + && rm -rf /var/lib/apt/lists/* + +ENV PATH="/opt/venv/bin:$PATH" + +COPY --from=dependencies /opt/venv /opt/venv COPY . . -RUN apt update && \ - apt-get install build-essential libffi-dev libssl-dev openssl --no-install-recommends -y \ - && pip install --no-cache-dir -r requirements.txt +CMD docker/run-backend.sh +HEALTHCHECK CMD docker/run-healthcheck.sh -CMD [ "python3", "-m" , "sanic", "app:app", "--fast", "--access-logs", "--motd", "--noisy-exceptions", "-H", "0.0.0.0"] +EXPOSE 8000 diff --git a/docker/run-backend.sh b/docker/run-backend.sh new file mode 100755 index 0000000..1e7b51a --- /dev/null +++ b/docker/run-backend.sh @@ -0,0 +1 @@ +python3 -m sanic /usr/src/app --fast --access-logs --motd --noisy-exceptions -H 0.0.0.0 diff --git a/docker/run-healthcheck.sh b/docker/run-healthcheck.sh new file mode 100755 index 0000000..1c514a5 --- /dev/null +++ b/docker/run-healthcheck.sh @@ -0,0 +1 @@ +curl --fail http://0.0.0.0:8000/docs