spring boot logging.file not getting created in openshift - spring-boot

I want to export the logs of a spring boot application into a file in order to preserve it in a persistent volume.
In application.properties i added logging.file=myapplication.log
When i build and run the docker image locally, the myapplication.log file gets created in the container. But when i push the image to the Openshift internal registry and do
`oc new-app --name=<app> <image-name>`
the container gets created and works fine but the log file does not exist. I also tried inserting -Dlogging.file=myapplication.log in the dockerfile which also works locally but not in openshift.
What i am doing wrong! I am going insane!

Related

Environment variables for Spring Cloud Config in Docker

So, I am learning about microservices (I am a beginner) and I'm facing an issue. I've went through the Spring Cloud Config and Docker docs hoping to find a solution, but I didn't.
I have an app with 3 microservices (Spring Boot) and 1 config server (Spring Cloud Config). I'm using a private Github repository for storing config files and this is my application.properties file for config server:
spring.profiles.active=git
spring.cloud.config.server.git.uri=https://github.com/username/microservices-config.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.git.username=${GIT_USERNAME}
spring.cloud.config.server.git.password=${GIT_ACCCESS_TOKEN}
I have a Dockerfile based on which I have created a Docker image for config server (with no problems). I created a docker-compose.yml which I use to create and run containers, but it fails because of an exception in my cloud config app. The exception is:
org.eclipse.jgit.api.errors.TransportException: https://github.com/username/microservices-config.git: not authorized
Which basically means that my environment variables GIT_USERNAME and GIT_ACCCESS_TOKEN (that I set up in Intellij's "Edit configuration" and use in application.properties) are not available for the config server to use in a container.
The question is: Do I need to somehow add those environment variables to .jar or to Docker image or to Docker container? Like I'm not sure how do I make them available for the config server to use in a container.
Any help or explanation is welcomed :)

How to mount files from Docker image to local machine

I wanted to externalize application.properties of spring boot using docker, so instead of keeping inside the container I wanted to outside the container.

Spring Cloud Config Server Not picking changes

I am trying to setup spring config cloud using local file system.
Below is my config on cloud server.
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/
Bootstrap.properties on client app is as shown below
spring.application.name=hello-world
spring.cloud.config.uri=http://localhost:8888
management.endpoints.web.exposure.include=*
I have also created hello-world.yml on class path for the hello-world spring boot application with property, test: Hello World
Followed below steps to make use of config server.
Step 1: Update the config file and start cloud config server. Able to
see config http://localhost:8888/hello-world/default
Step 2: Start client app hello-world, client app able to read the
test property file from cloud config server.
Step 3: Make changes to config by updating test: Good Bye on
hello-world.yaml.
At this moment, if I check
http://localhost:8888/hello-world/default ,it still shows old value.
Step 4: Run /actuator/refresh on client app. But it won't detect
any change on config server.
The new changes are reflected only if I restart the cloud config server.
Is there any configuration issue causing the cloud config server to unable to listen to changes ?
I could see o.s.cloud.commons.util.InetUtils : Cannot determine local hostname INFO log on cloud config app.
First of all I followed the same exact steps you followed and got the same issue, after almost day of search and study on the matter found out the followings,
we shouldn't use classpath:/<whatever> for
spring.cloud.config.server.native.searchLocations
because when we use so and build the project and run the location refers to the directory inside the generated .jar file, so we will not be able to update it in runtime.
To confirm this you can stop config server, open you .jar archive and delete hello-world.yml file then try http://localhost:8888/hello-world/default you will get default null responses
So we have to use some other locations for spring.cloud.config.server.native.searchLocations either with full directory path or just directory from app running location
Examples
For full path in windows use file:///full-path
spring.cloud.config.server.native.searchLocations: file:///E:\configs
Just for a directory (which will search project root directory if you running from IDE, if running jar then target directory or jar location spring.cloud.config.server.native.searchLocations: configs
spring.cloud.config.server.native.searchLocations: configs\whatever
we can configure multiple locations too as follows, spring.cloud.config.server.native.searchLocations: file:///E:\configs, configs

log4j2 logs for Spring boot application inside Docker container

I have a spring boot application which uses log4j2 to write logs. When I deploy this application to a Docker container, I want the logs to be written to a file at a specified location outside the container. How can I do this ? I tried providing the path of the log folder using an environment variable on startup but its of no use. No logs are being written. Please help
You have to mount the directory on the host file system inside the container. A simple web search will yield many results on how to do this such as How to mount a host directory in a Docker container

spring boot log file being overwritten on restart

I'm using https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html to start my spring-boot application on ubuntu 14.04 and it's working great except my log file gets overwritten whenever I restart the app. Probably it's something simple... but how can I configure that behaviour? I'm just using the default console logging to /var/log/app_name.log. If I pass in the logback "--logging.file=..." config to the init script, it logs both to the default as well as this file and this file doesn't get overwritten. Thanks!

Resources