Running Flask on Kubernetes with Minikube: From Docker Image to Live Pods
Step-by-step guide to containerizing a Flask app, deploying it to Minikube, and validating pods and services
Website Visitors:Building a Flask Image for Minikube
- Write the application
|
|
- Create the Dockerfile
|
|
- Build the image inside Minikube’s Docker daemon
|
|
We now have to add the image to minikube. We can do this in multiple ways. See detailed explanation but we can run eval $(minikube docker-env) simply run minikube image load k8s-webserver-image command. I’d prefer using the image load command as we don’t have to change back and forth between minikube docker and host docker.
So create app.py, Dockerfile files and run docker build command. This will create the custom image on your host. Next load it to minikube and continue below.
Kubernetes Deployment Manifest
|
|
imagePullPolicy: IfNotPresenttells the kubelet to use the locally built image rather than attempting to pull from a remote registry.- The
POD_NAMEenvironment variable is populated from the pod’s metadata, enabling the Flask endpoint to display the pod identifier.
Deploying to Minikube
|
|
The deployment creates three pods, each running the Flask container.
Validating the Application
1. Verify pod status
|
|
All pods should show Running and READY = 1/1.
2. Check container logs
|
|
Logs must contain the Flask startup message (* Running on http://0.0.0.0:80/).
3. Expose the service (NodePort)
|
|
Retrieve the allocated port:
|
|
4. Curl the endpoint from the host
|
|
Expected output: Running from pod <pod-name>.
5. Alternative: port‑forward
|
|
Same expected output confirms the pod’s environment variable is correctly injected.
6. Verify POD_NAME inside a pod
|
|
The command prints the pod’s metadata name, confirming the env mapping works.
Common Pitfalls
- Missing
__name__– The Flask app must be instantiated withFlask(__name__). Using a placeholder (**name**) prevents the application from starting. - Image not found – Deploying without building the image inside Minikube leads to
ErrImagePull. Building withminikube docker-envand settingimagePullPolicy: IfNotPresentresolves the issue. - Incorrect YAML indentation – YAML is whitespace‑sensitive; ensure
env,ports, andimagePullPolicyare properly nested undercontainers.
By following the build, deployment, and validation steps above, the Flask application runs reliably on Minikube without external container registries.
Using Docker with Minikube: A Practical Guide
When working with Minikube, a lightweight Kubernetes environment, understanding how Docker interacts with it is crucial.
Minikube’s Docker Environment
Minikube can run its own Docker daemon inside the VM or container that hosts the cluster. This is separate from your local Docker installation. To make your shell use Minikube’s Docker instead of your host’s Docker, run:
|
|
-
minikube docker-envprints environment variables that point Docker commands to Minikube’s daemon. -
eval $(...)applies these variables to your current shell. -
After this,
docker buildanddocker runcommands create images directly inside Minikube, making them immediately available for Kubernetes deployments.
To revert to your local Docker, run:
|
|
Why Minikube Has Its Own Docker
Kubernetes requires a container runtime (Docker, containerd, or CRI-O). Minikube bundles one inside its environment, often Docker, but newer versions may use containerd internally. Even then, it usually provides a Docker interface for building images.
You can check the container runtime with:
|
|
How to Get Images into Minikube Kubernetes
Kubernetes nodes cannot see your local Docker images by default. There are three main approaches:
- Using Minikube’s Docker (fastest for local dev)
|
|
-
Pros: Fast, simple, no registry needed
-
Cons: Only works inside Minikube, not portable
- Pushing to a Docker registry (real-world approach)
|
|
-
Pros: Portable, works in cloud clusters
-
Cons: Slower, requires registry access
- Using
minikube image load(middle ground)
|
|
-
Pros: Uses your local Docker without switching environments
-
Cons: Slightly slower than direct Minikube Docker
Tip: When using Minikube Docker or
minikube image load, setimagePullPolicy: Neverin your Kubernetes deployments to prevent unnecessary registry pulls.
Summary Table
| Method | Where Image Lives | Kubernetes Can See It? |
|---|---|---|
| Local Docker | Your machine | No |
| Minikube Docker | Inside Minikube | Yes |
| Remote Registry | Docker Hub, ECR, etc. | Yes |
This workflow allows developers to build, test, and deploy Kubernetes images locally with minimal friction, while still providing paths to production-ready deployments via container registries.
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.
