Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Docker image #24

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"python_version": "3.11",
"add_script": "y",
"year": "2024",
"add_pypi_release_ci": "y"
"add_pypi_release_ci": "y",
"docker_image": "y"
}
39 changes: 39 additions & 0 deletions {{ cookiecutter.project_name_dashed }}/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Ignore cache
__pycache__/
*.pyc
*.pyo
*.pyd
.ruff_cache/

# Ignore virtual environments
.venv/

# Ignore version control files and directories
.git/
.gitignore

# Ignore CI/CD and deployment-related files
.github/
.dockerignore
.cruft.json
.pre-commit-config.yaml
.safety-policy.yml
.readthedocs.yaml
.yamllint.yaml
.tool-versions

# Ignore markdown files (e.g., documentation)
*.md

# Ignore environment files (for local development)
.env

# Ignore deployment files
deployment/

# Ignore documentation and images
docs/
images/

# Ignore test files and directories
tests/
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
###################################################
# Stage 1: Get poetry #
###################################################
FROM python:{{ cookiecutter.python_version }}-slim AS poetry

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
&& rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 - && \
ln -s /root/.local/bin/poetry /usr/local/bin/poetry

###################################################
# Stage 2: Get deps #
###################################################
FROM poetry AS builder

# Set work directory
WORKDIR /app

# Copy poetry configuration
COPY pyproject.toml poetry.lock* /app/

# Install dependencies
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi --only main

# Copy the rest of the application code
COPY . /app/

###################################################
# Stage 3: Install wheel and create user #
###################################################
FROM python:{{ cookiecutter.python_version }}-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

# Set work directory
WORKDIR /app

# Copy dependencies from builder
COPY --from=builder /app /app

# Create and switch to a non-root user
RUN addgroup --system app && adduser --system --ingroup app app
RUN chown -R app:app /app
USER app

# Set labels for the Docker image
LABEL org.opencontainers.image.title="{{ cookiecutter.project_name }}"
LABEL org.opencontainers.image.description="{{ cookiecutter.short_description }}"
LABEL org.opencontainers.image.version="0.1.0"
LABEL org.opencontainers.image.authors="{{ cookiecutter.author_name }} <{{ cookiecutter.author_email }}>"
LABEL org.opencontainers.image.licenses="Apache-2.0"
LABEL org.opencontainers.image.source="https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.project_name_dashed }}"
LABEL org.opencontainers.image.url="https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.project_name_dashed }}"
LABEL org.opencontainers.image.documentation="https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.project_name_dashed }}/blob/main/README.md"
LABEL org.opencontainers.image.vendor="{{ cookiecutter.github_username }}"

# Expose port (if your application uses a specific port, adjust accordingly)
# EXPOSE 8000

# Define the entry point to the console script
ENTRYPOINT ["python" , "{{ cookiecutter.project_name_underscored }}/main.py"]

# Optional: Specify default command arguments
# CMD []