Java Spring Boot Random Port with Dockerfile - java

If you're using Java Spring Boot, you can set the server port the application runs on to any port, but also to a random port:
server:
port: 0
However, when I now use a Dockerfile for my application, I won't know what port to EXPOSE. Is there any way to find this port?

Don't use a random port for the Spring app. Use the default port and have Docker expose that as whatever port you like.
Don't use a random port for the Spring app. Specify it via an environment variable and have Docker provide that.

when you started an app, you can see at console I think. As I know docker use 8080 or 8081.
EDIT 1:
Check this -> link
You can expose a port through your Dockerfile or use --expose and then publish it with the -P flag. This will bind the exposed port to your Docker host on a random port (verified by running docker container ls).
You can expose a port through your Dockerfile or use --expose and then publish it with the -p 80:80 flag. This will bind the exposed port to your Docker host on port 80, and it expects the exposed port is 80 too (adjust as necessary with HOST:CONTAINER).
You can ignore exposing anything and just use -p 80:80 in which case this doubles as both exposing AND publishing the port.

Related

Is it possible to run springboot app on port 8081 but use port 8080 as url?

I plan to run my app on port 8081 since port 8080 is used by a separate local tomcat server but users are more concerned about not changing the URL that they are used to. that url includes port 8080 since the legacy app runs on the local tomcat server. Now, would it be possible to connect the new app to port 8081 but just on the url it would still be port 8080?
You need some kind of Proxy for this.
Every Port on your computer can be listened to by one application. Though you need an application that occupies this port and then forwards the request to some other application / port.
This can be done using for example Apache Webserver or Nginx. Or you can write a simple Spring Boot application that does the job: Run (and listen) at port 8080, and then use #Controller logic (or a Filter) to either forward the requests to port 8081 or to the Tomcat port.

Expose random port to docker-compose.yml

I need multiple instance of same application, for that I am using
server.port=0 to run application in random port.
my question is how can I map randomly generated port to docker-compose.yml to create multiple instances.
I am using spring boot at the back-end. I am unable to find any solution.
Any help much appreciated.
Each Docker container runs a single process in an isolated network namespace, so this isn't necessary. Pick a fixed port. For HTTP services, common port numbers include 80, 3000, 8000, and 8080, depending on permissions and the language runtime (80 requires elevated privileges, 3000 is Node's default, and so on). The exact port number doesn't matter.
You access the port from outside Docker space using a published port. If you're running multiple containers, there is the potential for conflict if multiple services use the same host port, which is probably what you're trying to avoid. In the docker run -p option or the Docker Compose ports: setting, it's possible to list only the port running inside the container, and Docker will choose a host port for you.
version: "3"
services:
web:
image: ...
ports:
- "8000" # no explicit host port
command: ... -Dserver.port=8000 # fixed container port
docker-compose port web 8000 will tell you what the host (public) port number is. For communication between containers in the same docker-compose.yml file, you can use the service name and the (fixed, known) internal port, http://web:8000.

Cannot connect jmx to java app running in docker on remote host

Assuming I have a server in my local network with ip 192.168.100.10.
There is docker container running in it with java application.
Now i want to connect to this java application with VisualVM from my computer which has ip address 192.168.100.20. I thought I had everything configured properly but it still does not work.
I have passed these JVM options:
-Dcom.sun.management.jmxremote"
-Dcom.sun.management.jmxremote.port=9010"
-Dcom.sun.management.jmxremote.authenticate=false"
-Dcom.sun.management.jmxremote.ssl=false"
-Dcom.sun.management.jmxremote.local.only=false"
-Dcom.sun.management.jmxremote.rmi.port=9010"
-Djava.rmi.server.hostname=192.168.100.10"
Then I have exposed port 9010 in Dockerfile:
EXPOSE 9010
Then added this port to docker-compose:
ports:
- "9010:9010"
I am trying to connect to remote host with JConsole or VisualVM from my local machine. In "Remote Process" input in JConsole I put "192.168.100.10:9010" but connection fails with error:
"The connection to 192.168.100.10:9010 did not succeed. Would you like to try again?"
What am I doing wrong?
The solution above is sufficient and working. I've been using env variable to set port number which was not working properly.

how to run a jar in amazon Ec2 and access it over the internet

I am fairly new to AWS Instances. I am able to install java in the EC2 instance and able to run my jar. but I am not able to access the jar from the internet. It's a rest service build using spring boot. It run's in tomcat port 8080. But I see the jar running in private mode where it can only be accessed from EC2 instance(from my understanding). But how can I run the jar in the EC2 instance where I can access it over the internet.
Add TCP port 8080 as inbound rule to your security group.
aws ec2 authorize-security-group-ingress \
--group-id $SECURITY_GROUP_ID \
--protocol tcp --port 8080 --cidr 0.0.0.0/0
See also: EC2: How to add port 8080 in security group?

Enable JMX in Tomcat docker container

I'm trying to enable JMX on tomcat docker image using docker-compose.yml but I'm still getting error that VisualVM cannot connect to the JMX.
tomcat:
image: tomcat:8.0-jre8
environment:
CATALINA_OPTS: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
ports:
- "9000:9000"
JMX requires more than just a single port since RMI is also involved. Remote JMX is always a challenge with Tomcat, and using Docker basically makes this "remote" access.
Have a look at Tomcat's JMX Remote Lifecycle Listener to see the port numbers that can be set, and use that listener to set them. If you don't, the RMI server is basically free to use whatever ports it wants to use and you can't predict them.
Once you set those ports, give the port mapping to Docker and you should be good to go.

Categories