简体   繁体   中英

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). It seems like this might be a windows problem.

I have a spring api running on a docker container (linux container). I use docker desktop on windows and I'm trying to make a request (in insomnia/postman/wtv) to that api.

If I run the api locally making the following request works perfectly:

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

This will list multiples task elements.

I've containerized this application like so:

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

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 容器上运行的 Spring 应用程序

The problem is, if I try to call the same endpoint as before from localhost I don't get any response. Insomnia returns an error saying: Error: Server returned nothing (no headers, no data)

I've also tried connecting to the container's ip (got it from docker inspect) but no luck.

Ports are exposed in 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.

It should be like below:

ports:
  - "8080:8080"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM