Kubernetes-client API to run and expose a docker image - java

I'm having trouble finding an easy way to automate the following kubectl command lines using Kubernetes-client Java API :
$ kubectl run foo --image=bar/baz:v1 --port 8080
$ kubectl expose deployment foo --type=LoadBalancer --port 80 --target-port 8080
The first one runs a docker image on port 8080 and the second one defines it as a load balancer and expose it to port 80.

Adding -v=9 shows the API requests made by those commands

Related

Docker for an Angular-Java app : ports not exposed properly

I am trying to dockerize a full-stack Java(Springboot)-Angular app. The Angular app is embedded in the application and makes REST calls to the Java app as GET localhost:8080/getSomeInfo. My Dockerfile is as
FROM maven:3.6.3-openjdk-8 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
FROM openjdk:8-jdk-alpine
COPY --from=build /home/app/target/*.jar /usr/local/lib/app.jar
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} /usr/local/lib/app.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/app.jar"]
FROM node:12.2.0
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY src/main/web/package.json /app/package.json
RUN npm install
RUN npm install -g #angular/cli#10.1.2
COPY src/main/web /app
CMD ng serve --host 0.0.0.0
I build it as
docker build -t springio/myapp .
I need to expose both the angular port and the java port, so I run it as
docker run -p 9898:4200 -p 8080:8080 -t springio/myapp
I am able to bring up the web page, but it cannot reach out to the Java server (can't connect to 8080). How can this be fixed please?
I have tried putting 'EXPOSE 8080' in the dockerfile, but that did not work
You should not put 2 applications in the same container. Although you can, it is considered a bad practise and it defeats the purpose of using containers.
I suggest having 2 Dockerfile: one for the back-end (java app) and one for the front-end (Angular app). Build each of them separately and start them independently.
For your specific problem I don't think the port is the problem, but is the Java app that is not even starting (it is actually not even present in the final image). Splitting the Dockerfile and starting the apps separately will fix your issue.
EXPOSE doesn't actually do anything. It is there to inform users of the Dockerfile which ports the application is listening on, so that they know which ports to map on the host.

Connect to a multi node Couchbase cluster on Docker in Java

I created a first couchbase server with the following command:
docker run -d --name db1 -p 8091-8094:8091-8094 -p 11210:11210 couchbase
The second server I created without the parameters for port because otherwise docker can't deploy the container:
docker run -d --name db2 couchbase
Now I can access the web console of the first container and create a cluster with the second container.
The problem is that I can't connect to the created cluster in Java because port 11210 is not published for the second container. I'm always getting a TimeoutException. How can I solve this?
You will have to expose the ports for the second container too, just as you did in first container (those "parameters" are port mappings).
Most likely, you were unable to run the second container with parameters because you tried mapping the same ports as you did in first container; aka, you tried this command:
docker run -d --name db2 -p 8091-8094:8091-8094 -p 11210:11210 couchbase
This wouldn't work because you cannot use ports that are already being used. So try mapping to different set of ports. For example:
docker run -d --name db2 -p 9091-9094:8091-8094 -p 11210:11210 couchbase
Notice how I am now using 9091-9094 instead of 8091-8094. The above command means: map ports 8091-8094 from the container to ports 9091-9094 in the host. Any requests sent to 9091-9094on host will be forwarded to 8091-8094 inside container automatically.
And then you can connect to your second container by specifying the new ports (9091-9094).
I suggest you to check the docker basics, especially the documentation for port mapping.
Finally I could solve the problem by setting up virtual machines for the servers with vagrants. This way it is no problem to have multiple Couchbase instances with the same ports.

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

java.net.UnknownHostException on Docker

I am trying to create docker containers for ZooKeeper and configure them in cluster mode (full code is here and here).
Containers are based on Alpine Linux (alpine:3.2 on Docker Hub), but the problem that I'm going to describe happens also with the official Java container (java:7).
I use the following commands to start the cluster:
docker run -d -h zk1 --name zk1 dockmob/zookeeper -s zk1,zk2,zk3
# wait some time ...
docker run -d -h zk2 --name zk2 dockmob/zookeeper -s zk1,zk2,zk3
docker run -d -h zk3 --name zk3 dockmob/zookeeper -s zk1,zk2,zk3
(They are available on docker hub, you can try them).
If I wait some time before starting the second and third containers, then the host names zk2 and zk3 are put in /etc/hosts too late (by docker), and Java is unable to find them: I get java.net.UnknownHostException in the logs of zk1 for both zk2 and zk3.
I found on the web that I need to disable JVM DNS cache in order to refresh the host names, so I introduced the following command in the Dockerfile in order to update the java.security settings:
RUN grep '^networkaddress.cache.ttl=' /usr/lib/jvm/java-1.7-openjdk/jre/lib/security/java.security || echo 'networkaddress.cache.ttl=10' >> /usr/lib/jvm/java-1.7-openjdk/jre/lib/security/java.security
It sets the DNS TTL property (networkaddress.cache.ttl) to 10 seconds.
The variable networkaddress.cache.negative.ttl is already set to its default value (10).
The behavior does not change. I get lots of java.net.UnknownHostException repeatedly.
What can be the cause of the problem?
In my case the java application was failing with java.net.UnknownHostException when running in docker. The reason was that I used --network=none docker flag (getting ip/hostname via dhcp and pipework). In this case, docker does not add automatically to /etc/hosts entry like
127.0.0.1 15e326aecf84
And getCanonicalHostName() Java function threw this exception.
Possible solutions:
add hostname entry to /etc/hosts file via docker run parameter --hostname=your-hostname.com
switch to docker-managed network configuration
I managed to get rid of the DNS issues by switching to Oracle JRE 8 and using the following hack in the Dockerfile:
RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf
I created a working Java 8 docker container container on Docker Hub (the code is on github).

Docker running nginx plus jar

I'm trying to run a docker container that contains both a java jar server and nginx in front of it to perform subdomain->port forwarding, and I don't seem to be setting it up correctly.
This is my Dockerfile:
FROM java:8
MAINTAINER somefool
RUN apt-get update
RUN apt-get -y install nginx
COPY theBigOwlServer.jar /data/server.jar
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80 8080
CMD java -jar /data/server.jar
CMD service nginx start #<--- line AAA
The java jar listens on ports 8080 and 8090. When I run this container with -p 80:80 -p 8080:8080, the jar just doesn't seem to start. I don't see any console output from it, and I can't reach it from outside the container with curl localhost:8080. I can reach nginx on port 80, but requests that should be forwarding to the jar are coming back with an empty reply.
However, if I comment out line AAA, then the jar starts fine. It generates console output and curl localhost:8080 reaches it. How can I run nginx and the jar together?
Docker containers are designed for single-process sandboxing, so only take one CMD argument. In this case it's just picking up the last one in the file. If you need to run multiple prorcesses in a container (and sometimes it makes sense to do so) then use something like Supervisor to run your commands for you (so your CMD would run Supervisor). Then you get goodies like process watchdogs and such thrown in too.
You could do something like:
ENTRYPOINT sh -c 'service nginx start && java -jar /data/server.jar'
I tried this out and it worked for me.

Categories