首页 > 其他分享 >How to link multiple docker-compose services via network

How to link multiple docker-compose services via network

时间:2024-07-12 23:41:05浏览次数:17  
标签:compose multiple network ping services service2 docker

How to link multiple docker-compose services via network

New scenario: Sharing a network between services

If you want define services in multiple docker-compose.yml files, and also have network connectivity between the services, you need to configure your services to use the same network.

To create an external network, you can run docker network create <name>. -- where <name> can be a single string without spaces.

Creating the network

$ docker network create external-example
2af4d92c2054e9deb86edaea8bb55ecb74f84a62aec7614c9f09fee386f248a6

Modified first docker-compose file with network configured

version: '3' 
services: 
  service1: 
    image: busybox 
    command: sleep infinity 

networks: 
  default: 
    external: 
      name: external-example 

Restarting the services

$ pwd
/tmp/docker-example/compose1

$ docker-compose up -d
Creating compose1_service1_1 ... done

Modified second docker-compose file with network configured

version: '3' 
services: 
  service2: 
    image: busybox 
    command: sleep infinity 

networks: 
  default: 
    external: 
      name: external-example 

Restarting the services

$ pwd
/tmp/docker-example/compose2

$ docker-compose up -d
Creating compose2_service2_1 ... done

After running docker-compose up -d on both docker-compose.yml files, we see that no new networks were created.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
25e0c599d5e5        bridge              bridge              local
2af4d92c2054        external-example    bridge              local
7df4631e9cff        host                host                local
194d4156d7ab        none                null                local

With the containers using the external-example network, they are able to ping one another.

Test: Service 1 ping Service 2

# By service name
$ docker exec -it compose1_service1_1 ping service2
PING service2 (172.24.0.3): 56 data bytes
64 bytes from 172.24.0.3: seq=0 ttl=64 time=0.054 ms
^C
--- service2 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.054/0.054/0.054 ms

# By container name
$ docker exec -it compose1_service1_1 ping compose2_service2_1
PING compose2_service2_1 (172.24.0.2): 56 data bytes
64 bytes from 172.24.0.2: seq=0 ttl=64 time=0.042 ms
^C
--- compose2_service2_1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.042/0.042/0.042 ms

Test: Service 2 ping Service 1

# By service name
$ docker exec -it compose2_service2_1 ping service1
PING service1 (172.24.0.2): 56 data bytes
64 bytes from 172.24.0.2: seq=0 ttl=64 time=0.041 ms
^C
--- service1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.041/0.041/0.041 ms

# By container name
$ docker exec -it compose2_service2_1 ping compose1_service1_1
PING compose1_service1_1 (172.24.0.3): 56 data bytes
64 bytes from 172.24.0.3: seq=0 ttl=64 time=0.042 ms
^C
--- compose1_service1_1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.042/0.042/0.042 ms

标签:compose,multiple,network,ping,services,service2,docker
From: https://www.cnblogs.com/lightsong/p/18299552

相关文章

  • 常用koishi-plugin-network-data-getter api推荐
           随机美少女{"command":"随机美少女","desc":"","sourceUrl":"http://api.weboss.hk/random/api.php","sendType":"image","dataType":"resource"......
  • NETWORK [4] HTTP与HTTPS 协议
    HTTP协议概述        HTTP(HyperTextTransferProtocol,超文本传输协议)是用于分布式、协作式和超媒体信息系统的应用层协议。它是互联网上最广泛使用的协议之一,主要用于从万维网(WWW)服务器传输超文本到本地浏览器。HTTP是一个基于请求-响应模型的无状态协议,通常运行在TC......
  • KAN: Kolmogorov-Arnold Networks (arXiv 2024)
    KAN官方代码库:https://github.com/KindXiaoming/pykan官方tutorials:https://kindxiaoming.github.io/pykan/目录AbstractKolmogorov–ArnoldNetworks(KAN)Kolmogorov-ArnoldRepresentationtheoremKANarchitectureImplementationdetailsKAN’sApproximation......
  • composer优化php项目
    composerdump-autoload--optimizecomposerinstall--no-dev--prefer-dist--prefer-stable这两个命令是在使用Composer时常用的,Composer是PHP的依赖管理工具。下面是这两个命令的解释:1.`composerdump-autoload--optimize`:-`composerdump-autoload`:这个命令会重......
  • TextCNN: Convolutional Neural Networks for Sentence Classification
    本文是CNN应用在NLP领域的开山之作。TextCNN的成功并不是网络结构的成功,而是通过引入已经训练好的词向量在多个数据集上达到了超越benchmark的表现,证明了构造更好的embedding,是提升NLP各项任务的关键能力。作者做了一系列实验,这些实验使用卷积神经网络(CNN)在预训练的词向量之上......
  • 容器之docker compose
    DockerCompose是一个用于定义和运行多容器Docker应用的工具。通过一个YAML文件,您可以配置应用程序需要的所有服务,并使用单个命令来创建和启动这些服务。以下是对DockerCompose的详细介绍:核心概念服务(Services)服务是一个运行容器的抽象。每个服务都运行一个镜像,并且......
  • docker-compose创建haproxy教程
    本文主要讲解通过docker-compose创建haproxy并进行代理一、haproxy简介HAProxy是一款基于事件驱动、单进程模型设计的四层与七层负载均衡器,它能够在TCP/UDP层面以及HTTP(S)等应用层协议上实现高效的流量分发。HAProxy不仅适用于Web服务器负载均衡,还能应用于数据库、邮件服务器......
  • 读论文《OSCNet: Orientation-Shared ConvolutionalNetwork for CT Metal Artifact Le
    论文题目:面向共享的CT金属伪影学习卷积网络论文主题:金属伪影去噪论文地址:OSCNet_TMI2023.pdf-Google云端硬盘这个是oscnet+,oscnet进阶版,感觉和acdnet很像其实,本文读论文,下一篇博客讲讲复现摘要:        在本文中,我们仔细研究了具有旋转对称条纹图案的金属工件......
  • docker-compose vs docker-stack
    docker-composevsdocker-stack都是docker两个容器编排工具,docker-compose是属于第三方容器编排工具需要单独安装,docker-stack是docker内置容器编排工具。docker-compose一般配合K8S使用,目前要容器管理方面K8S有着比较明显的优势,所以docker-compose目前比较流行。docker-stac......
  • [论文阅读] Drawing and Recognizing Chinese Characters with Recurrent Neural Netw
    Pretitle:DrawingandRecognizingChineseCharacterswithRecurrentNeuralNetworksource:TPAMI2018paper:https://arxiv.org/abs/1606.06539code:https://github.com/YifeiY/hanzi_recognitionref:https://blog.csdn.net/qq_35337126/article/details/8378776......