• docker-compose 启动多个容器 为容器设置自定义网络 互相ping


    1.通过docker network create 已经创建好了一个网络

    ubuntu:ultimate是我自己提交的镜像,集成了一些常用的软件包。download.sh 中的内容是安装nginx,启动然后开始ping test3
    后续的pin.sh pin1.sh pin2.sh分别是 ping nginx1、ping test1、ping test2,最后形成一个环。
    通过networks指定使用的网络是 net111111,这是我自定义的网络

    在这里插入图片描述
    在下面与services对齐的位置配置自定义的网络
    因为是事先建立好的,因此要用external:true
    如果不指定名称,默认就是net111111,就是你在顶级networks下写的这个名字
    在这里插入图片描述
    当然,可以指定名称!下面两种yml文件,第一个是直接写为了net111111,第二个名称改为了net1,但其实使用的还是自定义的net111111网络

    # 1
    version: '3.7'
    services:
      nginx_server:
        image: "ubuntu:ultimate"
        volumes:
          - "/app:/app"
        restart: always
        container_name: nginx1
        privileged: true
        entrypoint: [ "/app/download.sh" ]
        networks:
          - net111111
      test1:
        image: "ubuntu:ultimate"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test1
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net111111
        entrypoint: [ "/app/pin.sh" ]
      test2:
        image: "ubuntu:ultimate"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test2
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net111111
        entrypoint: [ "/app/pin1.sh" ]
      test3:
        image: "ubuntu:ultimate"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test3
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net111111
        entrypoint: [ "/app/pin2.sh" ]
    networks:
      net111111:
        external: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    # 2
    version: '3.7'
    services:
      nginx_server:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: nginx1
        privileged: true
        entrypoint: [ "/app/download.sh" ]
        networks:
          - net1
      test1:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test1
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net1
        entrypoint: [ "/app/pin.sh" ]
      test2:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test2
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net1
        entrypoint: [ "/app/pin1.sh" ]
      test3:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test3
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net1
        entrypoint: [ "/app/pin2.sh" ]
    networks:
      net1:
        external: true
        name: net111111
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    互相ping的结果

    2.在docker-compose.yml中配置网络

    # 3
    version: '3.7'
    services:
      nginx_server:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: nginx2
        privileged: true
        entrypoint: [ "/app/download.sh" ]
        networks:
          - net1
      test1:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test11
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net1
        entrypoint: [ "/app/pin.sh" ]
      test2:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test22
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net1
        entrypoint: [ "/app/pin1.sh" ]
      test3:
        image: "dpdk:v1.0.3"
        volumes:
          - "/app:/app"
        restart: always
        container_name: test33
        privileged: true
        depends_on:
          - nginx_server
        networks:
          - net1
        entrypoint: [ "/app/pin2.sh" ]
    networks:
      net1:
        name: net222222
        driver: bridge
        ipam:
          driver: default
          config:
            - subnet: 172.16.200.0/24
              gateway: 172.16.200.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    创建成功了
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    通过容器名访问一下nginx

    docker exec -it d711 w3m nginx2
    
    • 1

    在这里插入图片描述
    成功!

    参考
    Networking in Compose
    Compose file version 3 reference

  • 相关阅读:
    Java.lang.Class类 getSigners()方法有什么功能呢?
    java 线程的生命周期
    FPGA生成图像Modelsim仿真生成BMP图片保存显示
    2022-2028全球绕线片线圈行业调研及趋势分析报告
    考虑颜色信息的特征描述符----学习笔记
    ffmpeg 合并视频到一个画布
    【Spring 学习系列】Bean 的生命周期之初始化与销毁
    系列五、映射文件xxxMapper.xml
    spring
    340页11万字智慧政务大数据资源平台大数据底座数据治理建设方案
  • 原文地址:https://blog.csdn.net/weixin_43701790/article/details/125534764