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
相關文章
相關標籤/搜索