Wednesday 30 November 2022

Building Spring boot application manually and run it with the Docker Container


git clone https://github.com/callicoder/spring-boot-websocket-chat-demo.git

 cd spring-boot-websocket-chat-demo/

 cat Dockerfile

# Start with a base image containing Java runtime

FROM openjdk:11


# Add Maintainer Info

MAINTAINER Rajeev Kumar Singh <callicoder@gmail.com>


# Add a volume pointing to /tmp

VOLUME /tmp


# Make port 8080 available to the world outside this container

EXPOSE 8080


# The application's jar file

ARG JAR_FILE=target/websocket-demo-0.0.1-SNAPSHOT.jar


# Add the application's jar to the container

ADD ${JAR_FILE} websocket-demo.jar


# Run the jar file

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/websocket-demo.jar"]


mvn clean package


sudo docker build -t webchat .


sudo docker images


sudo docker run -p 5000:8080 --name web_chat webchat


http://localhost:5000/


Building a Spring Boot application in Jenkins using pipeline Groovy script and run with Docker Container

 

This Jenkins job will be building the docker image from the Git repo.

Create a pipeline job with Jenkins and add Pipeline Script.

pipeline {

    agent any


    tools {

        // Install the Maven version configured as "M3" and add it to the path.

        maven "M3"

    }


    stages {

        

        stage('Preparation') {

            steps {

                // Get some code from a GitHub repository

                git 'https://github.com/callicoder/spring-boot-websocket-chat-demo.git'


            }

        }

        

        stage('Build') {

            steps {


                // To run Maven on a Windows agent, use

                sh "mvn -Dmaven.test.failure.ignore=true clean package"

            }

            


            post {

                // If Maven was able to run the tests, even if some of the test

                // failed, record the test results and archive the jar file.

                success {

                    junit '**/target/surefire-reports/TEST-*.xml'

                    archiveArtifacts 'target/*.jar'

                }

            }

        }

 stage('Docker Build') {

   

          steps {

          sh 'docker build -t spring-boot-websocket-chat-demo .'

          }

        }

        

         

        stage('Deploy') {

   

          steps {

               sh 'docker stop app_container'

               sh 'docker rm app_container'

          sh 'docker run -d -p 5001:8080 --name app_container spring-boot-websocket-chat-demo'

          }

        }

    }

}















Tuesday 29 November 2022

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:

 

Error: - While running pipeline groovy script to build a docker image for spring boot chat demo.

+ docker build -t spring-boot-websocket-chat-demo .
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=spring-boot-websocket-chat-demo&target=&ulimits=null&version=1": dial unix /var/run/docker.sock: connect: permission denied
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
Stage "Deploy" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
solution:- 
sudo chmod 777 /var/run/docker.sock

How to check a Linux system is Little endian or Big

Example from Ubuntu System

#lscpu

lscpu | grep Endian

lscpu | grep -i byte




#python3

python3 -c "import sys; print(sys.byteorder)"




#Example from a little-endian system

echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6

#using awk

echo -n I | od -to2 | awk '{ print substr($2,6,1); exit}'

echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'

#on all unix systems

echo I | tr -d [:space:] | od -to2 | head -n1 | awk '{print $2}' | cut -c6





#with hexdump

echo -n I | hexdump -o | awk '{ print substr($2,6,1); exit}'

hexdump -s 5 -n 1 -C /bin/busybox

hexdump -s 5 -n 1 /bin/sh





# apt install dpkg-dev on ubuntu

dpkg-architecture | grep -i end





#take advantage of ELF file format

xxd -c 1 -l 6 /bin/ls






Ref:- how-to-tell-if-a-linux-system-is-big-endian-or-little-endian


Wednesday 23 November 2022

Build a Java Application with Jenkins in Docker


cat Dockerfile

from jenkins/jenkins:lts

USER root

RUN apt-get update -qq && apt-get install wget

RUN wget http://apache.cs.utah.edu/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz

RUN tar xzf apache-maven-3.8.6-bin.tar.gz -C /opt/

RUN ln -s /opt/apache-maven-3.8.6 /opt/maven

RUN ln -s /opt/maven/bin/mvn /usr/local/bin

RUN rm -f apache-maven-3.8.6-bin.tar.gz

ENV MAVEN_HOME /opt/maven

RUN chown -R jenkins:jenkins /opt/maven

RUN apt clean

USER jenkins


sudo docker image build -t jenkins-docker .


 sudo docker images

REPOSITORY        TAG       IMAGE ID       CREATED          SIZE

jenkins-docker    latest    ca1cb1129c7e   38 seconds ago   505MB

jenkins/jenkins   lts       25fa92c47840   8 days ago       463MB


sudo docker run -it -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock --restart unless-stopped jenkins-docker


sudo docker ps

CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS                                                                                      NAMES

7333f25c2524   jenkins-docker   "/usr/bin/tini -- /u…"   4 minutes ago   Up 4 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp   silly_sanderson


http://localhost:8080/


#get the admin password




Jenkins global configurations

sudo docker exec -it 7333f25c2524 /bin/bash

jenkins@7333f25c2524:/$ mvn --version

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)

Maven home: /opt/maven

Java version: 11.0.16.1, vendor: Eclipse Adoptium, runtime: /opt/java/openjdk

Default locale: en, platform encoding: UTF-8

OS name: "linux", version: "5.15.0-52-generic", arch: "amd64", family: "unix"

jenkins@7333f25c2524:/$ java --version

openjdk 11.0.16.1 2022-08-12

OpenJDK Runtime Environment Temurin-11.0.16.1+1 (build 11.0.16.1+1)

OpenJDK 64-Bit Server VM Temurin-11.0.16.1+1 (build 11.0.16.1+1, mixed mode)































Execute shell commands in Jenkins by  Creating a Job



































Example using jenkins-docker-maven-github