Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose ${PORT} spring boot application random port in docker?

How to expose ${PORT} spring boot application in docker?

Spring boot application starts on a random port and I have to expose the same in docker.

Is it possible?

like image 522
Thirumal Avatar asked Nov 07 '22 05:11

Thirumal


1 Answers

As mentioned in the comment, better to have a static port on the application side otherwise mapping exact port will be hard.

How to configure port for a Spring Boot application

Another option is to use the host network, so you will be able to access the application using container port, but this option is available for Linux only.

If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.

docker-host-network

docker run -it --net host --rm my_app

suppose your app is running on some random port 8087, then you will able to access the application using http://localhost:8087 because of --net host

like image 126
Adiii Avatar answered Nov 15 '22 07:11

Adiii