1

I am a beginner for docker implementation, I have created a simple "hello world" example to test my docker,

 package com.example.demo;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class DockerTestingApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(DockerTestingApplication.class, args);
        String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
        Logger logger = LoggerFactory.getLogger(DockerTestingApplication.class);
        logger.info(timeStamp +" :: Docker File is running Ok");
    }
}

When I run the application locally it works fine and it logs it perfectly in the log file.

Now, the issue is when i try to run in the application inside a docker, with cron tab added on it. On console the application(jar ) is executed as I assume it is as I have given a cron time period of 5 minutes and after every 5 min it exectues the jar. But how I will verify its working?

  1. Where to log the logs of the application process?
  2. How to give the locations of logs?

DOCKER FILE :-

FROM openjdk:8-jre-alpine
RUN apk add --no-cache bash
ENV LOG_LOCATION /var/log/applogs
RUN rm -rf /var/cache/apk/* \
    && mkdir /etc/test && mkdir /var/log/applogs
ADD crontab /etc/cron.d/crontab
ADD jar/ /etc/test/
CMD chmod +777 /var/log/applogs
RUN crontab /etc/cron.d/crontab
CMD ["/usr/sbin/crond", "-f", "-d", "0"]

CRONTAB:-

#!/bin/bash
*/5 * * * * root java -Dlogfile.location="/var/log/applogs/Log.log" -jar /etc/test/DockerTesting-0.0.1-SNAPSHOT.jar
# An empty line is required at the end of this file for a valid cron file.

I even logged in the docker container and reached the log location I have given but it doesn't creates the log.log file , I have searched a lot but didn't reached the solution.

Hope to find the solution.

vartika
  • 33
  • 6
  • have you confirmed "/var/log/applogs" is this location exists? have you tried to manually run this jar file inside docker container? – Rohan J Mohite Mar 12 '18 at 13:21
  • yes i tried to run the docker image , it just shows the command executing as per the cron tab, but i am unable to see the output of the application – vartika Mar 12 '18 at 13:59

1 Answers1

1

Docker will always redirect the STDOUT to the container log location . In simple terms if you want to read the logs you can get them by running the command.

docker logs $container_id_or_container_name

Further the actual location can be retrieved from this commmand

docker inspect --format='{{.LogPath}}' $ontainer_id_or_container_name

More info here stackoverflow question

Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34
  • But these are the logs of docker not the application log where i want the logger.info to get logged. – vartika Mar 12 '18 at 11:51