Im trying to create a pod using my local docker image as follow.
1.First I run this command in terminal
eval $(minikube docker-env)
2.I created a docker image as follow
sudo docker image build -t my-first-image:3.0.0 .
3.I created the pod.yml as shown below and I run this command
kubectl -f create pod.yml.
4.then i tried to run this command
kubectl get pods
but it shows following error
NAME READY STATUS RESTARTS AGE
multiplication-b47499db9-phpb7 0/1 ImagePullBackOff 0 23h
my-first-pod 0/1 ErrImagePull 0 7s
5.i get the pods logs
kubectl describe pod my-first-pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 99s default-scheduler Successfully assigned default/my-first-pod to minikube
Warning Failed 41s (x3 over 94s) kubelet, minikube Failed to pull image "my-first-image:3.0.0": rpc error: code = Unknown desc = Error response from daemon: pull access denied for my-first-image, repository does not exist or may require 'docker login'
Warning Failed 41s (x3 over 94s) kubelet, minikube Error: ErrImagePull
Normal BackOff 12s (x4 over 93s) kubelet, minikube Back-off pulling image "my-first-image:3.0.0"
Warning Failed 12s (x4 over 93s) kubelet, minikube Error: ImagePullBackOff
Normal Pulling 0s (x4 over 98s) kubelet, minikube pulling image "my-first-image:3.0.0"
Dockerfile
FROM node:carbon
WORKDIR /app
COPY . .
CMD [ "node", "index.js" ]
pods.yml
kind: Pod
apiVersion: v1
metadata:
name: my-first-pod
spec:
containers:
- name: my-first-container
image: my-first-image:3.0.0
index.js
var http = require('http');
var server = http.createServer(function(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain');
response.end('Welcome to the Golden Guide to Kubernetes
Application Development!');
});
server.listen(3000, function() {
console.log('Server running on port 3000');
});
When you build image to local Minikube registry you might want to set imagePullPolicy: Never
so that it will not try pulling the image from remote registry. The pod spec would look something like that:
kind: Pod
apiVersion: v1
metadata:
name: my-first-pod
spec:
containers:
- name: my-first-container
image: my-first-image:3.0.0
imagePullPolicy: Never
Since you are using an image without uploading it .You will have to set the imagePullPolicy to Never, otherwise Kubernetes will try to download the image.
minikube start
eval $(minikube docker-env)
docker build -t my-first-image:3.0.0 .
kubectl run my-first-container --image=my-first-image:3.0.0 --image-pull-policy=Never
kubectl get pods
Your pod spec should be like below
pods.yml
kind: Pod
apiVersion: v1
metadata:
name: my-first-pod
spec:
containers:
- name: my-first-container
image: my-first-image:3.0.0
imagePullPolicy: Never
This is because it can't download the docker image defined in your pod definition file. By default it downloads required images from DockerHub.
So after creating your my-first-image:3.0.0
image you have to publish it at DockerHub. For that create an account at DockerHub and login from terminal using login command
sudo docker login
After successful login, rebuild your docker image with your DockerHub username in tag and push it to DockerHub (more details)
sudo docker image build -t YOUR_DOCKERHUB_USERNAME/my-first-image:3.0.0 .
sudo docker push YOUR_DOCKERHUB_USERNAME/my-first-image:3.0.0
Update your image in pod.yml
as YOUR_DOCKERHUB_USERNAME/my-first-image:3.0.0
and create your pods as before.
You can instruct to find required docker image from your local machine instead of downloading from DockerHub. To do so you have to add imagePullPolicy: Never
in your pod.yml
file under specific container description. Below is an example of your pod.yml
file to show where to define image pull policy
kind: Pod
apiVersion: v1
metadata:
name: my-first-pod
spec:
containers:
- name: my-first-container
image: YOUR_DOCKERHUB_USERNAME/my-first-image:3.0.0
imagePullPolicy: Never
You have build the image but you need to push to docker repository.
In case you are building docker image on kubernetes node, it can work but better to push to repository so docker will pull from central repo, as the number of nodes increases you cant control where pod will invoke and image will be unavailable.
docker push -t my-first-image:3.0.0
In case you have private docker repositiory, push to private repository and use full qualified image name.
repository name is missing.
create an account in dockerHub. then build and push the image to your repo in dockerhub
sudo docker image build -t niranga/my-first-image:3.0.0 .
sudo docker login
sudo docker push niranga/my-first-image:3.0.0
update the image name in pod.yaml file as niranga/my-first-image:3.0.0
it should work