Hossam Hany
Hossam Hany

Reputation: 13

Calling rest API through docker container

I have developed backend Rest API using spring boot and frontEnd using angular 8 my question is I have deployed backend and frontend in the same server through docker-compose I need a way to make angular calling rest API through docker containers and network without exposing my backend rest API I have tried to call backend with its container name in angular but it is not working API link example from angular http://shop_soofybackendservice/api/departments

version: '3'

services:
  database:
    image: mysql:8.0.15
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: dbname
    volumes:
      - databasevolume:/var/lib/mysql
    networks:
      - databasenetwork
    deploy:
      mode: replicated
      replicas: 1
      labels: [ database=MYSQL_DATABASE ]


  soofybackendservice:
    image: tomcatsoofybackend:latest
    #build:
    #  context: ./onlineshopping/soofy-trade
    #  dockerfile: Dockerfile
    environment:
      mysqlhost: shop_database
      mysqlupassword: 123456
      mysqluname: root
      imagesPath: /home/ubuntu/images
    volumes:
      - imagesvolume:/home/ubuntu/images
    ports:
      - 81:8080
    depends_on:
      - database
    networks:
      - databasenetwork
      - backendnetwork
    deploy:
      mode: replicated
      replicas: 1
      labels: [ backend=backendWep ]


  #tomcatsoofyfrontend
  #http://backend.medodolphin.com/
  soofyfrontendservice:
    image: mealfrontend:latest
    #build:
    #  context: ./onlineshopping/soofy-frontend
    #  dockerfile: Dockerfile
    ports:
      - 82:80
    depends_on:
      - database
    networks:
      - backendnetwork
    deploy:
      mode: replicated
      replicas: 1
      labels: [ frontend=frontendWeb ]


volumes:
  databasevolume:
  imagesvolume:

networks:
  databasenetwork:
  backendnetwork:

Upvotes: 1

Views: 6118

Answers (2)

jordanvrtanoski
jordanvrtanoski

Reputation: 5537

The proper URL for communication between services (ex. tomcatsoofyfrontend calling the soofybackendservice) is the name of the service in the docker-compose. In your case it's soofybackendservice, therefore the endpoit is http://soofybackendservice:8080/api/departments

If your application is calling the endpoint from the remote browser/host, then you need to call http://<<ip or the domain name of docker host>>:81/api/departments.

Upvotes: 1

thisguy123
thisguy123

Reputation: 945

Angular is likely making client side calls. You need to expose your api and configure the URL accordingly. You'll likely need to specify localhost in your API url.

Upvotes: 1

Related Questions