0

new for docker implementation, trying after R&D.

TASK TO BE ACHIEVED :- To run the docker after a scheduled time (i.e 5 min) through writing CRON.

I have written a simple java application which just contain a println statement. I want to execute the project using Docker.

My Dockerfile:

FROM openjdk:8-jre-alpine
RUN mkdir /etc/test/
RUN chmod 777 /etc/test/
COPY /home/administrator/Documents/LearningWorkspace/DockerTesting/target/DockerTesting-0.0.1-SNAPSHOT.jar /etc/test/
CMD java -Xmx500M -jar /etc/test/DockerTesting-0.0.1-SNAPSHOT.jar

I executed this command on CMD :- sudo docker build Documents/DockerTest

Processing on cmd :-

Sending build context to Docker daemon 7.159 MB
Step 1/5 : FROM openjdk:8-jre-alpine
 ---> b1bd879ca9b3
Step 2/5 : RUN mkdir /etc/test/
 ---> Using cache
 ---> 4720a4e8fa67
Step 3/5 : RUN chmod 777 /etc/test/
 ---> Using cache
 ---> 91426c394ae4
Step 4/5 : COPY /home/administrator/Documents/LearningWorkspace/DockerTesting/target/DockerTesting-0.0.1-SNAPSHOT.jar /etc/test/
lstat home/administrator/Documents/LearningWorkspace/DockerTesting/target/DockerTesting-0.0.1-SNAPSHOT.jar: no such file or directory

What is the issue exactly with this Docker file.

PROBLEM 2 --

Now , My docker file has been successfully executed and on Running the docker Image the println statement of java application is printed on the console.

Now , I want to execute a cron job on the docker image i.e to run the particular docker after a fixed time.

i changed my docker to this by more of searching and trying :-

CRONTAB FILE:-

*/10 * * * * root java -jar /etc/test/DockerTesting-0.0.1-SNAPSHOT.jar 2>&1

UPDATED DOCKER FILE:-

FROM openjdk:8-jre-alpine
RUN mkdir /etc/test/
RUN chmod 777 /etc/test/
ADD crontab /etc/cron
ADD jar/ /etc/test/
RUN chmod 0600 /etc/cron
CMD java -Xmx500M -jar /etc/test/DockerTesting-0.0.1-SNAPSHOT.jar

But it is not working it prints the statement only ones.

Is there anything I am still missing on the Docker file?

The links i refered to Link 1 Link 2

vartika
  • 33
  • 6
  • Can you please show us the output of `/home/administrator/Documents/LearningWorkspace/DockerTesting/target/*.jar`. Error message `no such file` is quite self-explanatory. – Mike Doe Mar 06 '18 at 08:10
  • on the path there exist a jar file. So why its giving such error. I executed your command then its giving No source files were specified – vartika Mar 06 '18 at 08:28

2 Answers2

1

This happens, because you can't add any files to the Docker container from outside of the build context:

Extended description

The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

Beside your Dockerfile create a directory called jars for instance, put your jar file, then:

ADD jars/DockerTesting-0.0.1-SNAPSHOT.jar /etc/test/

or simply you can copy all jars from there:

ADD jars/ /etc/test/

This way you should end up with a container having /etc/test/DockerTesting-0.0.1-SNAPSHOT.jar inside. Anyway, any reason you put executables inside the /etc directory? I mean this will work, but people expect to have system configuration files there only. More suitable location would be /var or /usr/share, or maybe even /app ;)

Community
  • 1
  • 1
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • 1
    If you have dependencies you would either need to make a fat jar or add all the dependencies as well. – Oleg Sklyar Mar 06 '18 at 08:38
  • how exactly we will be knowing that our docker is running perfectly fine? Where is the place where we can see the loggers of the application generated through the execution of docker file – vartika Mar 06 '18 at 12:53
  • `docker ps` shows running containers. Dockerfile is ever executed, please read how Docker works in the first place. – Mike Doe Mar 06 '18 at 13:18
0

Here is my solution to run Java from a fastapi Docker container (stems from tiangolo/uvicorn-gunicorn:python3.8):

FROM tiangolo/uvicorn-gunicorn:python3.8

LABEL version="1.0"
LABEL description="Install any package using SDKMan"

ENV SDKMAN_DIR /root/.sdkman
# ENV GRAILS_VERSION 3.3.9
ENV JAVA_VERSION 8.0.282-open

RUN apt-get update && apt-get install -y zip curl
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl -s "https://get.sdkman.io" | bash
RUN chmod a+x "$SDKMAN_DIR/bin/sdkman-init.sh"

RUN set -x \
    && echo "sdkman_auto_answer=true" > $SDKMAN_DIR/etc/config \
    && echo "sdkman_auto_selfupdate=false" >> $SDKMAN_DIR/etc/config \
    && echo "sdkman_insecure_ssl=false" >> $SDKMAN_DIR/etc/config

WORKDIR $SDKMAN_DIR
RUN [[ -s "$SDKMAN_DIR/bin/sdkman-init.sh" ]] && source "$SDKMAN_DIR/bin/sdkman-init.sh" && exec "$@"

RUN source /root/.bashrc
RUN source "$SDKMAN_DIR/bin/sdkman-init.sh" && sdk list java && sdk install java $JAVA_VERSION
# RUN source "$SDKMAN_DIR/bin/sdkman-init.sh" && sdk install grails $GRAILS_VERSION

# RUN curl -s "https://get.sdkman.io" | bash
# RUN source "$HOME/.sdkman/bin/sdkman-init.sh"

# RUN sdk install java 8.0.181-oracle

COPY ./app /app
ENV PATH="/root/.sdkman/candidates/java/current/bin/:${PATH}"

WORKDIR /app
RUN pip install -r requirements.txt
0x90
  • 39,472
  • 36
  • 165
  • 245