Skip to content

Instantly share code, notes, and snippets.

@arifsuhan
Created January 22, 2023 06:04
Show Gist options
  • Save arifsuhan/2756fe15c1b7e15ee657a7c9ed8eb4c8 to your computer and use it in GitHub Desktop.
Save arifsuhan/2756fe15c1b7e15ee657a7c9ed8eb4c8 to your computer and use it in GitHub Desktop.
Create Docker Image and run Kubernates

Step 1: Create project using spring.io

curl https://start.spring.io/starter.tgz -d type=gradle-project \
-d language=java \
-d bootVersion=2.6.5 \
-d baseDir=helloG \
-d groupId=com.example.hello \
-d artifactId=helloG \
-d description=Demo%20project%20for%20Spring%20Boot \
-d packageName=com.example.hello.helloG \
-d packaging=jar \
-d javaVersion=11 \
-d dependencies=devtools,web | tar -xzvf -

Then follow steps

Step 2:

./gradlew clean build

Check if the project is build properly.

java -jar build/libs/helloG-0.0.1-SNAPSHOT.jar

Step 3: Create Docker File

FROM adoptopenjdk/openjdk11:alpine
VOLUME /tmp
EXPOSE 9222

ARG JAR_FILE=./build/libs/helloG-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} helloG-app.jar

ENTRYPOINT ["java","-jar","helloG-app.jar"]

Step 4.1:: Create docker image with tag [-t]

docker build -f Dockerfile -t hellog .

Step 4.2:: Check docker image proper or not

docker images
docker run -it --rm -p 8080:8080 hellog

Step 1: Setup Minikube

minikube start
docker images
docker ps -a

Step 2: Load local Docker image into Minikube

minikube image load helloG

Step 3: Create deployment.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellog
  labels:
    name: hellog
spec:
  selector:
    matchLabels:
      name: hellog
  template:
    metadata:
      labels:
        name: hellog
    spec:
      containers:
        - name: hellog
          image: hellog
          imagePullPolicy: Never
          ports:
            - containerPort: 8080

Apply it using kubectl

minikube status
kubectl apply -f deployment.yaml

Step 4:

kubectl port-forward deployment/hellog 8080:8080

Step 1: Clone Git Repo

Git clone https://github.com/Abszissex/medium-local-docker-image-minikube

Step 2: Build container

cd app 
docker build -t pz/demo .

Step 3: Run container

docker run -it --rm -p 8080:8080 pz/demo

Reference

  1. Local Docker Images in Minikube
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment