How can i access through ssh spring shell cli - java

I am trying to implement a simple cli application ( executable jar file running in linux docker image) using spring-shell library. After i started the Docker image with "docker run -it -p 8080:8080 springshelldemo" command my spring-shell app starts and the cli is available in cmd. How can i access this spring-shell cli from a second cmd using docker exec command (or some other better way) ?
I need this in order to make my app available to more than 1 users at the same time.

I found the answer to this problem. There is this great library on top of spring shell https://github.com/fonimus/ that gives you ssh functionality and with proper docker run file command you can access the spring shell through ssh with all features present.

Related

How can I execute predefined docker command from Java application with DockerClient?

I have Java Maven project with com.github.docker-java dependency.
There is poor documentation and I have no idea, how to call this command:
docker run --rm -v "$(pwd)":/browsertime sitespeedio/browsertime:16.3.0 --video --visualMetrics https://www.sitespeed.io/
I know, that I can exec command directly, but I think that using Java API would be preferred.
So what Java code that uses DockerClient will run command above properly?

Dockerized Java app dies with no error message but runs fine standalone

Please note: I know this questions is very similar to this one however you'll note that the solution in that case was to EXPOSE the port, which I am already doing. Hence although this questions sounds similar, I think its simply a different problem altogether with similar symptoms as the other question.
Docker Version 17.12.0-ce-mac49 (21995) here. I am experimenting with Docker for the first time and have built my first Docker image. My Dockerfile is:
FROM openjdk:8
RUN mkdir /opt/myapp
ADD build/libs/myapp.jar /opt/myapp
ADD application.yml /opt/myapp
ADD logback.groovy /opt/myapp
WORKDIR /opt/myapp
EXPOSE 9200
ENTRYPOINT java -Dspring.config=. -jar myapp.jar
I build it via:
docker build -t myapp .
Everything succeeds. I then tag it as if I'm going to push it to Quay:
docker tag <imageId> quay.io/myregistry/myapp:0.1.0-SNAPSHOT
However before I publish to Quay I want to run it locally to make sure it works:
docker run -it -p 9200:9200 -d --env-file /Users/myuser/myapp-local.env --name myapp myapp
When I run this I get an indication that the container is running, and I can even see it for a few seconds via docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3fa8f7a4288 myapp "/bin/sh -c 'java -D…" Less than a second ago Up 7 seconds 0.0.0.0:9200->9200/tcp myapp
However after a few seconds it stops running and disappears from docker ps altogether:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Furthermore I'm not able to SSH into the container:
docker exec -it f3fa8f7a4288 bash
Error: No such container: f3fa8f7a4288
...or see any logs/console output.
When I run myapp.jar outside of Docker (as a typical Spring Boot app, it starts up and runs beautifully without exceptions). How can I troubleshoot what is going on?
The docker logs command will show you the output a container is generating when you run it detached (with -d). This is likely to include the error message.
docker logs --tail 50 --follow --timestamps container
You can run the image in the foreground without the -d to see the output like when you run myapp.jar outside of Docker.
docker run my/image
So in this specific case:
docker run -it -p 9200:9200 --env-file /Users/myuser/myapp-local.env --name myapp myapp
If I am not mistaken, the issue you are experiencing is because you are using the shell form of ENTRYPOINT. Change it to use the exec version, as follows:
ENTRYPOINT ["java", "-Dspring.config=.", "-jar", "myapp.jar"]
The shell form will launch Java as a separate process just like a shell command. This causes PID 1 to return making Docker believe the container is finished. Using the exec form, the Java process replaces PID 1 and the container will continue running.

Deploying Play project as a service

I am trying to deploy a java web app built using Play.
This link provides a detailed instructions on how to do that.
I successfully managed to follow the instructions to deploy play using the dist command. To run the app I used the command below
play-projects-test-play-1.0-SNAPSHOT/bin/play-projects-test-play
The app is deployed on an Oracle Linux Server release 7.2 which I am connected to through ssh.
However my issue is that when terminating ssh connection would go offline as well. How do I run the it as a service (e.g. service mongod start) ?
You can run the process in background using nohup
nohup ./play-projects-test-play> /dev/null 2>&1 &
Also you can use screen as alternative
screen -A -m -d -S screenname ./play-projects-test-play &
note: you need to install screen
yum install screen

How do write a CSV file in a docker container volume with Java 8?

I am struggling with docker and the file system. I would like to write a file in a docker volume from my Java application. The main goal is that another application running on the same machine can read the file.
I read the related question, but I did not find any answer solving this with a java application. Any idea on how to do this?
Building on the answer to your other question:
How to import a CSV inside a Docker container with Java 8?
Example
So you have two docker containers with a need to share a file system? Assuming your java application is containerized, use it to create a persistent data container:
$ docker create -v /data --name mydata mydockerimage
Run your containerized programs using this data container
$ docker run -it --rm --volumes-from mydata mydockerimage create "/data/myfile.csv"
$ docker run -it --rm --volumes-from mydata mydockerimage read "/data/myfile.csv"
It's possible to pull files out of the data container:
$ docker cp mydata:/data/myfile.csv myfile.csv
Finally you'll want to cleanup the data container eventually
$ docker rm -v mydata
Update
You have not indicated how you're building or using your java program. I have assumed it's an executable jar that can either write or read a CSV file:
java -jar myjar.jar create "/data/myfile.csv"
java -jar myjar.jar read "/data/myfile.csv"
For an example of how to build such a container see:
How to build a docker container for a java app

How to run a Docker image from a Java program?

That's what I would do in the command line:
$ docker run -i imagename mycommand
Should I just use Runtime.getRuntime().exe()? Should I use one of the available Java APIs?
From what I've seen, the APIs would help me to pull and push images, but all I want is to run a particular command on a particular public image, and I don't seem to find an easy way to do that with the APIs.
I'm attaching the actual command I'd be executing, just in case:
$ docker run --rm -p 8080:8080 -i owasp/zap2docker-stable zap.sh -daemon -port 8080
You should think to use java api to manage docker images/container.
You can start with any of them
Java docker-java https://github.com/docker-java/docker-java Active
Java docker-client https://github.com/spotify/docker-client Active
Refer:
Docker Remote API client libraries

Categories