🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] > [参考](https://my.oschina.net/jastme/blog/1499403) > [详细参考](http://www.louisvv.com/archives/695.html) ## 容器可ping 外网 (自定赋值ip) 方案一: 使用 host 网络 `docker run -it --network host centos:7.2.1511` 方案二: 创建网卡macvlan网卡,创建容器时只是不指定ip ``` > docker run --restart always --net mynet --name test2 -dit centos /bin/bash ``` ## 给容器局域网 ip (用 macvlan 赋予静态ip) 为容器添加一个局域网环境可访问的局域网ip 查看 是否支持 macvlan(centos>=7) ``` lsmod | grep macvlan macvlan 19046 0 ``` ### docker 版 `ip route show` 在宿主机执行 ``` > ip route default via 192.168.0.1 dev eth0 proto static metric 100 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 192.168.0.0/23 dev eth0 proto kernel scope link src 192.168.0.110 metric 100 // 创建 mynet 的网卡 > docker network create -d macvlan --subnet=192.168.0.0/23 -o parent=eth0 mynet // 指定ip > docker run --restart always --net mynet --name test2 --ip=192.168.0.100 --privileged=true -dit centos /bin/bash //查看 ip 是否修改 或 其他主机 ping > docker exec -it d22 test2 > cat /etc/hosts ``` 说明 ``` subnet:网络 CIDR 地址 gateway:网关地址 aux-address:不要分配给容器的 ip 地址。字典,以 key=value 对出现 ip-range:指定具体的 ip 分配区间,也是 CIDR 格式,必须是 subnet 指定范围的子集 opt(o):和 macvlan driver 相关的选项,以 key=value 的格式出现 parent=eth0: 指定 parent interface macvlan_mode:macvlan 模式,默认是 bridge ``` ### docker compose 版 查看网段 ``` > ip route default via 192.168.0.1 dev eth0 proto static metric 100 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 192.168.0.0/23 dev eth0 proto kernel scope link src 192.168.0.110 metric 100 ``` #### 方案一,选创建固定网卡网卡 1. 创建网卡 ``` docker network create -d macvlan --subnet=192.168.0.0/23 --gateway=192.168.0.1 -o parent=eth0 mynet ``` 2. 创建docker compose <details> <summary>docker-compose.yaml</summary> ``` version: '3' services: s6_175: build: . image: dist_s6 container_name: s7_17 restart: always tty: true networks: mynet: ipv4_address: 192.168.0.78 # entrypoint: # - /etc/init.d/bigant_control # - start networks: mynet: external: true ``` </details> <br/> <details> <summary>Dockerfile</summary> ``` FROM centos:7 ADD ./im_server.tar.gz /root/ RUN cd /root/oneinstack \ && ./install.sh -a WORKDIR /home/im_user/im_server/im_server ## 启动服务 ENTRYPOINT ["/etc/init.d/bigant_control" , "start"] ``` </details> <br/> #### 方案二: docker-compose.yaml 动态创建 <details> <summary>docker-compose.yaml</summary> ``` version: '3' services: s6_175: build: . image: dist_s6 container_name: s7_17 restart: always tty: true networks: mynet: ipv4_address: 192.168.0.78 # entrypoint: # - /etc/init.d/bigant_control # - start networks: mynet: driver: macvlan driver_opts: parent: eth0 ipam: config: - subnet: 192.168.0.0/23 ``` </details> <br/> <details> <summary>Dockerfile</summary> ``` FROM centos:7 ADD ./im_server.tar.gz /root/ RUN cd /root/oneinstack \ && ./install.sh -a WORKDIR /home/im_user/im_server/im_server ## 启动服务 ENTRYPOINT ["/etc/init.d/bigant_control" , "start"] ``` </details> <br/> #### macvlan 类型网卡只能创建一次 #### 问题:虚拟机ping不通外网 1. 方案一 `vim /etc/sysctl.conf`添加 ``` net.ipv4.ip_forward=1 ``` 2. 更换固定ip