Resolving the Spring Boot and Docker-Compose Port Conflict: Understanding Host Network Mode
Автор: vlogize
Загружено: 2025-09-22
Просмотров: 0
Описание:
Learn how to properly configure your `Spring Boot` application with `Docker-Compose` by fixing port mapping issues and understanding `host network mode`.
---
This video is based on the question https://stackoverflow.com/q/63103787/ asked by the user 'Karthik' ( https://stackoverflow.com/u/4534592/ ) and on the answer https://stackoverflow.com/a/63103850/ provided by the user 'davidxxx' ( https://stackoverflow.com/u/270371/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Spring Boot, Docker-Compose: Always defaults to port 80
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Port Defaulting Issue in Spring Boot with Docker-Compose
When working with Spring Boot and Docker-Compose, it's not uncommon to run into configuration challenges, particularly with how ports are managed. One frequent source of confusion arises when an application seems to ignore the port settings specified in the docker-compose.yml file, defaulting instead to port 80. In this guide, we'll explore why this happens and how you can resolve the issue for a seamless application launch.
The Problem: Application Defaults to Port 80
In the scenario you might have faced, you created a Docker container using a standard Dockerfile provided in the Spring documentation, and your application.properties file was set to run on localhost at port 80. Despite specifying a different port, 8888, in your docker-compose.yml, the application consistently defaults to port 80. Let's break down the reason behind this problem:
Code Snippet Overview
Dockerfile: You would ensure you're running the correct application.
[[See Video to Reveal this Text or Code Snippet]]
Application Properties: Port settings are correctly configured to 80.
[[See Video to Reveal this Text or Code Snippet]]
Docker-Compose File: Indicates port 8888, which should be mapped correctly.
[[See Video to Reveal this Text or Code Snippet]]
The Root Cause: Host Network Mode
The key aspect that causes the application to default to port 80 is the network_mode: host setting in your docker-compose.yml. When you use host network mode, your container essentially shares the host's networking. This means:
Any port configurations in your docker-compose.yml will be superseded by the actual host ports.
Your application binds to the host network directly on port 80, which is why you're seeing this default behavior.
The Solution: Correct Port Mapping
To resolve the port conflict and utilize port 8888 effectively for your application, you need to adjust how you're mapping your ports. Here’s the correct mapping logic to implement within your docker-compose.yml:
Adjusted Code Snippet
[[See Video to Reveal this Text or Code Snippet]]
Explanation:
The line - 8888:80 ensures that requests arriving at port 8888 on the host will be forwarded to port 80 inside the container. This way, you can keep your application running as expected while allowing external access via the specified port, 8888.
Conclusion
By understanding the implications of using host network mode, you can effectively solve port conflicts when running Spring Boot applications inside a Docker container with docker-compose. Remember to adjust your port mappings accordingly to ensure that your applications are accessible in the way you intend. As you continue your journey with Docker and Spring Boot, these insights will aid in creating efficient and error-free deployments.
Feel free to share your experiences or additional questions in the comments below! Happy coding!
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: