如何不在 docker swarm 集群外暴露端口?

How not to expose port outside docker swarm cluster?

您好,如何在组合端口中定义仅用于集群内的通信?我不想在集群外公开我的应用程序?

version: "3.8"
services:
  management:
    image: ${IMAGE}
    env_file:
      - vars.env
    networks:
      - my-network
    ports:
      - 8080:80
    deploy:
      placement:
        constraints: [node.role == manager]
      replicas: 1
      update_config:
        parallelism: 2
      restart_policy:
        condition: on-failure

networks:
  my-network:
    external: true
ports:
  - 8080:80

会将我的应用暴露在 swarm 集群之外。如何让我的应用程序只能通过端口 80 在集群内部访问?

只需在 ports 部分省略您不希望暴露在入口网络之外的端口。例子 nginx.yaml:

version: "3.9"
services:
  nginx-1:
    image: nginx:alpine 
  nginx-2:
    image: nginx:alpine

docker stack deploy -c nginx.yaml nginx

docker stack ps nginx

docker exec -t <nginx-1 container ID> wget -qO- nginx-2

该命令将相应地打印从nginx-2 到nginx-1 的响应。不必在 ports 部分下列出端口以访问 within Swarm 网络中的容器端口。

对于您的情况,这意味着您的示例规范不需要 ports 部分,因为您不希望在 Swarm 网络之外公开 80。