Docker安装后,默认会创建下面三种网络类型
root@centos-mysql01:~#root@centos-mysql01:/data# docker network ls
NETWORK ID NAME DRIVER SCOPE
cab735099128 bridge bridge local
13a89298cb91 host host local
581ee02ee095 none null local
启动 Docker的时候,用 --network 参数,可以指定网络类型
root@centos-mysql01:~#docker run --privileged -itd --name centos_lnmp1.14 --network bridge --ip 172.17.0.10 centos /usr/sbin/init
bridge:桥接网络
默认情况下启动的Docker容器,都是使用 bridge,Docker安装时创建的桥接网络,每次Docker容器重启时,会按照顺序获取对应的IP地址,这个就导致重启下,Docker的IP地址就变了
none:无指定网络
使用 --network=none ,docker 容器就不会分配局域网的IP
host: 主机网络
使用 --network=host,此时,Docker 容器的网络会附属在主机上,两者是互通的。 例如,在容器中运行一个Web服务,监听8080端口,则主机的8080端口就会自动映射到容器中。
创建自定义网络:(设置固定IP)
root@centos-mysql01:~# docker network create --subnet=192.168.2.0/24 myhanye
步骤2: 创建Docker容器
root@centos-mysql01:~# docker run --privileged -itd --name hanye_centos --net myhanye --ip 192.168.2.2 centos /usr/sbin/init
开启sshd服务
root@centos-mysql01:~# docker exec -it 2be213ce23fa /bin/bash
[root@2be213ce23fa /]# yum install -y openssh-* vim
[root@2be213ce23fa /]# vim /etc/ssh/sshd_config
打开:Port 22
[root@2be213ce23fa /]# systemctl restart sshd