Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Thursday 29 July 2021

ORA-01882: timezone region not found in Docker

This Issue comes when the development environment and the DB Environment are different.

For Eg: I have my Local Box running in the IST Zone and the docker container in UTC Zone. Hence I arrived at this error.

To Resolve this Follow the below steps.

Create the file docker-compose.yml

version:'3'

services:
  app:
    build: .
    image: my-image
    ports:
      - "8084:8084"
    environment:
  - TZ="Asia/Kolkata"


In the above docker-compose file, I mentioned the environment variable TZ as "Asia/Kolkata". Thes Time zones can be found from the link.

Building and Starting the Docker Image

Use docker compose up

It will start the image without this Error.

Happy Solvings!!!!

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

The above Error states that the server you are accessing has some certificate, and you did not have it in your machine. You have to import this local, in the case of docker follow the below steps.


This post Assumes that you have valid certificates with you before proceeding. 



Starting Docker Images with PKI certificates

Happy Learning !!!!

Starting Docker Images with PKI certificates

Before starting this post, Make Sure you copy the certificate to the location where you have the docker file. Edit the following docker file according to your requirement. My Requirement was to copy the jar and start in docker.


Below is my DockerFile


FROM java:8

EXPOSE 8084

ADD ./target/my.jar my_docker.jar

USER root

COPY my.cer $JAVA_HOME/jre/lib/security

RUN \

    cd $JAVA_HOME/jre/lib/security \

    && keytool -keystore cacerts -storepass changeit -noprompt -trustcacerts -importcert -alias ldapcert -file my.cer

ENTRYPOINT ["java","-jar","my.jar"]


Once you copy the certificate and import it using the key tool it will import successfully.


After this build it and run using the port.


Building the Docker image


docker build -t  cert-sample .


Running the Docker image


docker run -d -p 8084:8084 cert-sample


Happy Learning!!!!

Sunday 18 July 2021

Kubernetes read local docker images

Follow the below steps to read the docker images from local instead of pulling them every time from the docker hub. By default, it always reads the images from the docker hub.


This saves us a lot of time by reducing the time to push to the docker hub. It takes a lot of time to push the image from local and tags it.


There are two steps involved.


Step:1


Open the command prompt in admin mode and execute the below command.


C:\Users\Syed>minikube docker-env


Once you execute the command "minikube docker-env" you will see the following output. 


SET DOCKER_TLS_VERIFY=1

SET DOCKER_HOST=tcp://127.0.0.1:32770

SET DOCKER_CERT_PATH=C:\Users\Syed\.minikube\certs

SET MINIKUBE_ACTIVE_DOCKERD=minikube

REM To point your shell to minikube's docker-daemon, run:

REM @FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env') DO @%i

Just Copy the Last line after REM and execute in the same command prompt. 


C:\Users\Syed>@FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env') DO @%i


After making this change, the local docker images will be visible to the K8's.


Step:2


In the Yaml file of the K8's make sure that the image pulls policy to be "Never". Point this file to the local docker build name and the tag. 


eg: imagePullPolicy: Never


Once you do the above two steps, then from next time make changes to the docker file in local, build it and see the changes in the K8's.


Happy Learning!!!!

Thursday 17 June 2021

Deploying SpringBoot Struts 2 Integration Project in Docker

 Follow my previous post and create the sample spring struts integration project from here.


Create a Docker File: Dockerfile


FROM tomcat:latest

ADD target/SpringStrutsDemo-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/

EXPOSE 8080

CMD ["catalina.sh","run"]


Build and tag the Docker File:spring-strutsdemo


C:\Users\Syed\Spring-workspace\SpringStrutsDemo>docker build -t spring-strutsdemo .

Sending build context to Docker daemon  58.75MB

Step 1/4 : FROM tomcat:latest

 ---> 5505f7218e4d

Step 2/4 : ADD target/SpringStrutsDemo-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/

 ---> a30a842ce761

Step 3/4 : EXPOSE 8080

 ---> Running in 35d616d2803f

Removing intermediate container 35d616d2803f

 ---> 2c848691227a

Step 4/4 : CMD ["catalina.sh","run"]

 ---> Running in 270c9c8d4b5d

Removing intermediate container 270c9c8d4b5d

 ---> f7b915b47c1f

Successfully built f7b915b47c1f

Successfully tagged spring-strutsdemo:latest


Run the docker image from the tag 


docker run -p 8080:8080 spring-strutsdemo

This will start in 8080 port. In case if it is not started well there could be an issue with the java version used in the docker or the project is not properly generated and war is invalid.

Access it using 

http://localhost:8080/SpringStrutsDemo-0.0.1-SNAPSHOT/message.action

where SpringStrutsDemo-0.0.1-SNAPSHOT is the context. 





Happy Learning!!!!



Sunday 1 November 2020

Creating Jenkins pipeline to Build and deploy the Docker Image

 To Understand the Jenkins pipeline and trigger build refer to my previous here


Prerequisite.


Install the Following plugins in Jenkins. navigating to ManageJenkins > ManagPlugins




Step1: Create the Docker credentials in Jenkins.


Navigate to Dashboard and  Select Manage Jenkins



Step2: Select Manage credentials.




Step:3 Create Credentials.

Select Stores scoped to Jenkins > Global Credentials > Add Credentials 

Make sure you give some Id to the credentials.


Step:4 Create Pipeline.


pipeline {

  environment {

    registry = "syedghouse14/greet-user-repo"

    registryCredential = 'Docker-Hub'

    dockerImage = ''

    dockerfile="${workspace}\\GreetUser\\Dockerfile"

    pomfile="${workspace}\\GreetUser\\pom.xml"

  }

  agent any

  stages {

    stage('Cloning Git') {

      steps {

        git 'https://github.com/Syed-SearchEndeca/gretuser.git'

        

      }

    }

    stage ('Build') {

steps {

withMaven(maven : 'apache-maven-3.6.3') {

bat "mvn clean package -f ${pomfile}"

}

}

}

    stage('Building image') {

      steps{

        script {

          dockerImage = docker.build registry + ":$BUILD_NUMBER",

          "--file ${dockerfile} ."

        }

      }

    }

    stage('Deploy Image') {

      steps{

        script {

          docker.withRegistry( '', registryCredential ) {

            dockerImage.push()

          }

        }

      }

    }

    stage('Remove Unused docker image') {

      steps{

        bat "docker rmi $registry:$BUILD_NUMBER"

      }

    }

  }

}


Step:5 Execute Success



Things to Consider


Make sure you pass the credentials created in the pipeline.

Pass the Docker File explicitly


 "--file ${dockerfile} ."


You will encounter the following error and resolution for that.

Step 4/5 : COPY GreetUser-0.0.1-SNAPSHOT.jar app.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder619200988/GreetUser-0.0.1-SNAPSHOT.jar: no such file or directory

In One of the step it fails, For this edit the docker file as below.


Initial Docker file.


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


Modified Docker file.


FROM openjdk:8-jdk-alpine

RUN addgroup -S spring && adduser -S spring -G spring

USER spring:spring

ENTRYPOINT ["java","-jar","target/*.jar"]

Here I skipped the Copy Step Instead added the target directly. Hope this helps.

Happy Learning !!!!



Sunday 30 August 2020

How to Push the Images to Docker Hub

1.Packaging

Package your spring applications using the following command.

mvn package

This Command will do the package as specified on the pom.xml.

2.Containerization.

Tag the docker Image.

Tag the Image with the Name in the local.

docker build -t searchio/gs-search-boot-docker .

If you have any dependencies then you can copy by the following way.

mkdir -p target/dependency

cd target/dependency; jar -xf ../*.jar

Once if you do this, it will copy all the dependency jars to the target/dependency.


3.Running the Docker Image.

docker run -p 8080:8080 searchio/gs-search-boot-docker

To run the docker Image use the above command.


4.Build the Image.

mvn spring-boot:build-image -D spring-boot.build-image.imageName=searchio/gs-search-boot-docker

After building follow the Below steps to Push the Image to the Docker Hub.

1. Docker Login.

docker login

UserName:

Password:

provide the username and password here. Once the login is success.


2. Create a repo 

in the docker hub eg: my-first-rep


3. Tag our Image build.

docker tag searchio/gs-search-boot-docker syedghouse14/my-first-repo:push1


here searchio/gs-search-boot-docker is the local tag and syedghouse14/my-first-repo:push1 is the repo created in the docker hub.

Once you follow the above steps then the next step is to push it to the repo and to the docker hub .

5. Push the Image to Docker Hub.

docker push syedghouse14/my-first-repo:push1

Friday 12 June 2020

Understanding docker file

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Now its time to understand about the docker engine and docker daemon.


Docker Engine.

Docker engine or Docker is a client server application that builds and executes containers using Docker components. REST API is a primary mode of communication between Docker Client and Docker Daemon.


Docker Daemon.

Docker Daemon is a server which interacts with the operating system and performs all kind of services. The Docker Daemon listens for REST API request and performs the operation. A command dockerd is used to start a Docker Daemon. Docker Host runs the Docker Daemon and Registry.

 

Docker Daemon checks the client request and communicates with the Docker components in order to perform a service whereas, Docker Engine or Docker is the base engine installed on your host machine to build and run containers using Docker components and services.

Environment variables are supported by the following list of instructions in the Dockerfile:


 COPY

The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

Multiple <src> resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.

Each <src> may contain wildcards and matching will be done using Go’s filepath.Match rules. For example:

COPY test.txt relativeDir/


ADD

The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.

Multiple <src> resources may be specified but if they are files or directories, their paths are interpreted as relative to the source of the context of the build.

 Each <src> may contain wildcards and matching will be done using Go’s filepath.Match rules

eg: To add all the files starting with "sye"

ADD sye* /mydir/


CMD

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

eg:

FROM ubuntu

CMD echo "This is a test." | wc -


ENTRYPOINT

An ENTRYPOINT allows you to configure a container that will run as an executable.

eg:

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


ENV

The ENV instruction sets the environment variable <key> to the value <value>. This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.

eg:

ENV myName="Syed Ghouse"


EXPOSE

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

eg:

EXPOSE 80/udp


FROM

The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.

eg:FROM openjdk:8-jdk-alpine

FROM instructions support variables that are declared by any ARG instructions that occur before the first FROM

An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM. To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage:

eg:

ARG VERSION=latest

FROM busybox:$VERSION

ARG VERSION

RUN echo $VERSION > image_version


MAINTAINER

The MAINTAINER instruction sets the Author field of the generated images. The LABEL instruction is a much more flexible version of this and you should use it instead, as it enables setting any metadata you require, and can be viewed easily, for example with docker inspect. To set a label corresponding to the MAINTAINER field you could use

eg: LABEL maintainer="syedghouse14@gmail.com"


RUN

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

eg:

RUN ["/bin/bash", "-c", "echo hello"]


USER sets the UID (or username) which is to run the container.


VOLUME is used to enable access from the container to a directory on the host machine.


WORKDIR sets the path where the command, defined with CMD, is to be executed.


LABEL

The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing.

eg: LABEL version="1.0"

 


Thursday 11 June 2020

Spring Boot With Docker

We have seen more definition about the containerization and its time for us to see some original work in action.
We will See in this post how to create a docker image and run from it. Of course, it is available in the spring site as a tutorial. I am providing here my version of achieving it.

The First you have to do with your application is to create the jar file of the application, this can be achieved by executing the below commands.

Prerequisite.

1.       If your application is maven based you have to execute the goal as” mvn:package”.

2.    Once you execute the package command it tries to create the jar and run the unit test cases, if you want not to execute the test cases then you can pass the following along with the goal. -Dmaven.test.skip=true

3.       Once the jar is generated you can check your application is running by calling the jar file.  Java -jar springbootdemo-0.0.1-SNAPSHOT.jar once you execute this, you can see the application started running.

4.       Download the docker from the website. https://docs.docker.com/installation/#installation and install it in the local. Makesure you are able to run the docker commands after installation.

Containerization.

1.       Create a file named “Dockerfile” with the following contents.

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

2   You can run the “docker build -t employeeservice . “ to create the tag.

This Dockerfile is very simple, but that’s all you need to run a Spring Boot app with no frills: just Java and a JAR file. The build will create a spring user and a spring group to run the application. It will then COPY the project JAR file into the container as "app.jar" that will be executed in the ENTRYPOINT. The array form of the Dockerfile ENTRYPOINT is used so that there is no shell wrapping the java process.

you can run the application using the following command.

docker run -p 7010:7010 employeeservice

That’s it now your application starts running from the docker image generated. Execute this from the directory where you have the docker file. On executing the build it will download the required and run the application locally.

That’s all Congrats on your first docker app.