简体   繁体   中英

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:

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 .

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.

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