Nanohttpd exits immediately when run in Docker container - java

I wrote a simple microservice in Java using nanohttpd for the HTTP server. I am able to run it from the jar file directly from the Windows 10 command prompt like this:
D:\Docker\JoeMicroserviceTest\src>java -jar JoeMicroserviceTest-1.0-SNAPSHOT.jar
The server starts fine and displays:
Server started, Hit Enter to stop.
I can then make HTTP requests against the server, and everything works great.
I then put it in a Docker image, and when I run it, the container starts, the nanohttpd server starts, then immediately stops. This is what I see:
Server started, Hit Enter to stop.
Server stopped.
There is pretty much no delay between the starting and stopping. And then of course my container exits because the server process stopped.
Here is my dockerfile:
FROM openjdk:8
COPY ./src/ /usr/src/JoeMicroserviceTest
WORKDIR /usr/src/JoeMicroserviceTest
CMD ["java","-jar","JoeMicroserviceTest-1.0-SNAPSHOT.jar"]
EXPOSE 8080
I build it like this:
docker build -t joe-microservice-test .
I run it like this:
docker run -p 8080:8080 joe-microservice-test
Why could this be happening?

Ok, I got this working by adding "-dit" to the run command. Here is the working run command:
docker run -dit -p 8080:8080 joe-microservice-test
Here's another question that helps explain why it's needed.

Related

How to start a java service from Jenkins

Jenkins newbie here, I'm using Jenkins to build a SpringBoot app with Maven. What I have done ok so far:
Check out the code
Build the app
Copy the app to app folder.
However i could not complete this step:
Start the app as a server (standalone, not using Tomcat).
I use this command
java -jar app.jar &
but as long as Jenkins finishs the job, the app also quits (I don't see the log shows that the app exits, but when I checked, it did not run)
Jenkins runs on same server with the app (Amazon linux).
Any help is much appreciate.
Try using nohup. Something like this:
killall -9 app.jar
nohup java -jar app.jar > app.log 2>&1 &
But I strongly advise you to create a Docker image with your application to deploy it.
Best regards.

I am able to install docker image & run from dockerhub, but mapping url does not shown (closed)

I m using docker toolbox in Windows 7, and when I run a command like
'docker run -p 5000:5000 -d in28min/todo-rest-api-h2:1.0.0.RELEASE'
, it worked and app executed.
-- Here is shown that app started.
Besides 'docker container ls' command give me running port.
So, when I request to corresponding URL(http://localhost:5000/hello-world), it does not respond (This site can’t be reached). What can be a problem here? As far as I know, that '-p' command allows us to request. Thanks in advance
I found solution. It is because of 'docker-machine ip'. which is different in docker toolbox. Default docker-machine ip is 192.168.99.100, not localhost.

Connecting to Cassandra from inside docker container

My Cassandra instance is running on google cloud platform and I am deploying my application which connects to cassandra in a container. The application works fine when I run it without dockerizing it. Once I deploy it in the container I am getting the below error,
NoHostAvailableException: All host(s) tried for query failed
I tried pinging the IP of the cassandra instance from inside the container and it is not timing out and the ping looks good.
As for the container, I am using the maven:latest image to create a container and run my application using webapp-runner inside the container.
This is my dockerfile
FROM maven:latest
COPY . /tmp
WORKDIR /tmp
RUN mvn clean package
EXPOSE 9042 80
CMD java -jar target/dependency/webapp-runner.jar target/testproject.war
This sounds like a firewall issue. Can you be sure the required ports are open?
http://cassandra.apache.org/doc/latest/faq/index.html?highlight=port#what-ports

Runtime.getRuntime().exec on host from inside a Docker process

I have a Java program that runs inside a Docker container. This program needs to execute a shell command that should be run by the host system, but just calling Runtime.getRuntime().exec(...) executes it inside the Docker container (as it should be).
Is there a way I can start a process from inside a container so the process runs outside? I suspect that the exec command should go via Docker to tell it that the command itself needs to be run on the host, but I'm not sure how to do that.
The idea how to do it can be based on how docker comand line client communicates with docker service. It is just a client, that uses unix socket (i.e. just file) to stream commands to the service. Thus you may connect 1) the service on the host machine via tcp (google for docker TCP socket) 2) may make volumes of docker host machine with docker utility to make them availdable on the docker container like those parameters of docker run on Ubuntu
-v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
that make you possible to exectude docker utility in container and actually all commands will be done in host service

Building a docker image doesnt stop because of minecraft server continuing to run

So I have been trying to learn docker for a few days now and set a first goal for me.
I want to run a spigot server inside a docker container and later on the road combine that with a BungeeCord network.
I have run into problem.
My dockerfile runs without problems but once it reaches the point where it starts the minecraft server, the images stops building.
I think this is due to the server continuing to run and not returning a code 0 to show docker to keep on running.
Am I wrong with my idea, and if not, how can I fix the problem?
Here is my Dockerfile:
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install openjdk-7-jre icedtea-7-plugin -y
RUN apt-get install wget -y
RUN mkdir mc_server && cd mc_server/
RUN wget http://getspigot.org/spigot18/spigot_server.jar
RUN java -Xms1536m -Xmx1536m -Dcom.mojang.eula.agree=true -jar spigot_server.jar nogui
This way the server starts up but docker never finishes building.
I hope I made my problem clear.
Greetings,
Joel
Replace that last RUN with CMD.
RUN / ADD / .. are used to build the static container environment where you want to run your application in. Everything that happens before running the actual application.
CMD and ENTRYPOINT define what's supposed to happen inside the container once you docker run it. This is where the startup script / call goes for the program.
The result of the Dockerfile is similar to a computer that's shut down but has everything installed on the harddrive including a script that autostarts the application. Turn it on and everything starts to run.
PS: https://hub.docker.com/search/?q=spigot&page=1&isAutomated=0&isOfficial=0&starCount=0&pullCount=0 there are several existing images

Categories