Docker - Spring boot application with profile - spring-boot

I have a docker image of a Spring boot application which is created by a developer from a different team. I'm trying to run this image on my Mac.
This application expects a profile name while starting the application. The properties file corresponding to that profile tries to locate a config file on the host machine.
Contents of application-local.properties
spring.config.import=file:/Users/userName/app.properties
Docker command I tried:
docker run -p 8080:8080 -e "JAVA_OPTS=-Dspring.profiles.active=local" application-name:0.0.1
This command fails with this exception as it is not able to access the file on the host machine.
01:09:20.764 [main] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.boot.context.config.ConfigDataResourceNotFoundException: Config data resource 'file [/Users/userName/app.properties]' via location 'file:/Users/userName/app.properties' cannot be found
I tried to use the -v flag to bind this directory on the host to some directory on the container but it still failed.
docker run -p 8080:8080 -v /Users/userName:/app -e "JAVA_OPTS=-Dspring.profiles.active=local" application-name:0.0.1
Exception-
02:50:36.882 [main] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.boot.context.config.ConfigDataResourceNotFoundException: Config data resource 'file [/Users/userName/app.properties]' via location 'file:/Users/userName/app.properties' cannot be found
The ENTRYPOINT of this docker image is:
/bin/sh -c #(nop) ENTRYPOINT ["/bin/sh" "-c" "exec java $JAVA_OPTS -XX:+UseContainerSupport -Djava.security.egd=file:/dev/./urandom -jar /opt/app/app.jar"]
How can I run this application on my machine? Am I missing anything? TIA.

Related

Cannot run Spring Boot aplication in Docker (Getting ERR_EMPTY_RESPONSE) in Windows 10?

I have a problem with my Spring Boot Application running in Docker.
Here is my Dockerfile embedded in my app shown below.
FROM adoptopenjdk:11-jre-hotspot
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/app-0.0.1-SNAPSHOT.jar"]
After I run mvn clean install, I get an app-0.0.1-SNAPSHOT.jar and then define it into a Docker file
Next, I ran this command docker build -t app . I saw this container installed on my docker desktop.
After running this command docker image ls, I also saw this container in the list.
I ran this command docker run -p 9999:8080 app to run it in docker.
The container worked flawlessly after running this command (docker ps)
Next, I test any URL like http://localhost:9999/getCategoryById/1 instead of http://localhost:8080/getCategoryById/1 in Postman but I get the message (Could not send request). When I test this URL in the browser, I get the message ERR_EMPTY_RESPONSE.
I learned the container IP address via docker inspect container_id then I used http://172.17.0.2:9999/getCategoryById/1 but nothing changed.
I also checked if the IP address manages the package (ping 172.17.0.2) but I got Request timed out message.
Here is my project link : Link
How can I fix my issue?
In your application, server.port property in application.properties file, that's used to configure port for Spring Boot embedded Tomcat Server is 8082.
To access the application on the container port 8080, you'd need to override server.port property. One of the ways property can be overridden is using an environment variable like below,
docker run -e SERVER_PORT=8080 -p 9999:8080 app
where SERVER_PORT corresponds to the container port specified in -p <hostPort>:<containerPort>
Other option is to directly update the property in application.properties file like below. After the update, you can then use the same command you've used to run the docker image docker run -p 9999:8080 app
server.port= 8080

Kernel-slim UBI docker image not working with springBootUtility

Team, Is it possible to use springBootUtility with OpenLiberty kernel-slim UBI images (e.g. - kernel-slim-java8-openj9-ubi) ?
https://openliberty.io/docs/21.0.0.7/reference/command/springbootUtility-thin.html
Because, it's giving an error as
Step 3/11 : RUN springBootUtility thin --sourceAppPath=/staging/fat-order-0.0.1-SNAPSHOT.jar --targetThinAppPath=/staging/thin-order-0.0.1-SNAPSHOT.jar --targetLibCachePath=/staging/lib.index.cache
---> Running in 3023c669c4d7
/bin/sh: springBootUtility: command not found
The springBootUtility is only working with OpenLiberty full UBI images
The kernel-slim image does not appear to have that command at all. Compare kernel-slim:
bash-5.1$ docker run --rm -it openliberty/open-liberty:kernel-slim-java8-openj9-ubi ls /opt/ol/wlp/bin
auditUtility binaryLog.bat productInfo securityUtility.bat serverSchemaGen
auditUtility.bat featureUtility productInfo.bat server serverSchemaGen.bat
binaryLog featureUtility.bat securityUtility server.bat tools
To full:
bash-5.1$ docker run --rm -it openliberty/open-liberty:full-java8-openj9-ubi ls /opt/ol/wlp/bin
auditUtility binaryLog.bat featureUtility pluginUtility securityUtility.bat springBootUtility
auditUtility.bat client featureUtility.bat pluginUtility.bat server springBootUtility.bat
batchManager client.bat jaxb productInfo server.bat tools
batchManager.bat ddlGen jaxrs productInfo.bat serverSchemaGen
binaryLog ddlGen.bat jaxws securityUtility serverSchemaGen.bat
There appears to be a hole in the documentation, since nothing indicates you need to do this, but you need to install the springBoot feature into Open Liberty before the command will be added. Copy your server.xml with spring boot specified into the image, then run features.sh:
COPY --chown=1001:0 server.xml /config/
RUN features.sh
After that, springBootUtility will be placed in the /opt/ol/wlp/bin dir and should be on the path as well for further Dockerfile directives to use.

M1 mac cannot run jboss/keycloak docker image

Switched to m1 mac a week ago and I cannot get my application up and running with docker because of the jboss/keycloak image not working as expected. Getting the following message from the container when trying to access localhost:8080
12:08:12,456 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC000001: Failed to start service org.wildfly.network.interface.private: org.jboss.msc.service.StartException in service org.wildfly.network.interface.private: WFLYSRV0082: failed to resolve interface private
12:08:12,526 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([("interface" => "private")]) - failure description: {"WFLYCTL0080: Failed services" => {"org.wildfly.network.interface.private" => "WFLYSRV0082: failed to resolve interface private"}}
12:08:13,463 ERROR [org.jboss.as] (Controller Boot Thread) WFLYSRV0026: Keycloak 12.0.4 (WildFly Core 13.0.3.Final) started (with errors) in 20826ms - Started 483 of 925 services (54 services failed or missing dependencies, 684 services are lazy, passive or on-demand)
Tried with all image versions and all behave the same. Has anyone managed to run this image without issues? Thanks
Also you can build the keycloak docker image locally, I was able to start keycloak after doing that. Here are the steps I follow;
Clone Keycloak containers repository: git clone git#github.com:keycloak/keycloak-containers.git
Open server directory (cd keycloak-containers/server)
Checkout at desired version, eg. git checkout 12.0.4
Build docker image docker build -t jboss/keycloak:12.0.4 .
Run Keycloak docker run --rm -p 9080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin jboss/keycloak:12.0.4
Using this image, I am now able to startup keycloak. https://hub.docker.com/r/wizzn/keycloak
For Keycloak 16, docker 20.10 and docker-compose 1.29, this image works flawlessly: https://hub.docker.com/r/sleighzy/keycloak - as suggested by #zakjan.
A service like:
keycloak:
image: sleighzy/keycloak
environment:
... your Keycloak config
Should be enough to get up and running.
I'm on an m1 and I ran this and it worked.
docker run --platform=linux/amd64 -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:17.0.0 start-dev
I merely add --platform=linux/amd64 to their docker command I found in https://www.keycloak.org/getting-started/getting-started-docker
The location for building a quarkus version of keycloak has changed, so this method will not work anymore for any major releases greater than 16. But the following script will. Just save it as an sh. file and execute it in your terminal. By enabling the last line, this will also directly start an instance of Keycloak.
The version number can be changed, but this is only tested for M1 chips and version 17.0.0.
VERSION=17.0.0 # set version here
cd /tmp
git clone git#github.com:keycloak/keycloak.git
cd keycloak/quarkus/container
git checkout $VERSION
docker build -t "quarkus-keycloak:$VERSION" .
#docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin "quarkus-keycloak:$VERSION" start-dev --http-relative-path /auth
There is an update to this issue - images for AMD64 and ARM64 architectures are now available and can be found here: https://quay.io/repository/keycloak/keycloak?tab=tags.
Ref the discussions in Github (https://github.com/keycloak/keycloak-containers/issues/341 and https://github.com/keycloak/keycloak/issues/8825).
jboss/keycloak not supported arm64 for now.
But you can use that image on docker hub: mihaibob/keycloak
https://hub.docker.com/r/mihaibob/keycloak
I'm using this and haven't difference.
I don't have a mac but I just started working with jboss/keycloak lately and have been able to get it to start.
Essentially what I did (assuming docker is installed):
docker pull jboss/keycloak:16.1.0
docker run --env-file targetDB.txt -p 8080:8080 jboss/keycloak:16.1.0
Might have to do those commands with sudo
This pulls the jboss/keycloak image from docker hub and then it runs it exposing the port 8080 within the container to the host machine. It also uses the environment variables in the .txt file (which contains info on the database endpoint you wish to connect keycloak to to persist data).
If you don't specify --env-file <text file> I believe keycloak uses its default h2 Database which isn't the best.
I have my local jboss/keycloak pointing to an postgres db I have in an AWS RDS environment, so the contents of the targetDB.txt for me is:
DB_VENDOR=postgres
DB_ADDR=<my postgres aws rds endpoint>:5432
DB_DATABASE=<name of the database>
DB_USER=<db username to connect to postgres instance>
DB_PASSWORD=<password associated with db username to connect>
If I'm not mistaken the name of the Database in DB_DATABASE field must already exist. So you'll need to create that before running the docker run command.
After you do the docker run command above and the logs show it starting up you should be able to access the keycloak admin console on your local browser:
http://localhost:8080/auth
If this is the first time you're running keycloak you have to create a master/admin user before you can log in.
To add a master user, run these commands (while your keycloak is already running):
docker exec <container id or container name> /opt/jboss/keycloak/bin/add-user-keycloak.sh -u <USERNAME> -p <PASSWORD>
then you need to restart your keycloak container:
docker restart <container id or container name>
Again you might have to do those commands with sudo.
After thats done, go back to your local web browser http://localhost:8080/auth and you can now access the login page and actually login with the username and password you created above.

Sap Java Connector Application in Docker

I am trying to build a sap java connector using springboot. To build this app we need sapjco3.jar and sapjco3.so.
I am using Hibersap library and have added the maven jar dependencies.
In the documentation it says to run the the app we need pass the java.library.path={path to the .so file} in java params.
The application is running fine in linux but I am facing :
Caused by: java.lang.ExceptionInInitializerError: JCo initialization failed with java.lang.UnsatisfiedLinkError: /usr/lib/libsapjco3.so: libuuid.so.1: cannot open shared object file: No such file or directory
when I am trying to run in Docker.
I have added this in my dockerfile. :
VOLUME ["/var/log/hip"]
ADD maven/#file# app.jar
COPY libsapjco3.so /usr/lib/libsapjco3.so
RUN chmod a+x -R /usr/lib/libsapjco3.so
RUN sh -c 'touch /app.jar'
CMD [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar --logging.path=/var/log/hip"]
and I am trying the path using:
System.setProperty("java.library.path","/usr/lib");
As I said, this is working in windows using dll and linux using but it is failing in docker running linux.
I had same problem (WSO2 Docker image and SAP integration), try to install "libuuid" package inside your Docker image. SAP connector was searching for libuuid, so I installed it and it started up. I was using Alpine Linux inside Docker image and this helped:
apk add libuuid

How to deploy web service on Docker container

This is my Dockerfile:
#Build java web app container image
FROM ubuntu
MAINTAINER wangyao
#Make java and tomcat install directory
RUN mkdir /usr/local/java
RUN mkdir /usr/local/tomcat
#Copy jre and tomcat into image
ADD jdk1.8.0_45.jdk /usr/local/java/
ADD apache-tomcat-7.0.62 /usr/local/tomcat/
ENV JAVA_HOME=/usr/local/java/Contents/Home
CMD cd /usr/local/tomcat;./bin/catalina.sh run
#Expose http port
EXPOSE 8080
Then i built it and try to start it:
wangyaos-MBP-3:flexcloud wangyao$ ls
Dockerfile apache-tomcat-7.0.62 jdk1.8.0_45.jdk
wangyaos-MBP-3:flexcloud wangyao$ docker build -t flexcloud .
Sending build context to Docker daemon 355.6 MB
Sending build context to Docker daemon
........
Successfully built 1f824d246b39
wangyaos-MBP-3:flexcloud wangyao$ docker run -i -t flexcloud
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java/Contents/Home
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
/usr/local/java/Contents/Home/bin/java: 20: /usr/local/java/Contents/Home/bin/java: Syntax error: word unexpected (expecting ")")
wangyaos-MBP-3:flexcloud wangyao$
what does word unexpected (expecting ")") mean ?
I used $ ./bin/catalina.sh run without docker, and I can run it, but i why can not run it on the docker container ?
As indicated in the comments, the error Syntax error: word unexpected (expecting ")") is a shell script error, which implies that /usr/local/java/Contents/Home/bin/java is not actually the java executable.
Accessing the container with docker exec -it <container> bash and executing /usr/local/java/Contents/Home/bin/java -version shows us whats actually going on, as per #KingOfSocket's comment.

Resources