1. 程式人生 > >Docker基礎篇3:容器管理

Docker基礎篇3:容器管理

1、建立容器常用選項

1.1、建立容器常用指令

【建立容器常用指令】

【建立容器是限制資源】

 

1.2、建立容器應用

【執行一個容器】

[[email protected]_190_147_centos ~]# docker container run -itd --name bs busybox
#-i表示互動式,-t表示偽終端,-d表示後臺執行,--name表示指定一個名字
226ef405c0dd90bad98c5912a673df931e1621a24a3b7a0b1ff1f8812e0037a4

【進入容器】

[[email protected]
_190_147_centos ~]# docker container attach bs #  / # ps -ef PID   USER     TIME  COMMAND     1 root      0:00 sh     7 root      0:00 ps -ef / # ip addr 

【如何進入一個容器退出之後讓其繼續執行?】 

[[email protected]_190_147_centos ~]# docker container attach bs
/ # mount
/dev/vda1 on /etc/resolv.conf type ext4 (rw,noatime,data=ordered) #dns解析的檔案
/dev/vda1 on /etc/hostname type ext4 (rw,noatime,data=ordered)
/dev/vda1 on /etc/hosts type ext4 (rw,noatime,data=ordered)
注意:每一次容器重啟的時候,都需要重新掛載/etc/resolv、/etc/hostname、/etc/hosts三個檔案。

【設定環境變數】

[[email protected]_190_147_centos ~]# docker container run -it -d -e wzy=wanzhuang --name bs1 busybox
1bb43c06c2d0831c0a6ece44bbb9160df242049c967dd5729c5824f4f4923dd1
[[email protected]_190_147_centos ~]# docker exec -it bs1 sh
/ # echo $wzy
wanzhuan

【埠對映】

[[email protected]_190_147_centos ~]# docker container run -itd -p 8080:80 --name nginx01 nginx
檢視是否能訪問
[
[email protected]
_190_147_centos ~]# curl http://localhost:8080

【檢視容器日誌的輸出】

[[email protected]_190_147_centos ~]# docker logs nginx01
172.17.0.1 - - [29/Nov/2018:07:15:03 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

[[email protected]_190_147_centos ~]# ls /var/lib/docker/containers/18c554b3d5b2c5b460da7fc60796e394401ce8c51de753b1118d4c6dcd19e3ac/
18c554b3d5b2c5b460da7fc60796e394401ce8c51de753b1118d4c6dcd19e3ac-json.log  hostconfig.json  mounts
checkpoints                                                                hostname         resolv.conf
config.v2.json                                                             hosts            resolv.conf.hash

【容器退出時重啟策略】

[[email protected]_190_147_centos ~]# docker container run -itd -p 8082:80 --restart=always --name nginx03 nginx
18cf7094bcc47df969d26c3a30e99f3bfcd4821330e110fcb9f9e7ee71f50eb3

【限制cpu/memory】

[[email protected]_190_147_centos ~]# docker container run --help |grep cpu
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
[[email protected]_190_147_centos ~]# docker container run -itd --cpus 1 --name nginx04 nginx
8de8f68f92269db19c98129390a2bc5f2976d53c67514b144391241b1b900dab
[[email protected]_190_147_centos ~]# docker container run -itd --memory 256m --name nginx05 nginx
檢視資源利用率:docker container stats 容器名稱
[[email protected]_190_147_centos ~]# docker container stats nginx05

2、管理容器常用指令

2.1、管理容器常用指令

2.2、管理容器應用