docker网络2-自定义网络(none)

本文详细介绍了如何在Docker中使用--net=none参数启动容器,并手动配置其网络连接。通过创建veth pair接口,将接口B放入容器网络命名空间,并设置IP地址及默认网关,实现了容器的网络访问功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置容器网络

使用–net=none 参数,可以自行的配置网络,让容器具有网络访问的权限!

  • 启动一个/bin/bash容器,指定–net=none参数
[root@localhost ~]# docker run -i -t -d --net=none ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
9f2da5eac031422517e0919700748bcd8c13747014ef5d0b4ccad1d975514c81
  • 查看当前运行容器ID,创建网络命名空间
[root@localhost ~]# docker inspect -f '{
   {.State.Pid}}' 9f
63995
[root@localhost ~]# mkdir -p /var/run/netns
[root@localhost ~]# ln -s /proc/63995/ns/net /var/run/netns/63995
  • 检查桥接网卡的IP和子网掩码的信息
[root@localhost ~]# ip addr show docker0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
   link/ether 02:42:a5:52:8e:bb brd ff:ff:ff:ff:ff:ff
   inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
      valid_lft forever preferred_lft forever
   inet6 fe80::42:a5ff:fe52:8ebb/64 scope link
      valid_lft forever preferred_lft forever
  • 安装brctl
[root@localhost ~]# yum install bridge-utils -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package bridge-utils.x86_64 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值