2

Good morning everyone, i'm quite new to Docker and i'm trying to learn it with a simple project. Currently i have a client container and a service container, with their respective Dockerfiles as follow:

#Server Dockerfile
FROM openjdk:17

COPY target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]
#Client Dockerfile
FROM node:16-alpine

WORKDIR './src/app'

COPY package.json .
RUN npm install

RUN apk update 
RUN apk add curl

COPY . .
EXPOSE 4200

CMD ["npm","start"]
#docker-compose

version: "0.0.0"

services:
  client:
    build: Ecommerce-client/
    container_name: ecommerce-client
    ports: #per essere chiamato dall'esterno
      - 4200:4200 
    networks:
      - ecommerce
  
  server:
    build: Ecommerce-products/
    container_name: ecommerce-product
    networks:
      - ecommerce
    expose: #la porta che espone all'interno della nostra network
      - "8080"

networks:
  ecommerce:
    driver: bridge

The simple client just show a list of objects sent by the service, in localhost everything works just fine but then when i try with the docker-compose file, i get the error net::ERR_NAME_NOT_RESOLVED.

In my ts client i have one simple call to the service as follow:

this.httpClient.get<any>("http://ecommerce-product:8080/products").subscribe(data => {
      this.products = data;
    });

I tryed to create a bash inside my client and make a call with curl and everything works, but when i open it in my browser it doesn't work. What could be my problem? Thank you!

10
  • And http://ecommerce-product resolves to? Please also show the docker-compose.yml Commented Aug 26, 2023 at 15:15
  • 1
    Check whether or not you forwarded the port to the host machine. The docker-compose file will show you that information. Commented Aug 26, 2023 at 16:04
  • sorry i forgot, i added it with an edit Commented Aug 26, 2023 at 16:52
  • Is "ecommerce-product:8080/products" a valid URL? Am I missing something here? Commented Aug 26, 2023 at 16:58
  • 2
    The browser isn't in a container and can't see the Docker-network host names. This also extends to your front-end application; even if its code is served from a container, the actual application runs in the browser, and outside of Docker. In a developer setup, the easiest thing to do is to add ports: for the backend and connect to localhost and the backend's published port. Commented Aug 26, 2023 at 18:00

1 Answer 1

7

As @David Maze commented: The browser isn't in a container and can't see the Docker-network host names. This also extends to your front-end application; even if its code is served from a container, the actual application runs in the browser, and outside of Docker. In a developer setup, the easiest thing to do is to add ports: for the backend and connect to localhost and the backend's published port

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.