Skip to main content
XenonStack

DevOps

XenonStack Blog CI/CD Pipeline for Python with Docker and Kubernetes

| Published | Updated

Key takeaway

  • Build a CI/CD pipeline for a Python app: containerise with Docker, push to a registry, deploy to Kubernetes and verify the rollout. Steps and pitfalls.

A CI/CD pipeline for a Python application builds the code into a container image, tests it, pushes the image to a registry and deploys it to Kubernetes automatically on every change. Continuous integration merges and tests changes often. Continuous delivery keeps the result in a state that can be released to production at any point.

This guide is for developers and DevOps engineers who have a Python service and want a repeatable route from source code to a running Kubernetes workload. It covers containerising the app, publishing the image, writing the Deployment and Service files, verifying the rollout, and where an automation server and AI agents fit. For the Jenkins side, see Continuous Delivery Pipeline with Jenkins.

What does a Python delivery pipeline need before you start?

The pieces are the same whether you run a local cluster or a cloud one. The Kubernetes documentation describes a Deployment as something that "manages a set of Pods to run an application workload, usually one that doesn't maintain state", which is the shape most Python web services take.

ItemPurposeNote
Kubernetes cluster and kubectlRuns the container and lets you manage it from the command lineA local cluster is enough for a first test
Application source codeThe Python service you want to shipPin dependencies in requirements.txt
DockerfileLists the steps that build the imageKeep it small and cache dependency installs
Container registryStores versioned images the cluster pullsDocker Hub, a cloud registry or a private registry
Shared persistent storageKeeps files that must outlive a podOnly needed if the app writes files, not for a database

How do you containerise a Python application?

The Docker guide to containerising a Python app states that containerising means "packaging it together with its dependencies, configuration, and runtime". Its Dockerfile has two stages. A builder stage creates a virtual environment and installs requirements, and a runtime stage copies that environment and the source code. The guide notes that you should download dependencies as a separate step to take advantage of Docker's caching, and that its minimal runtime image runs as nonroot by default. It also lists paths a .dockerignore file keeps out of the build, such as __pycache__, .venv, .git and .env.

A minimal single-stage version looks like this. Use a Python release that is currently supported, not the Python 2.7 base image that many older tutorials used.

FROM python:3.12-slim
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5035
CMD ["python", "wsgi.py"]

Build and tag the image with a version, for example docker build -t my-app:1.0 .. Never rely on a moving "latest" tag in a deployment file.

How do you publish the image to a registry?

Retag the image with the registry address, log in, and push it. Login commands differ between registries and change between command-line versions, so copy the current login steps from your registry's own documentation rather than from an old script. Do not put cloud access keys in files that are committed or baked into an image. If you use a private registry that is not served over TLS, the Docker daemon has to be told to allow it, and that setting weakens transport security, so prefer a registry with a valid certificate.

Two-column diagram. Build side: commit, build and test, package, publish. Deploy side: apply manifests, rollout check, smoke test, rollback.

How do you deploy the image to Kubernetes?

Describe the workload in two files, a Deployment and a Service, and apply them with kubectl. The sample manifests in older guides used the extensions/v1beta1 API group, which is no longer how Deployments are declared. Use apps/v1 and include a selector that matches the pod labels.

FileWhat it definesFields to check
DeploymentThe image, number of replicas and labels for the podsapiVersion, selector, image tag, containerPort
ServiceA stable address and port in front of the podsSelector matches pod labels, port and type
PersistentVolumeClaim (optional)A request for storage that survives pod restartsAccess mode, size, mount path

Apply the files with kubectl apply -f so that later changes update the running workload instead of failing because it already exists. A NodePort Service exposes the app on a port of every node, which suits testing. For production traffic, put an ingress or load balancer in front instead.

How do you verify and troubleshoot the release?

Check that the pods are running with kubectl get pods in the right namespace, find the exposed port with kubectl get svc, then open the node address and port in a browser or call the health endpoint. The Kubernetes documentation also lists reference pages for kubectl rollout status, rollout history and rollout undo, which let a pipeline wait for a release and roll it back. When something fails, work through this order: pod status, container logs, service port and selector, and finally the dependencies in requirements.txt.

Which stages should the pipeline automate?

StageTriggerGate before the next stage
Build and testEvery commit or pull requestUnit tests and linting pass
PackageMerge to the main branchImage builds and is tagged with the commit or version
PublishSuccessful package stageImage is in the registry
DeployPublish done, or manual approval for productionRollout completes and a smoke test passes

Teams that want help setting this up can read about DevOps consulting services, or follow the same pattern for other stacks in Azure DevOps Pipeline for Laravel Php and Continuous Delivery for Scala Applications.

How do AI agents change a Python delivery pipeline?

A fixed pipeline runs the same steps every time. An AI agent can read a failed build log, propose a fix, open a pull request or check a rollout, while the pipeline's gates and approvals still decide what reaches production. The useful design question is which actions an agent may take alone and which need a human sign-off. The AI agents computing platform is XenonStack's route to building and governing agents for DevOps and automation work like this.

What should you do first?

  1. Containerise one service with a pinned, supported Python version and a .dockerignore file.
  2. Push a versioned image to a registry with credentials held outside the repository.
  3. Write the Deployment and Service files with apps/v1 and apply them to a test namespace.
  4. Add a pipeline that builds, tests, publishes and deploys on merge.
  5. Add a rollout check and a rollback step before you add production approvals.

To map one decision your agents make in the pipeline, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is the difference between continuous integration and continuous delivery?

Continuous integration merges and tests code changes frequently. Continuous delivery goes further and keeps every successful build ready to deploy to production.

Do I need persistent storage for a Python app on Kubernetes?

Only if the app writes files that must survive a pod restart, such as uploads. Services that keep state in a database usually do not need it.

Which registry should I use for container images?

Any registry your cluster can reach works, including Docker Hub, a cloud provider registry or a private registry. Choose by where the cluster runs, access control and cost.

Why should I avoid the latest image tag?

A moving tag makes it unclear which build is running and makes rollbacks harder. A version or commit tag ties each deployment to one image.

How do I roll back a failed Kubernetes release?

Use the kubectl rollout undo command on the Deployment, and check the result with kubectl rollout status. Add both to the pipeline so a failed smoke test triggers the rollback.

Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.

Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.

Table of Contents

navdeep-singh-gill

Navdeep Singh Gill

Global CEO and Founder of XenonStack

LinkedIn profile

Navdeep Singh Gill is serving as Chief Executive Officer and Product Architect at XenonStack. He holds expertise in building SaaS Platform for Decentralised Big Data management and Governance, AI Marketplace for Operationalising and Scaling. His incredible experience in AI Technologies and Big Data Engineering thrills him to write about different use cases and its approach to solutions.

Get the latest articles in your inbox

Subscribe Now

Interested in solving your challenges with the XenonStack team?
Get Started

Tell us about you and your priorities so we can prepare the right conversation. It takes about 2 minutes.

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Continue