Exposing spring boot random server port using docker

We plan to deploy multiple docker images on same VM running same app. In my knowledge, we need to expose webapp port in docker container. But if we plan to use “random” server port in spring boot (using server.port=0), how we expose that random port at run time?

Second question is, does spring boot app picks random port which is available in host VM or just docker container space?

  • How to create read-only user in the Docker Registry v2?
  • How to run or use multiple registry mirror?
  • Does Docker support SO_REUSEPORT for kernel 3.9+?
  • docker daemon start using ansible
  • docker-compose: using multiple Dockerfiles for multiple services
  • starting container using docker API
  • how to modify files in a container using a script
  • Why does the Symfony URL generator in PHPUnit point to localhost?
  • Multiple versions of PHP through Nginx in Docker
  • mysql cannot be started in Mgt Development Environment
  • Docker compose file memory, cpu limits
  • Dockerizing PostgreSQL - psql Connection refused
  • One Solution collect form web for “Exposing spring boot random server port using docker”

    Spring-boot allocate the port at the run time, not at the build time. That means, the port selected by spring boot will not be available until you start the application natively or in the docker container.

    Answer to your second question is that Docker pick the port inside the Docker Container. So, practically all the ports will be available other than the ones used by OS.

    When combining these two statements, unless you have any specific restrictions, you should specify the port manually to spring-boot application and expose it at docker run execution?

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.