简体   繁体   English

在谷歌云构建中的两个容器之间进行通信

[英]Communicate between two containers in Google cloud build

I am running my CI/CD pipeline in Google cloud build.我在 Google 云构建中运行我的 CI/CD 管道。 My app has web and wget containers.我的应用程序有webwget容器。 I am trying to reach web from wget我正在尝试从wget访问web

Cloud build internally used cloudbuild bridge network while starting containers as steps.云构建内部使用cloudbuild桥接网络,同时启动容器作为步骤。 So I am expecting these steps to communicate using names.所以我期待这些步骤使用名称进行通信。 But its failing.但它失败了。

If I create my own docker bridge netwok then they communicating.如果我创建自己的 docker bridge netwok,那么它们就会进行通信。

I want to know why cloudbuild network is not working as expected.我想知道为什么 cloudbuild 网络没有按预期工作。

Please let me know if you know any other ways to establish communication between step containers.如果您知道在步骤容器之间建立通信的任何其他方法,请告诉我。

cloudbuild.yaml云构建.yaml

steps:

- name: 'gcr.io/cloud-builders/docker'
  id: Web server
  args: ["run", "-d", "--name", "mani", "manikantanr/hostname_ip"]

- name: 'gcr.io/cloud-builders/wget'
  id: wget web mani:8000
  args: ["-qO-", "http://mani:8000"]

To understand the cloudbuild internals I used few docker commands.为了了解 cloudbuild 的内部结构,我使用了一些 docker 命令。

debug-cloudbuild.yaml调试-cloudbuild.yaml

steps:

- name: 'gcr.io/cloud-builders/docker'
  id: Docker Version
  args: ["version"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker info
  args: ["info"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker volume ls
  args: ["volume", "ls"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker volume inspect homevol
  args: ["volume", "inspect", "homevol"]


- name: 'gcr.io/cloud-builders/docker'
  id: Docker network ls
  args: ["network", "ls"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker network inspect cloudbuild
  args: ["network", "inspect", "cloudbuild"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker ps before
  args: ["container", "ls", "--no-trunc"]

- name: 'gcr.io/cloud-builders/docker'
  id: Web server
  args: ["run", "-d", "--name", "mani", "manikantanr/hostname_ip"]
  # waitFor: ['-']

- name: 'gcr.io/cloud-builders/wget'
  id: wget ipinfo
  args: ["-qO-", "https://ipinfo.io"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker ps after
  args: ["container", "ls", "--no-trunc"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker inspect mani host network
  args: ["inspect", "mani"]

- name: 'gcr.io/cloud-builders/docker'
  id: Docker alpine ifconfig inside container
  args: ["run", "alpine", "ifconfig"]

- name: 'gcr.io/cloud-builders/wget'
  id: wget mani:8000
  args: ["-qO-", "http://mani:8000"]

I had a similar issue setting up integration tests on cloud build.我在云构建上设置集成测试时遇到了类似的问题。 I was trying to run integration tests from another builder (go-builder) against my other containers (started through docker-compose community built containers).我试图从另一个构建器 (go-builder) 对我的其他容器(通过 docker-compose 社区构建的容器开始)运行集成测试。

Without specifying any networks on docker-compose.yaml, all containers are started on the default network ( https://docs.docker.com/compose/networking/ ).无需在 docker-compose.yaml 上指定任何网络,所有容器都在默认网络 ( https://docs.docker.com/compose/networking/ ) 上启动。 On cloud build, it creates a new network named cloudbuild_default and places all my containers there.在云构建中,它创建了一个名为cloudbuild_default的新网络,并将我所有的容器都放在那里。 By forcing all containers to join cloudbuild network through my docker-compose.yaml file, I was able to establish communications and run my tests against them.通过我的 docker-compose.yaml 文件强制所有容器加入cloudbuild网络,我能够建立通信并对它们运行测试。

#docker-compose.yaml

networks:
  default:
    external:
      name: cloudbuild

This might be an alternate configuration for you.这可能是您的替代配置。 Hope it helps希望能帮助到你

From the docs :文档

Each build step is run with its container attached to a local Docker network named cloudbuild.每个构建步骤都在其容器连接到名为 cloudbuild 的本地 Docker 网络的情况下运行。 This allows build steps to communicate with each other and share data.这允许构建步骤相互通信并共享数据。

You can use docker compose and using cloudbuild network, for example:您可以使用 docker compose 和使用cloudbuild网络,例如:

#docker-compose.yml
app-workspace:
  ...
  network_mode: cloudbuild
db-mysql:
  ...
  network_mode: cloudbuild
...
networks:
  default:
    external:
      name: cloudbuild

Or if you are using docker run , add option --network cloudbuild .或者,如果您使用的是 docker docker run ,请添加选项--network cloudbuild

After that, you can communicate to other services you defined in the previous step as you expect.之后,您可以按预期与您在上一步中定义的其他服务进行通信。 For example:例如:

#steps
- id: 'Ping to other container'
  name: gcr.io/cloud-builders/curl
  args: ["app-workspace:your-service-port"]

Hope this helps.希望这可以帮助。

I did an experiment and it looks like (without doing any special setup) you can communicate between build step containers by using the name step_x (0-based numbering).我做了一个实验,看起来(无需进行任何特殊设置)您可以使用名称 step_x(从 0 开始的编号)在构建步骤容器之间进行通信。

For example if you have a web-server listening on the endpoint /hello (on port 8081) in the container for the first build step (step_0).例如,如果您有一个 Web 服务器侦听容器中的端点 /hello(在端口 8081 上)以进行第一个构建步骤 (step_0)。 You can make requests to that endpoint from another build step container by making a request to http://step_0:8081/hello .您可以通过向http://step_0:8081/hello发出请求,从另一个构建步骤容器向该端点发出请求。

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

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