1. docker run創建容器常見選項
1.1 創建容器
選項 | 描述 |
-i,-interactive | 交互式 |
-t,-tty | 分配一個偽終端 |
-d,-detach | 運行容器到后台 |
-e,-env | 設置環境變量 |
-p(小寫),-publish list | 發布容器端口到主機 |
-P(大寫),-publish -all | 發布容器所有EXPOSE的端口到宿主機隨機端口 |
--name string | 指定容器名稱 |
-h,-hostname | 指定容器主機名 |
-ip string | 指定容器IP,只能用於自定義網絡 |
-network | 連接容器到一個網絡 |
-v,-volume list | 綁定掛載一個卷 |
-restart string | 容器退出時重啟策略,默認no,可選值:【always|on-failure】 |
1. 2 容器資源限制
選項 | 描述 |
-m,--memory | 容器可以使用的最大內存量 |
-memory-swap | 允許交換到磁盤的內存量 |
-memory-swappiness=<0-100> | 容器使用swap分區交換的百分比(0-100,默認為-1) |
--oom-kill-disable | 禁止OOM killer |
--cpus | 可以使用的cpu數量 |
--cpuset-cpus | 限制容器使用特定的cpu核心,如(0-3, 0,1) |
--cpu-shares | cpu共享(相對權重) |
2. docker run 創建容器使用
2.1 docker run -it創建一個容器,並進入容器
1 [root@test-2 ~]# docker run -it nginx
2.2 docker run -d創建一個容器,並在后台運行
1 [root@test-1 playbooks]# docker run -it -d nginx
2.3 docker run -e創建一個容器,並設置環境變量
1 [root@test-1 playbooks]# docker run -it -d -e test=123456 nginx
2.4 docker run -it -d -p80:80 創建一個容器,並設置本機端口對應容器端口
1 [root@test-1 playbooks]# docker run -it -d -p80:80 nginx
2.5 docker run -it -name webnginx創建一個容器,並設置一個容器的名稱
1 [root@test-1 playbooks]# docker run -it -d --name webnginx nginx
2.6 docker run -it -d -P(大寫p)創建一個容器,並隨機分配一個本機端口對應容器端口
1 [root@test-1 ~]# docker run -it -d --name web1 -P nginx
3. docker 容器資源限制
3.1 案例1-內存限制-m參數,允許容器最多使用500M內存和100M的swap,並禁用OOM killer
1 [root@test-1 ~]# docker run -it -d --name nginx03 --memory="500m" --memory-swap="100m" --oom-kill-disable nginx 2 [root@test-1 ~]# docker stats nginx03 #查看運行狀態
3.2 cpu限額 ,允許容器最多使用一個的cpu
1 [root@test-1 ~]# docker run -it -d --name nginx04 --cpus="1" nginx 2 #允許容器最多使用50%的cpu 3 [root@test-1 ~]# docker run -it -d --name nginx05 --cpus=".5" nginx