Docker设置固定ip地址

Docker安装后,默认会建立下面三种网络类型mysql

 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地址就变了sql

none:无指定网络

使用 --network=none ,docker 容器就不会分配局域网的IPdocker

host: 主机网络

使用 --network=host,此时,Docker 容器的网络会附属在主机上,二者是互通的。
例如,在容器中运行一个Web服务,监听8080端口,则主机的8080端口就会自动映射到容器中。vim

建立自定义网络:(设置固定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
相关文章
相关标签/搜索