docker网卡的IP地址修改

1. 安装docker

请参考 Linux系统在线安装docker任意版本完整教程

2. dockers启动一个容器查看容器ip

docker run -d  --name  nginx  -p 80:80 nginx #启动一个容器
docker ps -a  #查看容器正常运行
docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx ##查看nginx容器ip
[root@ecs-214609 ~]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx
172.17.0.2
[root@ecs-214609 ~]# 

3. 修改docker网卡的IP

修改Docker宿主机的Docker网络
可以在Docker主机上修改/etc/docker/daemon.json文件,添加如下内容

[root@ecs-214609 docker]# cat /etc/docker/daemon.json 
{
 "registry-mirrors" : ["https://mj9kvemk.mirror.aliyuncs.com"],
 "bip": "192.168.1.5/24",
 "exec-opts":["native.cgroupdriver=systemd"]
}
[root@ecs-214609 docker]# systemctl restart docker
############### "bip": "192.168.1.5/24", 是新添加的

这会将Docker容器分配IP的子网修改为192.168.1.0/24,并且默认网关设置为192.168.1.1。
重启Docker后生效,此后创建的容器会在此子网分配IP

4.验证修改后的结果

[root@ecs-214609 docker]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:64:bb:ca brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.15/24 brd 192.168.0.255 scope global noprefixroute dynamic eth0
       valid_lft 73188sec preferred_lft 73188sec
    inet6 fe80::f816:3eff:fe64:bbca/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:6c:96:16:6a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.5/24 brd 192.168.10.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:6cff:fe96:166a/64 scope link 
       valid_lft forever preferred_lft forever
[root@ecs-214609 docker]# 
#############看到docker0的IP变化了
docker restart nginx #重启nginx容器
docker ps -a #查看状态
docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx  #查看容器ip
[root@ecs-214609 docker]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx  #看到容器IP变化了
192.168.10.1
[root@ecs-214609 docker]# 

到这里就成功了!

你可能感兴趣的:(docker容器相关,linux,docker,tcp/ip,容器)