简体   繁体   English

从 Z0F4137ED15025B504542Z85B 主机向在 docker 容器中运行的 spring api 发出请求

[英]Make a request to a spring api running in a docker container from windows host

So, I searched around for an answer on this matter but either people don't address the issue or they say there's no problem doing this on their computer (mac or linux).所以,我四处寻找关于这个问题的答案,但要么人们没有解决这个问题,要么他们说在他们的计算机(mac 或 linux)上这样做没有问题。 It seems like this might be a windows problem.看起来这可能是 windows 问题。

I have a spring api running on a docker container (linux container).我有一个 spring api 在 docker 容器(Linux 容器)上运行。 I use docker desktop on windows and I'm trying to make a request (in insomnia/postman/wtv) to that api.我在 windows 上使用 docker 桌面,我正在尝试向 api 提出请求(在失眠/邮递员/wtv中)。

If I run the api locally making the following request works perfectly:如果我在本地运行 api,则以下请求可以完美运行:

http://localhost:8080/api/task/

This will list multiples task elements.这将列出多个任务元素。

I've containerized this application like so:我已经像这样容器化了这个应用程序:

Dockerfile Dockerfile

FROM openjdk:11.0.7
COPY ./target/spring-api-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
RUN sh -c 'touch spring-api-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java", "-jar", "spring-api-0.0.1-SNAPSHOT.jar"]

docker-compose.yml docker-compose.yml

version: '3.8'
services:
  api:
    build: .
    depends_on:
      - mysql
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/test?createDatabaseIfNotExist=true
    ports:
      - "8080:80"

  mysql:
    image: mysql
    ports:
      - "3306:3306"
    environment:
    - MYSQL_ROOT_PASSWORD=root
    - MYSQL_USER=root
    - MYSQL_PASSWORD=root
    - MYSQL_DATABASE=test

If I do docker-compose up this works without issue:如果我执行 docker-compose ,则此操作没有问题:

在 docker 容器上运行的 Spring 应用程序

The problem is, if I try to call the same endpoint as before from localhost I don't get any response.问题是,如果我尝试从 localhost 调用与以前相同的端点,我没有得到任何响应。 Insomnia returns an error saying: Error: Server returned nothing (no headers, no data) Insomnia 返回错误:错误:服务器未返回任何内容(无标头,无数据)

I've also tried connecting to the container's ip (got it from docker inspect) but no luck.我也尝试连接到容器的 ip (从 docker 检查获得)但没有运气。

Ports are exposed in docker-compose.yml.端口在 docker-compose.yml 中公开。 What am I missing?我错过了什么?

Thanks in advance.提前致谢。

Port mapping is incorrect.Spring boot application started at 8080 (from the image I see) inside container and it should be mapped to 8080 inside the container.端口映射不正确。Spring 启动应用程序在容器内的 8080(从我看到的图像)开始,它应该映射到容器内的 8080。

It should be like below:它应该如下所示:

ports:
  - "8080:8080"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows上使用Docker开发Spring Boot应用程序,但在Ubuntu VM上运行Docker容器(作为Docker主机) - Developing a spring boot application with docker on windows but run the docker container on a Ubuntu VM (as Docker Host) 将pdf作为在docker容器中运行的spring应用程序的静态资源提供 - Serve pdf as static resource from spring application running in docker container 由于 CORS ZC5FD214CDD0D2B3B422Z2E73B022BA5C,无法使 Spring 引导 API 请求 - Unable To Make Spring Boot API Request Due To CORS Docker Spring 在 docker 容器中运行的应用程序无法访问 - Spring application running in docker container is not reachable 在docker上运行的mysql容器无法与spring boot java api服务连接 - mysql container which is running on docker can't able to connect with spring boot java api service 无法从 postman 向我的 docker spring-boot 容器发送请求 - Can't send request from postman to my docker spring-boot container 使用Spring向一个API发出发布请求 - Make a post request from one api to another using spring Spring Boot、PostgreSQL 和 Docker - 在容器中运行时连接被拒绝 - Spring Boot, PostgreSQL, and Docker - Connection Refused whil Running in Container 在Docker上运行的Spring Cloud Netflix App被容器杀死了吗? - Spring Cloud Netflix apps running on Docker are killed by the container why? Docker 容器运行 Spring 引导应用程序未响应请求 - Docker container running Spring Boot application not answering requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM