You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows宿主机请求Docker容器中Spring API无响应问题求助

Fix for Unresponsive Containerized Spring API on Windows Docker Desktop

Alright, let's figure out why your containerized Spring API isn't responding when you hit it from your host machine. The main issue here is a port mismatch in your Docker Compose setup, and it's an easy fix!

1. The Port Mapping Mistake

Look at your docker-compose.yml for the api service:

ports:
  - "8080:80"

This line tells Docker to map your host's port 8080 to the container's port 80. But here's the problem: your Spring Boot application is defaulting to listening on port 8080 inside the container (since you haven't changed the server port via application.properties or startup arguments).

So when you send a request to http://localhost:8080/api/task/ from your host, it's trying to connect to port 80 in the container—where nothing is running. That's why you get the "Server returned nothing" error.

Fix It

Update the ports section to map host port 8080 to container port 8080:

ports:
  - "8080:8080"

2. Verify the Fix (and Debug Further if Needed)

After updating the config, stop and restart your containers with:

docker-compose down && docker-compose up --build

Then:

  • Test the endpoint again at http://localhost:8080/api/task/—it should work now.
  • If it still doesn't respond, check if your Spring app actually started properly in the container. Run this command to view the logs:
    docker logs <your-api-container-name>
    
    Look for a line like Started SpringApiApplication in X seconds (JVM running for Y)—that confirms the app is up. If you see database connection errors, you might need to adjust your SPRING_DATASOURCE_URL or wait a few seconds for MySQL to fully initialize (since depends_on only waits for the container to start, not the database service).
  • You can also check which ports the app is listening on inside the container. Exec into the container:
    docker exec -it <your-api-container-name> sh
    
    Then install net-tools (if missing) and check listening ports:
    apt update && apt install net-tools
    netstat -tulpn
    
    You should see java listening on port 8080.

3. Bonus: Optional Port Configuration

If you want your app to run on port 80 inside the container (to match your original mapping), you can modify your Spring Boot setup:

  • Add server.port=80 to your application.properties file, or
  • Update the entrypoint in your Dockerfile to pass the port as an argument:
    ENTRYPOINT ["java", "-jar", "-Dserver.port=80", "spring-api-0.0.1-SNAPSHOT.jar"]
    
    Either way, keep the ports: - "8080:80" mapping in your Compose file.

内容的提问来源于stack exchange,提问作者Tiago Martinho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 12:07:49