美文网首页
docker-compose links及external_li

docker-compose links及external_li

作者: ArtisCoder | 来源:发表于2019-08-28 22:52 被阅读0次

    links

    version: "3.4"
    services:
      redis-web:
        image: redis-web:1.0
        container_name: redis-web
        restart: always
        environment:
          REDIS_HOST: redis
        ports:
          - 8001:8001
        # 链接到其它容器,并启别名redis
        links:
          - redis-app:redis
      redis-app:
        image: redis:latest
        container_name: redis-app
        restart: always
    

    进入容器,发现可以成功ping通redis

    [root@vm02 test]# docker exec -it 31e0f35a1f77 bash
    root@31e0f35a1f77:/# ping redis
    PING redis (172.19.0.2) 56(84) bytes of data.
    64 bytes from redis-app.test_default (172.19.0.2): icmp_seq=1 ttl=64 time=0.060 ms
    64 bytes from redis-app.test_default (172.19.0.2): icmp_seq=2 ttl=64 time=0.060 ms
    64 bytes from redis-app.test_default (172.19.0.2): icmp_seq=3 ttl=64 time=0.145 ms
    64 bytes from redis-app.test_default (172.19.0.2): icmp_seq=4 ttl=64 time=0.054 ms
    

    external_links

    用于链接到外部容器。所有容器需要在同一个网络中。

    1. 首先,自定义一个网络net-a
    docker network create net-a --driver bridge
    
    1. 创建外部容器
    docker run --name redis-external --net net-a -d redis
    
    1. 部署docker-compose,使用外部网络net-a
    version: "3.4"
    services:
    
      redis-web:
        image: redis-web:1.0
        container_name: redis-web
        restart: always
        environment: 
          REDIS_HOST: redis
        ports:
          - 8001:8001
        links:
          - redis-app:redis
      redis-web-external:
        image: redis-web:1.0
        container_name: redis-web-external
        restart: always
        environment:
          REDIS_HOST: redis-external
        ports:
          - 8002:8001
        external_links:
          - redis-external
      redis-app:
        image: redis:latest
        container_name: redis-app
        restart: always
    networks:
      default:
        external:
          name: net-a
    

    web容器内部已经可以ping通外部容器了。

    [root@vm02 network-test]# docker exec -it 3ca6747e0ff7 ping redis-external
    PING redis-external (172.24.0.7) 56(84) bytes of data.
    64 bytes from redis-external.net-a (172.24.0.7): icmp_seq=1 ttl=64 time=0.091 ms
    64 bytes from redis-external.net-a (172.24.0.7): icmp_seq=2 ttl=64 time=0.122 ms
    64 bytes from redis-external.net-a (172.24.0.7): icmp_seq=3 ttl=64 time=0.185 ms
    

    相关文章

      网友评论

          本文标题:docker-compose links及external_li

          本文链接:https://www.haomeiwen.com/subject/bwvvectx.html