为什么有角度的容器需要裸露的端口进行连接? [英] Why a angular container needs a exposed port to connect?

查看:104
本文介绍了为什么有角度的容器需要裸露的端口进行连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个关于理论的问题,我的应用程序运行良好...

this is just a question about theory, my app is running perfectly...

因此,我有3个使用docker-compose运行的服务:a postgres数据库后端springboot 前端角度

So, I have 3 services runing with docker-compose: a postgres database, a backend springboot and a frontend angular.

根据我所知,码头工人容器无需公开端口即可从其他docker容器访问端口,因此无需公开或绑定端口,因为它们都是容器,并且可以使用默认的桥接网络模式相互访问(这就是我学会了,不知道这是否正确)

From what I know, docker containers can acess ports from other docker containers without the need to expose the port, so there is no need to expose nor bind the ports because they are all containers and can access each other with the default bridge network mode (that's what I learned, don't know if this is right).

我只需要从前端容器公开端口,就可以从本地主机访问该端口了。

I only need to expose the port from the frontend container so I can access from my localhost.

问题是:我可以使用后端(backend-> database)来访问数据库,而无需导出任何端口,但是前端(frontend->后端)与nginx一起使用angular,它仅在暴露后端端口的情况下起作用,为什么?

The thing is: I can access the database with the backend (backend -> database) without the need of exporting any ports, but with the frontend (frontend -> backend) using angular with nginx, it only works with the backend port exposed, why?

docker-compose.yml:

version: "3"
services:
  ### DATABASE ###
  db:
    image: postgres:latest
    container_name: mydb
    network_mode: bridge
    environment:
      - POSTGRES_PASSWORD=envpass
      - POSTGRES_USER=envuser
      - POSTGRES_DB=database

    # It works without exposing
    # expose: 
      # - 5432
    # ports:
      # - 5433:5432

  ### BACKEND ###
  backend:
    image: angularback
    container_name: backend
    network_mode: bridge
    expose:
      - 8080
    ports:
      - 8082:8080
    depends_on:
      - db
    links:
      - db

  ### FRONTEND ###
  frontend:
    image: angularfront
    container_name: frontend
    network_mode: bridge
    expose:
      - 80
    ports:
      - 8084:80
    depends_on:
      - backend
    links:
      - backend


推荐答案

您Angular前端正在从前端容器外部向Spring后端发出请求。它是从您的浏览器内部发出的请求。这就是为什么后端也需要公开的原因。

You Angular frontend is making requests to the Spring backend from outside the frontend container. It's making request from inside your browser. That's why the backend also needs to be exposed.

第二,您不需要链接。链接将自动完成,因为两个服务都在同一个网络中。

Second, you don't need links. The linking will be done automatically since both services are in the same network.

这里是一个更新的配置,它改用网络:

Here is a updated config, that uses networks instead:

version: "3"
services:
  ### DATABASE ###
  db:
    image: postgres:latest
    environment:
      - POSTGRES_PASSWORD=envpass
      - POSTGRES_USER=envuser
      - POSTGRES_DB=database
    # Only add the ports here, if you want to access the database using an external client.
    # ports:
      # - "5433:5432"
    networks:
      - backend


  ### BACKEND ###
  backend:
    image: angularback
    ports:
      - "8082:8080"
    depends_on:
      - db
    networks:
      - backend
      - frontend


  ### FRONTEND ###
  frontend:
    image: angularfront
    ports:
      - "8084:80"
    depends_on:
      - backend
    networks:
      - frontend

networks:
  backend:
  frontend:

当不在生产环境中运行时,我还建议将所有端口直接绑定到主机接口(127.0.0.1),以防止网络中的其他人访问计算机上的端口,例如:

When not running in production, I'd also recommend to bind the ports all directly to the host interface (127.0.0.1), to prevent others in your network to access the port on your machine, like this:

ports:
  - "127.0.0.1:8084:80"

这篇关于为什么有角度的容器需要裸露的端口进行连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆