Exposing spring boot random server port using docker











up vote
0
down vote

favorite












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?










share|improve this question


















  • 1




    If you have one application per container, you can use the same port for each of the application. The docker containers have their own separate ports, independent of the host system or other containers. You would then specify the mapping from a host port to the container port when you run the container.
    – dunni
    Jun 24 '16 at 21:23















up vote
0
down vote

favorite












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?










share|improve this question


















  • 1




    If you have one application per container, you can use the same port for each of the application. The docker containers have their own separate ports, independent of the host system or other containers. You would then specify the mapping from a host port to the container port when you run the container.
    – dunni
    Jun 24 '16 at 21:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question













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?







docker spring-boot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 24 '16 at 19:13









Ashish Jain

104311




104311








  • 1




    If you have one application per container, you can use the same port for each of the application. The docker containers have their own separate ports, independent of the host system or other containers. You would then specify the mapping from a host port to the container port when you run the container.
    – dunni
    Jun 24 '16 at 21:23














  • 1




    If you have one application per container, you can use the same port for each of the application. The docker containers have their own separate ports, independent of the host system or other containers. You would then specify the mapping from a host port to the container port when you run the container.
    – dunni
    Jun 24 '16 at 21:23








1




1




If you have one application per container, you can use the same port for each of the application. The docker containers have their own separate ports, independent of the host system or other containers. You would then specify the mapping from a host port to the container port when you run the container.
– dunni
Jun 24 '16 at 21:23




If you have one application per container, you can use the same port for each of the application. The docker containers have their own separate ports, independent of the host system or other containers. You would then specify the mapping from a host port to the container port when you run the container.
– dunni
Jun 24 '16 at 21:23












1 Answer
1






active

oldest

votes

















up vote
1
down vote













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?






share|improve this answer





















  • Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
    – Ashish Jain
    Jun 24 '16 at 20:27










  • I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
    – Ashish Jain
    Jun 24 '16 at 20:28






  • 1




    The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
    – Shibashis
    Jun 24 '16 at 22:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38020567%2fexposing-spring-boot-random-server-port-using-docker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













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?






share|improve this answer





















  • Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
    – Ashish Jain
    Jun 24 '16 at 20:27










  • I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
    – Ashish Jain
    Jun 24 '16 at 20:28






  • 1




    The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
    – Shibashis
    Jun 24 '16 at 22:12















up vote
1
down vote













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?






share|improve this answer





















  • Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
    – Ashish Jain
    Jun 24 '16 at 20:27










  • I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
    – Ashish Jain
    Jun 24 '16 at 20:28






  • 1




    The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
    – Shibashis
    Jun 24 '16 at 22:12













up vote
1
down vote










up vote
1
down vote









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?






share|improve this answer












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?







share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 24 '16 at 19:52









techtabu

2,1851320




2,1851320












  • Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
    – Ashish Jain
    Jun 24 '16 at 20:27










  • I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
    – Ashish Jain
    Jun 24 '16 at 20:28






  • 1




    The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
    – Shibashis
    Jun 24 '16 at 22:12


















  • Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
    – Ashish Jain
    Jun 24 '16 at 20:27










  • I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
    – Ashish Jain
    Jun 24 '16 at 20:28






  • 1




    The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
    – Shibashis
    Jun 24 '16 at 22:12
















Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
– Ashish Jain
Jun 24 '16 at 20:27




Don't we need to specific port to be exposed, at time of docker run command. docker run will start container and at start of container, spring boot app will start.
– Ashish Jain
Jun 24 '16 at 20:27












I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
– Ashish Jain
Jun 24 '16 at 20:28




I also read somewhere, net=host option might work for me. It will expose OS network stack to container env.
– Ashish Jain
Jun 24 '16 at 20:28




1




1




The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
– Shibashis
Jun 24 '16 at 22:12




The comment from @dunni is accurate. You can start the app on same port and let docker expose the service on a random port on the host. docker run -p 3000 my_image will make docker expose the service in a random port.
– Shibashis
Jun 24 '16 at 22:12


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38020567%2fexposing-spring-boot-random-server-port-using-docker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini