log4j2 logs for Spring boot application inside Docker container - spring-boot

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

Related

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.

How to access local files from docker image running springboot using the file URI?

I have a Spring Boot application running in a docker image. We can access local API's using host.docker.internal, so is there any way to access local files using its URL, i.e. for eg:
file:///Users/ayush.singhal/Downloads/arguments.csv
I know about accessing the file by mounting the volume, but I am trying to do it from internal program itself that is running in the docker image.
You can add a mount point using "volumes" in the docker-compose file that will point to a folder in the host system. By default, docker isolates the container from everything else.

How I can deal with the problem of deployed spring cloud application in docker accessing shared files write and read?

I deployed spring cloud application in docker.The spring cloud application use accessing picture file.When I deployed spring cloud application in docker by dockerfile. In a local development environment,I can access the picture file.When I deployed the application in docker,it cast bug.It shows that the spring cloud application not find the file on the host computer. What do I need to deal with the problem?
I have tried to copy the host computer picture file to docker volumne path. But it can not work.
My host computer of picture file path in my application yml file is like this.
originImgPath: /tmp/tcps/klanalyze/originImg
captchaImgPath: /tmp/tcps/klanalyze/captchaImg
The picture saved on the host computer path is like this.
/tmp/tcps/klanalyze/originImg
/tmp/tcps/klanalyze/captchaImg
My docker file by package is like this.
FROM jdk-8u191:20190321
MAINTAINER beigai_liyang
VOLUME /tmp
ADD target/klanalyze-gateway-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8888
ENTRYPOINT [ "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar" ]
My code is like this.
//read yml setting
#Autowired
private GatewayProperty gatewayProperty;
public void loadPicture(){
……
//load file
File file = new File(gatewayProperty.getOriginImgPath());
……
}
My docker version is 17.12.1-ce.
My spring cloud version is Finchley.SR1.
My Spring boot version is 2.0.3.RELEASE.
My host computer is cent-os 7.
Seems you need use the -v to mount your host direct to /tmp volume.
docker run -v /tmp:/tmp image_name
Concerns:
Not very make sense about "copy the host computer picture file to docker volumne path"
Past your docker run command here, so we could see how you run the container

spring boot logging.file not getting created in openshift

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!

Spring Config Location pointing to a Kubernetes Persistent Volume

Our application landscape consists of Spring Boot apps hosted on docker containers managed by Kubernetes.
In Spring Boot, we use the property "spring.config.location" to specify the external location of the property files. The java command is as follows:
java -jar myproject.jar --spring.config.location={file://some file path}
Now instead of using the local file path, can I create a Kube persistent volume and give that path in the above command?
What Kube volume type should I use to allow for the same semantics of file://{file path} ?
We could successfully read application.properties file from a Kubernetes Persistent Volume using the "spring.config.location" command line argument. The steps we followed were as follows:
Created a persistent volume on Kubernetes (PVC was set to ReadWriteMany option, as multiple microservices will use the same properties file).
Mounted the volume to the Pod of each microservice (changes in the pod script file). Mounted file system accessible via - '/shared/folder/properties' path.
Added the "spring.config.location" parameter to the "java -jar" command - e.g. java -Dspring.profiles.active=$spring_profile -Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -jar myJar.jar --spring.config.location=file:/shared/folder/properties

Resources