Local kubernetes run docker pull from local image fail

10/12/2021

My story is:

1, I create a spring-boot project, with a Dockerfile inside. 2, I successfully create the docker image IN LOCAL with above docker file. 3, I have a minikube build a K8s for my local. 4, However, when I try to apply the k8s.yaml, it tells me that there is no such docker image. Obviously my docker app search in public docker hub, so what I can do?

Below is my dockerfile

FROM openjdk:17-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
expose 8080
ENTRYPOINT ["java","-jar","/app.jar"]

Below is my k8s.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pkslow-springboot-deployment
spec:
  selector:
	matchLabels:
	  app: springboot
  replicas: 2
  template:
	metadata:
	  labels:
	    app: springboot
	spec:
	  containers:
	    - name: springboot
	      image: cicdstudy/apptodocker:latest
	      ports:
	        - containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
  labels:
	app: springboot
  name: pkslow-springboot-service
spec:
  ports:
	- port: 8080
	  name: springboot-service
	  protocol: TCP
	  targetPort: 8080
	  nodePort: 30080
  selector:
	app: springboot
  type: NodePort
-- Vincent
docker
kubernetes

2 Answers

10/12/2021

In Kubernetes there is no centralized built-in Container Image Registry exist. Depending on the container runtime in the K8S cluster nodes you have, it might search first dockerhub to pull images. Since free pull is not suggested or much allowed by Dockerhub now, it is suggested to create an account for development purposes. You will get 1 private repository and unlimited public repository which means that whatever you pushed to public repositories, there somebody can access it. If there is no much concern on Intellectual Property issues, you can continue that free account for development purposes. But when going production you need to change that account with a service/robot account.

  1. Create an Account on DockerHub https://id.docker.com/login/
  2. Login into your DockerHub account locally on the machine where you are building your container image

    docker login --username=yourhubusername --email=youremail@company.com

  3. Build,re-tag and push your image once more (go to the folder where Dockerfile resides)

  • docker build -t mysuperimage:v1 .
  • docker tag mysuperimage:v1 yourhubusername/mysuperimage:v1
  • docker push yourhubusername/mysuperimage:v1
  1. Create a secret for image registry credentials

kubectl create secret docker-registry regcred --docker-server=https://index.docker.io/v1/ --docker-username=<yourhubusername> --docker-password=<yourhubpassword> --docker-email=<your-hub-email>

  1. Create a service account for deployment

kubectl create serviceaccount yoursupersa

  1. Attach secret to the service account named "yoursupersa"

kubectl patch serviceaccount yoursupersa -p '{"imagePullSecrets": {"name": "docker-registry"}}'

  1. Now create your application as deployment resource object in K8S

kubectl create deployment mysuperapp --image=yourhubusername/mysuperimage:v1 --port=8080

  1. Then patch your deployment with service account which has attached registry credentials.(which will cause for re-deployment)

kubectl patch deployment mysuperapp -p '{"spec":{"template":{"spec":{"serviceAccountName":"yoursupersa"}}}}'

  1. the last step is expose your service

kubectl expose deployment/mysuperapp

Then everything is awesome! :)

-- Burak Cansizoglu
Source: StackOverflow

10/13/2021

if you just want to be able to pull images from your local computer with minikube you can use eval $(minikube docker-env) this leads to all docker related commands being used on your minikube cluster to use your local docker daemon. so a pull will first look in your hosts local images instead of hub.docker.io.

more information can be found here

-- meaningqo
Source: StackOverflow