dissabte, 26 de setembre del 2020

s2i with docker to test images locally

this document explain how to create docker imges to redhad openshift and test images locally whith only docker

Install docker and configure docker for snap.

you need install docker. I have used snap docker for ubuntu20 but each docker daemaon and client shudl be ok

to work with images stream you need to have it in your dockerd and it is necessary have the capacity to pull images from redhad

create user docker and add your user if you want to avoid sudo docker.

docker login registry.redhat.io

this create a file with credentials tokens from user and pwd this file shud be accesible in ~/.docker In case of snapcraft this file is located inside of snap and you shuld copy it

cp /home/jordi/snap/docker/471/.docker/config.json /home/jordi/.docker/config.json

install s2i

only download from gith and set the executable in the path

create image from git

it is possible and easyest way to create image

 s2i  build  https://github.com/jordiesc/java-springboot-basic registry.redhat.io/redhat-openjdk-18/openjdk18-openshift jordi:v2
INFO Performing Maven build in /tmp/src
INFO Using MAVEN_OPTS -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError
INFO Using Apache Maven 3.6.1 (Red Hat 3.6.1-6.3)
Maven home: /opt/rh/rh-maven36/root/usr/share/maven
Java version: 1.8.0_262, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "5.4.0-48-generic", arch: "amd64", family: "unix"
INFO Running 'mvn -e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga -Dfabric8.skip=true --batch-mode -Djava.net.preferIPv4Stack=true -s /tmp/artifacts/configuration/settings.xml -Dmaven.repo.local=/tmp/artifacts/m2  package'
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...

to run the container

❯ docker run -p 8080:8080  jordi:v1
Starting the Java application using /opt/jboss/container/java/run/run-java.sh ...
INFO exec  java -javaagent:/opt/jboss/container/jolokia/jolokia.jar=config=/opt/jboss/container/jolokia/etc/jolokia.properties -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError -cp "." -jar /deployments/rest-service-0.0.1-SNAPSHOT.jar
I> No access restrictor found, access to any MBean is allowed
Jolokia: Agent started with URL https://172.17.0.2:8778/jolokia/

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

create from source with maven or from binary

you can build from binary o from source code. you must to specify a folder in both cases

❯ cd java-springboot-basic
❯ ll
total 36K
-rwxrwxr-x 1 jordi jordi 9,9K sep 26 11:22 mvnw
-rw-rw-r-- 1 jordi jordi 6,5K sep 26 11:22 mvnw.cmd
-rw-rw-r-- 1 jordi jordi 1,4K sep 26 10:13 pom.xml
-rw-rw-r-- 1 jordi jordi  406 sep 26 11:34 README.md
drwxrwxr-x 4 jordi jordi 4,0K sep 26 10:14 src
drwxrwxr-x 9 jordi jordi 4,0K sep 26 11:09 target
❯ s2i build target  registry.redhat.io/redhat-openjdk-18/openjdk18-openshift jordi:v6
INFO S2I source build with plain binaries detected
INFO Copying binaries from /tmp/src to /deployments ...
rest-service-0.0.1-SNAPSHOT.jar
rest-service-0.0.1-SNAPSHOT.jar.original
classes/
classes/com/
classes/com/example/
classes/com/example/restservice/
classes/com/example/restservice/Greeting.class
classes/com/example/restservice/GreetingController.class
classes/com/example/restservice/Headers.class
classes/com/example/restservice/RestServiceApplication.class
generated-sources/
generated-sources/annotations/
generated-test-sources/
generated-test-sources/test-annotations/
maven-archiver/
maven-archiver/pom.properties
maven-status/
maven-status/maven-compiler-plugin/
maven-status/maven-compiler-plugin/compile/
maven-status/maven-compiler-plugin/compile/default-compile/
maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
maven-status/maven-compiler-plugin/testCompile/
maven-status/maven-compiler-plugin/testCompile/default-testCompile/
maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst
maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst
surefire-reports/
surefire-reports/TEST-com.example.restservice.GreetingControllerTests.xml
surefire-reports/com.example.restservice.GreetingControllerTests.txt
test-classes/
test-classes/com/
test-classes/com/example/
test-classes/com/example/restservice/
test-classes/com/example/restservice/GreetingControllerTests.class
Build completed successfully

alternatively

<LeftMouse>❯ cd java-springboot-basic
❯ ll
total 36K
-rwxrwxr-x 1 jordi jordi 9,9K sep 26 11:22 mvnw
-rw-rw-r-- 1 jordi jordi 6,5K sep 26 11:22 mvnw.cmd
-rw-rw-r-- 1 jordi jordi 1,4K sep 26 10:13 pom.xml
-rw-rw-r-- 1 jordi jordi  406 sep 26 11:34 README.md
drwxrwxr-x 4 jordi jordi 4,0K sep 26 10:14 src
drwxrwxr-x 9 jordi jordi 4,0K sep 26 11:09 target
❯ cd ..
❯ s2i build java-springboot-basic  registry.redhat.io/redhat-openjdk-18/openjdk18-openshift jordi:v5

from Docker file

export a docker file

s2i generate registry.redhat.io/redhat-openjdk-18/openjdk18-openshift Dockerfile.jordi

create a folder and move the Dockerfile.jordi to this folder and rename it to Dockerfile

below a sampel with the only the jar, this docker file also work with source code with maven

❯ cd s2idocker
❯ tree
.
├── Dockerfile
├── s3i.adoc
└── target
    └── rest-service-0.0.1-SNAPSHOT.jar

here the docker file

❯ cat Dockerfile
FROM registry.redhat.io/redhat-openjdk-18/openjdk18-openshift
LABEL "io.openshift.s2i.build.image"="registry.redhat.io/redhat-openjdk-18/openjdk18-openshift" \
      "io.openshift.s2i.scripts-url"="image:///usr/local/s2i" \
      "io.openshift.s2i.destination"="/tmp"

USER root
# Copying in source code
COPY ./target /tmp/src
#COPY /home/jordi/jordi/repositorios/github/sprign-securty-saml/target /tmp/src
# Change file ownership to the assemble user. Builder image must support chown command.
RUN chown -R 1001:0 /tmp/src
USER 1001
# Assemble script sourced from builder image based on user input or image metadata.
# If this file does not exist in the image, the build will fail.
RUN /usr/local/s2i/assemble
# Run script sourced from builder image based on user input or image metadata.
# If this file does not exist in the image, the build will fail.
CMD /usr/local/s2i/run

build the docker file

docker build . -t jordi:v4

test and run image

❯ docker images
REPOSITORY                                                 TAG                 IMAGE ID            CREATED             SIZE
jordi                                                      v4                  3f13a7f2bbfc        2 minutes ago       539MB
jordi                                                      v3                  1dbdc19facc1        33 minutes ago      506MB
jordi                                                      v2                  3d9fbf5de1db        2 hours ago         555MB
<none>                                                     <none>              1e244fb4b7a1        2 hours ago         555MB
jordi                                                      v1                  dd78eb8ca2c9        2 hours ago         555MB
jordi                                                      latest              a1064a60f7e6        2 hours ago         555MB
registry.redhat.io/redhat-openjdk-18/openjdk18-openshift   latest              46b0b4cf8a00        2 months ago        473MB
hello-world                                                latest              bf756fb1ae65        8 months ago        13.3kB

❯ docker run -p 8080:8080  -itd  jordi:v4
27e11949e11f9ed432a53804f1ef4ec44dcdd39cc03d0ffcba791a3e508f278f
❯ curl -H "name1: value1" -H "name2: value2" http://localhost:8080/headers
{"host":"localhost:8080","name2":"value2","name1":"value1","user-agent":"curl/7.68.0","accept":"*/*"}%

later