docker使用外部网络

1.新建网络
可以手动新建或者使用已有docker-compose建好的网络(本例使用docker-compose已建好的)

手动:  docker network create root_defaul

查看已有docker网络:

docker network ls 
[root@k8s-master nacos]# docker network ls 
NETWORK ID     NAME            DRIVER    SCOPE
69636339e666   bridge          bridge    local
2a1f74b60414   host            host      local
3e74598db211   nacos_default   bridge    local
6240cb8cb524   none            null      local
b873332c5a05   root_defaul     bridge    local
be0a82ac753f   root_default    bridge    local

2.docker-compose引用外部网络(root_default )

version: "3"
services:
  nacos:
    image: nacos-server:v1.4.2
    container_name: saasoperation-nacos
    privileged: true
     # - ./cluster-logs:/home/nacos/logs
     #      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "17848:7848"
      - "18848:8848"
      - "19848:9848"
      - "19555:9555"
      - "19849:9849"
    env_file:
      - ./env/nacos-ip.env
    restart: always
    
    #主要为下方这两部分
    
    networks:
      - root_default # 指定网络
networks:
  root_default:
    external: true # 来自外部

3.验证

[root@k8s-master nacos]# docker exec -it  nacos bash 
[root@4632f3bae525 nacos]# ping fastdfs 
PING fastdfs (172.22.0.2) 56(84) bytes of data.
64 bytes from root_fastdfs_1.root_default (172.22.0.2): icmp_seq=1 ttl=64 time=0.124 ms
^C
--- fastdfs ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.124/0.124/0.124/0.000 ms

你可能感兴趣的:(docker,网络,容器)