From 2a4d96bfda99f7101ccbaad6a5e9a6c3cd26aa9f Mon Sep 17 00:00:00 2001 From: Xanonymous Date: Wed, 3 May 2023 01:31:59 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20rewrite=20docker=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 5 ++++ Dockerfile | 62 ++++++++++++++++++++++++++++++++++++++-------- docker-compose.yml | 6 ++++- 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..18fc7f1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +LICENSE +README.md +sonar-project.properties +setup.py +.github diff --git a/Dockerfile b/Dockerfile index 877b1e2..8dd93e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,59 @@ -FROM python:alpine +# Use an official Python runtime as a parent image +FROM python:alpine AS base -ARG PORT=7777 -ARG HOST='0.0.0.0' +# Set up a non-root user and working directory +RUN addgroup -S appgroup && adduser -S appuser -G appgroup +USER appuser +WORKDIR /app -ENV PORT=$PORT -ENV HOST=$HOST +# Copy only the necessary files for dependency installation +COPY requirements.txt . -COPY . /heyptt +# Install any needed packages specified in requirements.txt +RUN pip install --trusted-host pypi.python.org -r requirements.txt --no-cache-dir -WORKDIR /heyptt +# Use multistage build to create a separate build stage +FROM base AS builder -RUN pip3 install -r ./requirements.txt --upgrade --no-cache-dir +# Copy the rest of the application code +COPY --chown=appuser:appgroup . . -EXPOSE $PORT +# New stage for building the web assets +FROM node:lts-alpine AS web_builder -CMD ["python3", "serve.py"] +# Initialize pnpm globally +RUN corepack enable + +# Set up a non-root user and working directory for the web project +RUN addgroup -S webgroup && adduser -S webuser -G webgroup +USER webuser +WORKDIR /web + +# Copy the package.json and pnpm-lock.yaml files for dependency installation +COPY --chown=webuser:webgroup web/package.json web/pnpm-lock.yaml ./ + +# Install the web project dependencies +RUN pnpm install --frozen-lockfile + +# Copy the rest of the web project files +COPY --chown=webuser:webgroup web/ . + +# Build the web project +RUN pnpm build + +# Switch back to the base image +FROM base + +# Copy the compiled application from the builder stage +COPY --from=builder --chown=appuser:appgroup /app /app + +# Copy the built web assets to /website +COPY --from=web_builder --chown=appuser:appgroup /web/dist /app/website + +# Set the environment variables +ENV PORT=${PORT} +ENV HOST=${HOST:-0.0.0.0} + +# Make the container executable and run the application +ENTRYPOINT ["python3"] +CMD ["serve.py"] diff --git a/docker-compose.yml b/docker-compose.yml index 5458049..9a45eda 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,14 @@ version: '3' services: - heyptt: + hey-ptt: build: context: . dockerfile: ./Dockerfile + image: hey-ptt ports: - '7777:7777' + environment: + - PORT=7777 + - HOST=0.0.0.0 restart: unless-stopped