简体   繁体   English

如何使 docker-compose 服务可以相互访问?

[英]How to make docker-compose services accesible with each other?

I'm trying to make a frontend app accesible to the outside.我正在尝试使前端应用程序可以访问外部。 It depends on several other modules, serving as services/backend.它依赖于其他几个模块,用作服务/后端。 This other services also rely on things like Kafka and OpenLink Virtuoso (Database).其他服务也依赖于 Kafka 和 OpenLink Virtuoso(数据库)之类的东西。

How can I make all of them all accesible with each other and how should I expose my frontend to outside internet?我怎样才能让所有这些都可以相互访问,我应该如何将我的前端暴露给外部互联网? Should I also remove any "localhost/port" in my code, and replace it with the service name?我是否还应该删除代码中的任何“localhost/port”,并将其替换为服务名称? Should I also replace every port in the code for the equivalent port of docker?我是否还应该将代码中的每个端口都替换为 docker 的等效端口?

Here is an extraction of my docker-compose.yml file.这是我的 docker-compose.yml 文件的摘录。

version: '2'
services:
    zookeeper:
        image: confluentinc/cp-zookeeper:latest
        environment:
          ZOOKEEPER_CLIENT_PORT: 2181
          ZOOKEEPER_TICK_TIME: 2000
        ports:
          - 22181:2181

    kafka:
        image: confluentinc/cp-kafka:latest
        depends_on:
          - zookeeper
        ports:
            - 29092:29092
        environment:
          KAFKA_BROKER_ID: 1
          KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
          KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
          KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
          KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
          KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
   frontend:
       build:
          context: ./Frontend
          dockerfile: ./Dockerfile
       image: "jcpbr/node-frontend-app"
       ports:
          - "3000:3000"
       # Should I use links to connect to every module the frontend access and for the other modules as well?
       links: 
           - "auth:auth"
    auth:

      build:
          context: ./Auth
          dockerfile: ./Dockerfile
      image: "jcpbr/node-auth-app"
      ports:
          - "3003:3003" 
   (...)

How can I make all of [my services] all accesible with each other?我怎样才能使所有 [我的服务] 都可以相互访问?

Do absolutely nothing.绝对什么都不做。 Delete the obsolete links: block you have.删除过时的links:阻止你。 Compose automatically creates a network named default that you can use to communicate between the containers, and they can use the other Compose service names as host names; Compose 会自动创建一个名为default的网络,您可以使用该网络在容器之间进行通信,并且它们可以使用其他 Compose 服务名称作为主机名; for example, your auth container could connect to kafka:9092 .例如,您的auth容器可以连接到kafka:9092 Also see Networking in Compose in the Docker documentation.另请参阅 Docker 文档中的 Compose 网络

(Some other setups will advocate manually creating Compose networks: and overriding the container_name: , but this isn't necessary. I'd delete these lines in the name of simplicity.) (其他一些设置将提倡手动创建 Compose networks:并覆盖container_name: ,但这不是必需的。为了简单起见,我会删除这些行。)

How should I expose my frontend to outside internet?我应该如何将我的前端暴露给外部互联网?

That's what the ports: ['3000:3000'] line does.这就是ports: ['3000:3000']行所做的。 Anyone who can reach your host system on port 3000 (the first port number) will be able to access the frontend container.任何可以通过端口 3000(第一个端口号)访问您的主机系统的人都可以访问frontend容器。 As far as an outside caller is concerned, they have no idea whether things are running in Docker or not, just that your host is running an HTTP server on port 3000.就外部调用者而言,他们不知道事情是否在 Docker 中运行,只是您的主机在端口 3000 上运行 HTTP 服务器。

Setting up a reverse proxy, maybe based on Nginx, is a little more complicated, but addresses some problems around communication from the browser application to the back-end container(s).设置反向代理,可能基于 Nginx,稍微复杂一些,但解决了从浏览器应用程序到后端容器通信的一些问题。

Should I also remove any "localhost/port" in my code?我还应该删除代码中的任何“localhost/port”吗?

Yes, absolutely.是的,一点没错。

...and replace it with the service name? ...并将其替换为服务名称? every port?每个端口?

No, because those settings will be incorrect in your non-container development environment, and will probably be incorrect again if you have a production deployment to a cloud environment.不,因为这些设置在您的非容器开发环境中将不正确,并且如果您将生产部署到云环境,则可能再次不正确。

The easiest right answer here is to use environment variables.这里最简单的正确答案是使用环境变量。 In Node code, you might try在 Node 代码中,您可以尝试

const kafkaHost = process.env.KAFKA_HOST || 'localhost';
const kafkaPort = process.env.KAFKA_PORT || '9092';

If you're running this locally without those environment variables set, you'll get the usually-correct developer defaults.如果您在没有设置这些环境变量的情况下在本地运行它,您将获得通常正确的开发人员默认设置。 But in your Docker-based setup, you can set those environment variables但是在基于 Docker 的设置中,您可以设置这些环境变量

services:
  kafka:
    image: confluentinc/cp-kafka:latest
    environment:
       KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092  # must match the Docker service name
  app:
    build: .
    environment:
      KAFKA_HOST: kafka
      # default KAFKA_PORT is still correct

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM