A container image packages the exact runtime needed to start a service: application code, Python and system dependencies, startup command, and exposed port. It replaces laptop-specific assumptions with a reproducible artifact that can run unchanged across local machines and hosting environments. section 5.1 section 5.1.5
Build the image from a deliberately bounded project directory. Pin the base Python version and requirements, copy the source into a working directory, install required system libraries and packages, expose the listening port, and define the process Docker starts. Keep mutable data outside the image: a local SQLite file would be reset whenever a new container starts. section 5.1 section 5.1.5
Listing 5.4 Creating the Dockerfile
FROM python:3.11-slim RUN apt-get update && apt-get install -y libgomp1 && rm -rf /var/lib/apt/lists/* #A WORKDIR /app COPY . /app RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt #B EXPOSE 8000 #C CMD ["python", "app.py"] #D
This Dockerfile turns the application into a Python 3.11 image, installs libgomp1 and pinned dependencies, advertises port 8000, and starts app.py directly. section 5.1.5
Treat an image as a production contract rather than a zip file:
- Exclude .env from the build context with .dockerignore. Supply secrets at runtime so a shared or scanned image cannot expose API keys. section 5.1.5
- Tag and push the tested image to a private container registry. Azure App Service, GitHub Actions, and Kubernetes can then pull the same tagged artifact. section 5.2.1
- Run tests in the image intended for production. A passing pipeline verifies both application logic and the dependencies included in requirements.txt. section 5.2.4
From local build to hosted service
Build a local image, pass environment variables when running it, then tag and push it to an Azure Container Registry. A Web App can pull that immutable image at startup while configuration supplies secrets separately; GitHub Actions can rebuild, test, and replace the prior container on each successful commit. section 5.1.5 section 5.2 section 5.2.1 section 5.2.4































