Skip to content

Commit

Permalink
fix(pydiscordsh): smaller docker file
Browse files Browse the repository at this point in the history
  • Loading branch information
h0lybyte committed Jan 7, 2025
1 parent 474c4aa commit 4469e44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
8 changes: 8 additions & 0 deletions apps/pydiscordsh/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__/
*.pyc
*.pyo
*.pyd
*.log
.git
node_modules
tests
40 changes: 25 additions & 15 deletions apps/pydiscordsh/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Base image with Python and Poetry pre-installed
FROM python:3.12-slim
# Stage 1: Builder
FROM python:3.12-slim as builder

# Set environment variables for Poetry and Python
ENV PYTHONUNBUFFERED=1 \
Expand All @@ -11,30 +11,40 @@ ENV PYTHONUNBUFFERED=1 \

# Install system dependencies and Poetry
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
git \
curl git \
&& curl -sSL https://install.python-poetry.org | python3 - \
&& ln -s /opt/poetry/bin/poetry /usr/local/bin/poetry \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
&& apt-get clean && rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Copy only the relevant files for dependency installation first
# Copy and install dependencies
COPY pyproject.toml poetry.lock ./

# Install dependencies with Poetry (including project dependencies)
RUN poetry install --no-root --only main

# Copy the rest of the app code
# Stage 2: Final lightweight image
FROM python:3.12-slim

# Set the same environment variables for consistency
ENV PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
POETRY_VERSION=1.8.2 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

# Copy Poetry from the builder stage
COPY --from=builder /opt/poetry /opt/poetry
COPY --from=builder /usr/local/bin/poetry /usr/local/bin/poetry

WORKDIR /app
# Copy the installed dependencies and the rest of the app
COPY --from=builder /app /app
COPY . .

# Install the project itself (if it's structured as a package)
# Install the app without dev dependencies
RUN poetry install --no-dev

# Expose Port
# Expose the port and run the server
EXPOSE 3000

# Set the default command to run the application
CMD ["poetry", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"]

0 comments on commit 4469e44

Please sign in to comment.