|
| 1 | +# https://github.com/devcontainers/images/tree/main/src/python |
| 2 | + |
| 3 | +# syntax=docker/dockerfile:1.4 |
| 4 | + |
| 5 | +# Use single-stage build with Python 3.12 |
| 6 | +FROM mcr.microsoft.com/devcontainers/python:1-3.12-bookworm |
| 7 | + |
| 8 | +# Set build arguments |
| 9 | +ARG PYTHON_VERSION=3.12 |
| 10 | + |
| 11 | +# Set environment variables |
| 12 | +ENV PYTHONUNBUFFERED=1 \ |
| 13 | + PYTHONDONTWRITEBYTECODE=1 \ |
| 14 | + PIP_NO_CACHE_DIR=1 \ |
| 15 | + PIP_DISABLE_PIP_VERSION_CHECK=1 \ |
| 16 | + PYTHONPATH=/workspaces/Apim-Samples/shared/python:/workspaces/Apim-Samples \ |
| 17 | + DEBIAN_FRONTEND=noninteractive |
| 18 | + |
| 19 | +# Install system dependencies as root |
| 20 | +USER root |
| 21 | +RUN --mount=type=cache,target=/var/cache/apt \ |
| 22 | + --mount=type=cache,target=/var/lib/apt/lists \ |
| 23 | + apt-get update && \ |
| 24 | + # Remove Python 3.11 and other versions if they exist |
| 25 | + apt-get remove -y python3.11* python3-minimal python3.11-minimal || true && \ |
| 26 | + apt-get autoremove -y && \ |
| 27 | + # Install essential packages |
| 28 | + apt-get install -y --no-install-recommends curl wget jq tree git-lfs vim nano htop && \ |
| 29 | + apt-get clean && \ |
| 30 | + # Create symbolic links to ensure python3 points to Python 3.12 |
| 31 | + ln -sf /usr/local/bin/python3.12 /usr/bin/python3 && \ |
| 32 | + ln -sf /usr/local/bin/python3.12 /usr/bin/python && \ |
| 33 | + # Ensure latest pip is installed for Python 3.12 |
| 34 | + /usr/local/bin/python3.12 -m pip install --upgrade pip setuptools wheel |
| 35 | + |
| 36 | +# Switch to vscode user |
| 37 | +USER vscode |
| 38 | + |
| 39 | +# Configure Azure CLI for Codespaces (prebuild optimization) |
| 40 | +RUN az config set core.login_experience_v2=off 2>/dev/null || true && \ |
| 41 | + az extension add --name containerapp --only-show-errors 2>/dev/null || true && \ |
| 42 | + az extension add --name front-door --only-show-errors 2>/dev/null || true |
| 43 | + |
| 44 | +# Configure shell to auto-activate virtual environment |
| 45 | +RUN echo "# Auto-activate APIM Samples virtual environment" >> ~/.bashrc && \ |
| 46 | + echo "if [ -f /workspaces/Apim-Samples/.venv/bin/activate ]; then" >> ~/.bashrc && \ |
| 47 | + echo " source /workspaces/Apim-Samples/.venv/bin/activate" >> ~/.bashrc && \ |
| 48 | + echo "fi" >> ~/.bashrc && \ |
| 49 | + echo "# Auto-activate APIM Samples virtual environment" >> ~/.zshrc && \ |
| 50 | + echo "if [ -f /workspaces/Apim-Samples/.venv/bin/activate ]; then" >> ~/.zshrc && \ |
| 51 | + echo " source /workspaces/Apim-Samples/.venv/bin/activate" >> ~/.zshrc && \ |
| 52 | + echo "fi" >> ~/.zshrc && \ |
| 53 | + # Add helpful aliases |
| 54 | + echo "alias ll='ls -alF'" >> ~/.bashrc && \ |
| 55 | + echo "alias la='ls -A'" >> ~/.bashrc && \ |
| 56 | + echo "alias l='ls -CF'" >> ~/.bashrc && \ |
| 57 | + echo "alias pytest-cov='python -m pytest --cov=. --cov-report=html'" >> ~/.bashrc |
| 58 | + |
| 59 | +# Set final working directory |
| 60 | +WORKDIR /workspaces/Apim-Samples |
| 61 | + |
| 62 | +# Add health check for the virtual environment |
| 63 | +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ |
| 64 | + CMD . /workspaces/Apim-Samples/.venv/bin/activate && python -c "import sys, pip; print(f'Python {sys.version}'); print(f'Pip {pip.__version__}'); import requests, jwt; print('Core packages OK')" || exit 1 |
| 65 | + |
| 66 | +# Add labels for maintainability |
| 67 | +LABEL maintainer="APIM Samples Team" \ |
| 68 | + description="Simplified dev container for Azure API Management samples with Codespaces prebuild optimization" \ |
| 69 | + version="2.1" \ |
| 70 | + python.version="3.12" \ |
| 71 | + debian.version="bookworm" \ |
| 72 | + venv.location="/workspaces/Apim-Samples/.venv" |
0 commit comments