0% found this document useful (0 votes)
3 views1 page

Dockerfile for Python 3.11 Slim Image

This document outlines a Dockerfile for a Python 3.11 slim image, focusing on security and size efficiency. It includes steps for setting up the working directory, installing dependencies, creating necessary directories, and configuring a non-root user. Additionally, it sets up a health check and specifies the command to run the application using Uvicorn.

Uploaded by

avi.d.99015
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Dockerfile for Python 3.11 Slim Image

This document outlines a Dockerfile for a Python 3.11 slim image, focusing on security and size efficiency. It includes steps for setting up the working directory, installing dependencies, creating necessary directories, and configuring a non-root user. Additionally, it sets up a health check and specifies the command to run the application using Uvicorn.

Uploaded by

avi.d.99015
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Use Python 3.

11 slim image for smaller size and security


FROM python:3.11-slim

# Set working directory


WORKDIR /app

# Set environment variables


ENV PYTHONPATH=/app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Install system dependencies


RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*

# Copy requirements first for better Docker layer caching


COPY [Link] .

# Install Python dependencies


RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r [Link]

# Copy application code


COPY . .

# Create necessary directories


RUN mkdir -p logs data temp && \
touch logs/.gitkeep data/.gitkeep temp/.gitkeep

# Create non-root user for security


RUN adduser --disabled-password --gecos '' --uid 1000 appuser && \
chown -R appuser:appuser /app

USER appuser

# Expose port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD python -c "import requests; [Link]('[Link]
|| exit 1

# Run the application


CMD ["uvicorn", "main:app", "--host", "[Link]", "--port", "8000"]

You might also like