22
33# syntax=docker/dockerfile:1.4
44
5- # BUILD stage for dependencies - use MAJOR semantic image (1) with Python 3.12
6- FROM mcr.microsoft.com/devcontainers/python:1-3.12-bookworm AS builder
5+ # Use single- stage build with Python 3.12
6+ FROM mcr.microsoft.com/devcontainers/python:1-3.12-bookworm
77
8- # Set build arguments for flexibility
9- ARG VIRTUAL_ENV_PATH=/home/vscode /.venv
8+ # Set build arguments for workspace virtual environment
9+ ARG VIRTUAL_ENV_PATH=/workspaces/Apim-Samples /.venv
1010ARG PYTHON_VERSION=3.12
1111
12- # Set environment variables early for better caching
13- ENV PYTHONUNBUFFERED=1 \
14- PYTHONDONTWRITEBYTECODE=1 \
15- PIP_NO_CACHE_DIR=1 \
16- PIP_DISABLE_PIP_VERSION_CHECK=1 \
17- VIRTUAL_ENV=${VIRTUAL_ENV_PATH} \
18- PATH="${VIRTUAL_ENV_PATH}/bin:$PATH"
19-
20- # Switch to vscode user for build
21- USER vscode
22-
23- # Create virtual environment with optimized settings
24- RUN /usr/local/bin/python3.12 -m venv $VIRTUAL_ENV --copies
25-
26- # Copy only requirements.txt first for better Docker layer caching
27- COPY --chown=vscode:vscode requirements.txt /tmp/requirements.txt
28-
29- # Install Python packages with mount cache for pip
30- RUN --mount=type=cache,target=/home/vscode/.cache/pip,uid=1000,gid=1000 \
31- . $VIRTUAL_ENV/bin/activate && \
32- /usr/local/bin/python3.12 -m pip install --upgrade pip setuptools wheel && \
33- pip install --compile -r /tmp/requirements.txt && \
34- pip install --compile pytest pytest-cov coverage ipykernel && \
35- pip list --format=freeze > /tmp/installed-packages.txt && \
36- echo "✅ Pip version: $(pip --version)" > /tmp/pip-version.txt
37-
38- # ##################################################################################################
39-
40- # PRODUCTION stage - use MAJOR semantic image (1) with Python 3.12
41- FROM mcr.microsoft.com/devcontainers/python:1-3.12-bookworm AS production
42-
43- # Set build arguments for flexibility
44- ARG VIRTUAL_ENV_PATH=/home/vscode/.venv
45- ARG PYTHON_VERSION=3.12
46-
47- # Set environment variables early for better caching
12+ # Set environment variables for workspace virtual environment
4813ENV PYTHONUNBUFFERED=1 \
4914 PYTHONDONTWRITEBYTECODE=1 \
5015 PIP_NO_CACHE_DIR=1 \
@@ -54,10 +19,8 @@ ENV PYTHONUNBUFFERED=1 \
5419 PATH="${VIRTUAL_ENV_PATH}/bin:$PATH" \
5520 DEBIAN_FRONTEND=noninteractive
5621
57- # Install system dependencies as root with mount cache
22+ # Install system dependencies as root
5823USER root
59-
60- # Remove any existing Python installations to ensure clean Python 3.12 only environment
6124RUN --mount=type=cache,target=/var/cache/apt \
6225 --mount=type=cache,target=/var/lib/apt/lists \
6326 apt-get update && \
@@ -73,56 +36,47 @@ RUN --mount=type=cache,target=/var/cache/apt \
7336 # Ensure latest pip is installed for Python 3.12
7437 /usr/local/bin/python3.12 -m pip install --upgrade pip setuptools wheel
7538
76- # Copy the virtual environment from builder stage
77- COPY --from=builder --chown=vscode:vscode ${VIRTUAL_ENV_PATH} ${VIRTUAL_ENV_PATH}
78-
79- # Copy pip version info from builder
80- COPY --from=builder /tmp/pip-version.txt /tmp/pip-version.txt
81-
82- # Switch to vscode user for all remaining operations
39+ # Switch to vscode user and set workspace as working directory
8340USER vscode
41+ WORKDIR /workspaces/Apim-Samples
8442
85- # Create virtual environment with Python 3.12 explicitly
86- RUN /usr/local/bin/python3.12 -m venv ${VIRTUAL_ENV_PATH} --copies && \
87- # Verify virtual environment was created correctly
88- ls -la ${VIRTUAL_ENV_PATH} && \
89- ls -la ${VIRTUAL_ENV_PATH}/bin/ && \
90- # Activate virtual environment and verify Python version
91- . ${VIRTUAL_ENV_PATH}/bin/activate && \
92- python --version && \
93- which python && \
94- # Ensure pip is up to date in virtual environment
95- python -m pip install --upgrade pip setuptools wheel && \
96- pip --version
43+ # Create virtual environment directly in workspace during build
44+ RUN /usr/local/bin/python3.12 -m venv .venv --copies
9745
98- # Configure shell environment with optimizations
99- RUN echo "# Virtual environment auto-activation" >> ~/.bashrc && \
100- echo "export VIRTUAL_ENV=${VIRTUAL_ENV_PATH}" >> ~/.bashrc && \
101- echo "export PATH=\"\$ {VIRTUAL_ENV}/bin:\$ PATH\" " >> ~/.bashrc && \
102- echo "source \$ {VIRTUAL_ENV}/bin/activate" >> ~/.bashrc && \
103- echo "# Virtual environment auto-activation" >> ~/.zshrc && \
104- echo "export VIRTUAL_ENV=${VIRTUAL_ENV_PATH}" >> ~/.zshrc && \
105- echo "export PATH=\"\$ {VIRTUAL_ENV}/bin:\$ PATH\" " >> ~/.zshrc && \
106- echo "source \$ {VIRTUAL_ENV}/bin/activate" >> ~/.zshrc && \
107- # Create helpful aliases and functions
108- echo "# APIM Samples helpful aliases" >> ~/.bashrc && \
46+ # Copy requirements file and install packages
47+ COPY --chown=vscode:vscode requirements.txt ./requirements.txt
48+ RUN --mount=type=cache,target=/home/vscode/.cache/pip,uid=1000,gid=1000 \
49+ . .venv/bin/activate && \
50+ pip install --upgrade pip setuptools wheel && \
51+ pip install -r requirements.txt && \
52+ pip install pytest pytest-cov coverage ipykernel && \
53+ echo "✅ Virtual environment created at /workspaces/Apim-Samples/.venv" && \
54+ echo "✅ Python version: $(python --version)" && \
55+ echo "✅ Pip version: $(pip --version)" && \
56+ echo "✅ Packages installed: $(pip list | wc -l)"
57+ # Configure shell to auto-activate virtual environment
58+ RUN echo "# Auto-activate APIM Samples virtual environment" >> ~/.bashrc && \
59+ echo "if [ -f /workspaces/Apim-Samples/.venv/bin/activate ]; then" >> ~/.bashrc && \
60+ echo " source /workspaces/Apim-Samples/.venv/bin/activate" >> ~/.bashrc && \
61+ echo "fi" >> ~/.bashrc && \
62+ echo "# Auto-activate APIM Samples virtual environment" >> ~/.zshrc && \
63+ echo "if [ -f /workspaces/Apim-Samples/.venv/bin/activate ]; then" >> ~/.zshrc && \
64+ echo " source /workspaces/Apim-Samples/.venv/bin/activate" >> ~/.zshrc && \
65+ echo "fi" >> ~/.zshrc && \
66+ # Add helpful aliases
10967 echo "alias ll='ls -alF'" >> ~/.bashrc && \
11068 echo "alias la='ls -A'" >> ~/.bashrc && \
11169 echo "alias l='ls -CF'" >> ~/.bashrc && \
112- echo "alias pipr='pip install -r requirements.txt'" >> ~/.bashrc && \
113- echo "alias pytest-cov='python -m pytest --cov=. --cov-report=html'" >> ~/.bashrc && \
114- echo "alias azlogin='az login --use-device-code'" >> ~/.bashrc
115-
116- # Set working directory
117- WORKDIR /workspaces/Apim-Samples
70+ echo "alias pytest-cov='python -m pytest --cov=. --cov-report=html'" >> ~/.bashrc
11871
11972# Add health check for the virtual environment
12073HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
121- CMD . ${VIRTUAL_ENV_PATH} /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
74+ 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
12275
123- # Add labels for better maintainability
76+ # Add labels for maintainability
12477LABEL maintainer="APIM Samples Team" \
125- description="Optimized dev container for Azure API Management samples" \
126- version="1 .0" \
78+ description="Simplified dev container for Azure API Management samples" \
79+ version="2 .0" \
12780 python.version="3.12" \
128- debian.version="bookworm"
81+ debian.version="bookworm" \
82+ venv.location="/workspaces/Apim-Samples/.venv"
0 commit comments