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
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.
Login into your DockerHub account locally on the machine where you are building your container image
docker login --username=yourhubusername --email=youremail@company.com
Build,re-tag and push your image once more (go to the folder where Dockerfile resides)
kubectl create secret docker-registry regcred --docker-server=https://index.docker.io/v1/ --docker-username=<yourhubusername> --docker-password=<yourhubpassword> --docker-email=<your-hub-email>
kubectl create serviceaccount yoursupersa
kubectl patch serviceaccount yoursupersa -p '{"imagePullSecrets": {"name": "docker-registry"}}'
kubectl create deployment mysuperapp --image=yourhubusername/mysuperimage:v1 --port=8080
kubectl patch deployment mysuperapp -p '{"spec":{"template":{"spec":{"serviceAccountName":"yoursupersa"}}}}'
kubectl expose deployment/mysuperapp
Then everything is awesome! :)
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