简体   繁体   中英

Docker-compose ERR_NAME_NOT_RESOLVED (Java Spring + Angular)

I have a two simple images:

#Angular image
FROM node:12.2.0

WORKDIR /app

ENV PATH /app/node_modules/.bin:$PATH

COPY package.json /app/package.json
RUN npm install
RUN npm install -g @angular/cli@7.3.9

COPY . /app

CMD ng serve 

&

# Java spring (REST) image
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY ./target/api-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar", "app.jar"]

And my docker-compose:

version: '3'

services:
  web_app_speech:
    image: web_app_speech
    restart: always
    ports:
      - "4300:4200"
    depends_on: 
      - api_speech_docker
  api_speech_docker:
    image: api_speech_docker
    ports:
      - "8080:8080"
    restart: always

$> docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
15c719d31861        web_app_speech      "/bin/sh -c 'ng serv…"   9 minutes ago       Up 9 minutes        0.0.0.0:4300->4200/tcp   azure_web_app_speech_1
044fd15f07e4        api_speech_docker   "java -Djava.securit…"   10 minutes ago      Up 9 minutes        0.0.0.0:8080->8080/tcp   azure_api_speech_docker_1

I can access my REST API from localhost:8080 and my web app from localhost:4300 without problem but when I try to perform a call from my web_app to my rest_api I have the following error:

OPTIONS http://api_speech_docker:8080/speech net::ERR_NAME_NOT_RESOLVED

I have no idea how to fix this, if you need more logs tell me !

Thanks for your help

To my understanding your REST API is being called from your browser which cannot resolve the docker service names. For different applications, I used the following solutions:

Use localhost: http://localhost:8080/speech

  • Works great for locally hosted projects
  • If hosting on the cloud, may cause other errors

or

Hit endpoint through public IP http://13.192.123.12:8080/speech

  • Only works if the IP address does not change
  • If hosting on the cloud, inbound traffic through port 8080 must be allowed as well

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