[英]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 ,则此操作没有问题:
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.