Docker Compose:连接两个React应用程序的网络

huangapple go评论63阅读模式
英文:

docker compose: network two react apps

问题

我正在尝试学习Docker Compose网络。我有两个React应用程序,分别在*/one/two*上显示网页。在尝试将它们连接到网络之前,每个应用程序都可以使用Docker Compose分别运行而不出现问题,并使用Compose文件中的以下设置...

ports:
    - 3001:3000

现在,我需要在同一个Docker网络上运行它们。因此,我创建了一个名为common-network的Docker网络,并将该网络引用添加到每个应用程序的Docker Compose文件中。我还将第二个应用程序的Compose文件更新为仅公开其容器端口EXPOSE 3000(请参见底部的文件);否则,如果我保持两者的端口映射都是3001:3000,那么3001端口会出现主机端口冲突。

使用下面的文件,如果我对每个应用程序运行docker-compose up -d --build,我确认我可以在任何容器中无问题地使用docker exec -it <container> ping <service>来ping另一个服务。然而,一旦我在浏览器中导航到*/one*,如果我导航到*/two*,我会得到一个空白页面。

我是不是在错误的方向上?

我需要做什么才能成功地在它们之间导航?

第一个:

version: '3.7'
services:
  page-one-dev:
    container_name: page-one-dev
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    ports:
      - 3001:3000
    environment:
      - CHOKIDAR_USEPOLLING=true
      - DOCKER_CLIENT_TIMEOUT=120
      - COMPOSE_HTTP_TIMEOUT=120
networks: 
  default: 
    external: 
      name: common-network

第二个:

version: '3.7'
services:
  page-two-dev:
    container_name: page-two-dev
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    # ports:
    #  - 3001:3000
    expose:
      - 3000
    environment:
      - CHOKIDAR_USEPOLLING=true
      - DOCKER_CLIENT_TIMEOUT=120
      - COMPOSE_HTTP_TIMEOUT=120
networks: 
  default: 
    external: 
      name: common-network
英文:

I am trying to learn docker-compose networking. I have two react apps that each display a web page at /one and /two, respectively. Before trying to network them, each app ran separately without issue using docker-compose and using this setting in the compose files...

ports:
    - 3001:3000

Now, I need to run them both on the same docker network. So, I created a docker network named common-network and added that network reference to the docker-compose file for each app. I also updated the second app compose file to only expose its container port EXPOSE 3000 (see file at bottom); otherwise if I kept the same port mapping of 3001:3000 for both then there was a host port conflict for 3001.

Using the files below, if I do a docker-compose up -d --build for each app, I confirmed that I can ping the other service from either container without issue using docker exec -it <container> ping <service>. However, once I navigate in a browser to /one, if I navigate to /two I get a blank page.

Am I going about this wrong?

What do I need to do to successfully navigate between them?

first:

version: '3.7'
services:
  page-one-dev:
    container_name: page-one-dev
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    ports:
      - 3001:3000
    environment:
      - CHOKIDAR_USEPOLLING=true
      - DOCKER_CLIENT_TIMEOUT=120
      - COMPOSE_HTTP_TIMEOUT=120
networks: 
  default: 
    external: 
      name: common-network

second:

version: '3.7'
services:
  page-two-dev:
    container_name: page-two-dev
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
   # ports:
    #  - 3001:3000
    expose:
      - 3000
    environment:
      - CHOKIDAR_USEPOLLING=true
      - DOCKER_CLIENT_TIMEOUT=120
      - COMPOSE_HTTP_TIMEOUT=120
networks: 
  default: 
    external: 
      name: common-network

答案1

得分: 0

In this case, since you want both apps to be accessible on the same port and host, you need what's called a "reverse proxy".

The most common ones are nginx, caddy and, particularly interesting in a container environment, traefik.

英文:

In this case, since you want both apps to be accessible on the same port and host, you need what's called a "reverse proxy".

The most common ones are nginx, caddy and, particularly interesting in a container environment, traefik.

huangapple
  • 本文由 发表于 2023年3月15日 21:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定